| Line Number |
../DebugInfoTest/example_mips_dbg.ll
|
Hit count |
Line Number |
../DebugInfoTest/example_mips.ll
|
Hit count |
| 1 |
//===- ScalarEvolution.cpp - Scalar Evolution Analysis --------------------===// |
--- |
1 |
//===- ScalarEvolution.cpp - Scalar Evolution Analysis --------------------===// |
--- |
| 2 |
// |
--- |
2 |
// |
--- |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
--- |
3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
--- |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
--- |
4 |
// See https://llvm.org/LICENSE.txt for license information. |
--- |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
--- |
5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
--- |
| 6 |
// |
--- |
6 |
// |
--- |
| 7 |
//===----------------------------------------------------------------------===// |
--- |
7 |
//===----------------------------------------------------------------------===// |
--- |
| 8 |
// |
--- |
8 |
// |
--- |
| 9 |
// This file contains the implementation of the scalar evolution analysis |
--- |
9 |
// This file contains the implementation of the scalar evolution analysis |
--- |
| 10 |
// engine, which is used primarily to analyze expressions involving induction |
--- |
10 |
// engine, which is used primarily to analyze expressions involving induction |
--- |
| 11 |
// variables in loops. |
--- |
11 |
// variables in loops. |
--- |
| 12 |
// |
--- |
12 |
// |
--- |
| 13 |
// There are several aspects to this library. First is the representation of |
--- |
13 |
// There are several aspects to this library. First is the representation of |
--- |
| 14 |
// scalar expressions, which are represented as subclasses of the SCEV class. |
--- |
14 |
// scalar expressions, which are represented as subclasses of the SCEV class. |
--- |
| 15 |
// These classes are used to represent certain types of subexpressions that we |
--- |
15 |
// These classes are used to represent certain types of subexpressions that we |
--- |
| 16 |
// can handle. We only create one SCEV of a particular shape, so |
--- |
16 |
// can handle. We only create one SCEV of a particular shape, so |
--- |
| 17 |
// pointer-comparisons for equality are legal. |
--- |
17 |
// pointer-comparisons for equality are legal. |
--- |
| 18 |
// |
--- |
18 |
// |
--- |
| 19 |
// One important aspect of the SCEV objects is that they are never cyclic, even |
--- |
19 |
// One important aspect of the SCEV objects is that they are never cyclic, even |
--- |
| 20 |
// if there is a cycle in the dataflow for an expression (ie, a PHI node). If |
--- |
20 |
// if there is a cycle in the dataflow for an expression (ie, a PHI node). If |
--- |
| 21 |
// the PHI node is one of the idioms that we can represent (e.g., a polynomial |
--- |
21 |
// the PHI node is one of the idioms that we can represent (e.g., a polynomial |
--- |
| 22 |
// recurrence) then we represent it directly as a recurrence node, otherwise we |
--- |
22 |
// recurrence) then we represent it directly as a recurrence node, otherwise we |
--- |
| 23 |
// represent it as a SCEVUnknown node. |
--- |
23 |
// represent it as a SCEVUnknown node. |
--- |
| 24 |
// |
--- |
24 |
// |
--- |
| 25 |
// In addition to being able to represent expressions of various types, we also |
--- |
25 |
// In addition to being able to represent expressions of various types, we also |
--- |
| 26 |
// have folders that are used to build the *canonical* representation for a |
--- |
26 |
// have folders that are used to build the *canonical* representation for a |
--- |
| 27 |
// particular expression. These folders are capable of using a variety of |
--- |
27 |
// particular expression. These folders are capable of using a variety of |
--- |
| 28 |
// rewrite rules to simplify the expressions. |
--- |
28 |
// rewrite rules to simplify the expressions. |
--- |
| 29 |
// |
--- |
29 |
// |
--- |
| 30 |
// Once the folders are defined, we can implement the more interesting |
--- |
30 |
// Once the folders are defined, we can implement the more interesting |
--- |
| 31 |
// higher-level code, such as the code that recognizes PHI nodes of various |
--- |
31 |
// higher-level code, such as the code that recognizes PHI nodes of various |
--- |
| 32 |
// types, computes the execution count of a loop, etc. |
--- |
32 |
// types, computes the execution count of a loop, etc. |
--- |
| 33 |
// |
--- |
33 |
// |
--- |
| 34 |
// TODO: We should use these routines and value representations to implement |
--- |
34 |
// TODO: We should use these routines and value representations to implement |
--- |
| 35 |
// dependence analysis! |
--- |
35 |
// dependence analysis! |
--- |
| 36 |
// |
--- |
36 |
// |
--- |
| 37 |
//===----------------------------------------------------------------------===// |
--- |
37 |
//===----------------------------------------------------------------------===// |
--- |
| 38 |
// |
--- |
38 |
// |
--- |
| 39 |
// There are several good references for the techniques used in this analysis. |
--- |
39 |
// There are several good references for the techniques used in this analysis. |
--- |
| 40 |
// |
--- |
40 |
// |
--- |
| 41 |
// Chains of recurrences -- a method to expedite the evaluation |
--- |
41 |
// Chains of recurrences -- a method to expedite the evaluation |
--- |
| 42 |
// of closed-form functions |
--- |
42 |
// of closed-form functions |
--- |
| 43 |
// Olaf Bachmann, Paul S. Wang, Eugene V. Zima |
--- |
43 |
// Olaf Bachmann, Paul S. Wang, Eugene V. Zima |
--- |
| 44 |
// |
--- |
44 |
// |
--- |
| 45 |
// On computational properties of chains of recurrences |
--- |
45 |
// On computational properties of chains of recurrences |
--- |
| 46 |
// Eugene V. Zima |
--- |
46 |
// Eugene V. Zima |
--- |
| 47 |
// |
--- |
47 |
// |
--- |
| 48 |
// Symbolic Evaluation of Chains of Recurrences for Loop Optimization |
--- |
48 |
// Symbolic Evaluation of Chains of Recurrences for Loop Optimization |
--- |
| 49 |
// Robert A. van Engelen |
--- |
49 |
// Robert A. van Engelen |
--- |
| 50 |
// |
--- |
50 |
// |
--- |
| 51 |
// Efficient Symbolic Analysis for Optimizing Compilers |
--- |
51 |
// Efficient Symbolic Analysis for Optimizing Compilers |
--- |
| 52 |
// Robert A. van Engelen |
--- |
52 |
// Robert A. van Engelen |
--- |
| 53 |
// |
--- |
53 |
// |
--- |
| 54 |
// Using the chains of recurrences algebra for data dependence testing and |
--- |
54 |
// Using the chains of recurrences algebra for data dependence testing and |
--- |
| 55 |
// induction variable substitution |
--- |
55 |
// induction variable substitution |
--- |
| 56 |
// MS Thesis, Johnie Birch |
--- |
56 |
// MS Thesis, Johnie Birch |
--- |
| 57 |
// |
--- |
57 |
// |
--- |
| 58 |
//===----------------------------------------------------------------------===// |
--- |
58 |
//===----------------------------------------------------------------------===// |
--- |
| 59 |
|
--- |
59 |
|
--- |
| 60 |
#include "llvm/Analysis/ScalarEvolution.h" |
--- |
60 |
#include "llvm/Analysis/ScalarEvolution.h" |
--- |
| 61 |
#include "llvm/ADT/APInt.h" |
--- |
61 |
#include "llvm/ADT/APInt.h" |
--- |
| 62 |
#include "llvm/ADT/ArrayRef.h" |
--- |
62 |
#include "llvm/ADT/ArrayRef.h" |
--- |
| 63 |
#include "llvm/ADT/DenseMap.h" |
--- |
63 |
#include "llvm/ADT/DenseMap.h" |
--- |
| 64 |
#include "llvm/ADT/DepthFirstIterator.h" |
--- |
64 |
#include "llvm/ADT/DepthFirstIterator.h" |
--- |
| 65 |
#include "llvm/ADT/EquivalenceClasses.h" |
--- |
65 |
#include "llvm/ADT/EquivalenceClasses.h" |
--- |
| 66 |
#include "llvm/ADT/FoldingSet.h" |
--- |
66 |
#include "llvm/ADT/FoldingSet.h" |
--- |
| 67 |
#include "llvm/ADT/STLExtras.h" |
--- |
67 |
#include "llvm/ADT/STLExtras.h" |
--- |
| 68 |
#include "llvm/ADT/ScopeExit.h" |
--- |
68 |
#include "llvm/ADT/ScopeExit.h" |
--- |
| 69 |
#include "llvm/ADT/Sequence.h" |
--- |
69 |
#include "llvm/ADT/Sequence.h" |
--- |
| 70 |
#include "llvm/ADT/SmallPtrSet.h" |
--- |
70 |
#include "llvm/ADT/SmallPtrSet.h" |
--- |
| 71 |
#include "llvm/ADT/SmallSet.h" |
--- |
71 |
#include "llvm/ADT/SmallSet.h" |
--- |
| 72 |
#include "llvm/ADT/SmallVector.h" |
--- |
72 |
#include "llvm/ADT/SmallVector.h" |
--- |
| 73 |
#include "llvm/ADT/Statistic.h" |
--- |
73 |
#include "llvm/ADT/Statistic.h" |
--- |
| 74 |
#include "llvm/ADT/StringExtras.h" |
--- |
74 |
#include "llvm/ADT/StringExtras.h" |
--- |
| 75 |
#include "llvm/ADT/StringRef.h" |
--- |
75 |
#include "llvm/ADT/StringRef.h" |
--- |
| 76 |
#include "llvm/Analysis/AssumptionCache.h" |
--- |
76 |
#include "llvm/Analysis/AssumptionCache.h" |
--- |
| 77 |
#include "llvm/Analysis/ConstantFolding.h" |
--- |
77 |
#include "llvm/Analysis/ConstantFolding.h" |
--- |
| 78 |
#include "llvm/Analysis/InstructionSimplify.h" |
--- |
78 |
#include "llvm/Analysis/InstructionSimplify.h" |
--- |
| 79 |
#include "llvm/Analysis/LoopInfo.h" |
--- |
79 |
#include "llvm/Analysis/LoopInfo.h" |
--- |
| 80 |
#include "llvm/Analysis/MemoryBuiltins.h" |
--- |
80 |
#include "llvm/Analysis/MemoryBuiltins.h" |
--- |
| 81 |
#include "llvm/Analysis/ScalarEvolutionExpressions.h" |
--- |
81 |
#include "llvm/Analysis/ScalarEvolutionExpressions.h" |
--- |
| 82 |
#include "llvm/Analysis/TargetLibraryInfo.h" |
--- |
82 |
#include "llvm/Analysis/TargetLibraryInfo.h" |
--- |
| 83 |
#include "llvm/Analysis/ValueTracking.h" |
--- |
83 |
#include "llvm/Analysis/ValueTracking.h" |
--- |
| 84 |
#include "llvm/Config/llvm-config.h" |
--- |
84 |
#include "llvm/Config/llvm-config.h" |
--- |
| 85 |
#include "llvm/IR/Argument.h" |
--- |
85 |
#include "llvm/IR/Argument.h" |
--- |
| 86 |
#include "llvm/IR/BasicBlock.h" |
--- |
86 |
#include "llvm/IR/BasicBlock.h" |
--- |
| 87 |
#include "llvm/IR/CFG.h" |
--- |
87 |
#include "llvm/IR/CFG.h" |
--- |
| 88 |
#include "llvm/IR/Constant.h" |
--- |
88 |
#include "llvm/IR/Constant.h" |
--- |
| 89 |
#include "llvm/IR/ConstantRange.h" |
--- |
89 |
#include "llvm/IR/ConstantRange.h" |
--- |
| 90 |
#include "llvm/IR/Constants.h" |
--- |
90 |
#include "llvm/IR/Constants.h" |
--- |
| 91 |
#include "llvm/IR/DataLayout.h" |
--- |
91 |
#include "llvm/IR/DataLayout.h" |
--- |
| 92 |
#include "llvm/IR/DerivedTypes.h" |
--- |
92 |
#include "llvm/IR/DerivedTypes.h" |
--- |
| 93 |
#include "llvm/IR/Dominators.h" |
--- |
93 |
#include "llvm/IR/Dominators.h" |
--- |
| 94 |
#include "llvm/IR/Function.h" |
--- |
94 |
#include "llvm/IR/Function.h" |
--- |
| 95 |
#include "llvm/IR/GlobalAlias.h" |
--- |
95 |
#include "llvm/IR/GlobalAlias.h" |
--- |
| 96 |
#include "llvm/IR/GlobalValue.h" |
--- |
96 |
#include "llvm/IR/GlobalValue.h" |
--- |
| 97 |
#include "llvm/IR/InstIterator.h" |
--- |
97 |
#include "llvm/IR/InstIterator.h" |
--- |
| 98 |
#include "llvm/IR/InstrTypes.h" |
--- |
98 |
#include "llvm/IR/InstrTypes.h" |
--- |
| 99 |
#include "llvm/IR/Instruction.h" |
--- |
99 |
#include "llvm/IR/Instruction.h" |
--- |
| 100 |
#include "llvm/IR/Instructions.h" |
--- |
100 |
#include "llvm/IR/Instructions.h" |
--- |
| 101 |
#include "llvm/IR/IntrinsicInst.h" |
--- |
101 |
#include "llvm/IR/IntrinsicInst.h" |
--- |
| 102 |
#include "llvm/IR/Intrinsics.h" |
--- |
102 |
#include "llvm/IR/Intrinsics.h" |
--- |
| 103 |
#include "llvm/IR/LLVMContext.h" |
--- |
103 |
#include "llvm/IR/LLVMContext.h" |
--- |
| 104 |
#include "llvm/IR/Operator.h" |
--- |
104 |
#include "llvm/IR/Operator.h" |
--- |
| 105 |
#include "llvm/IR/PatternMatch.h" |
--- |
105 |
#include "llvm/IR/PatternMatch.h" |
--- |
| 106 |
#include "llvm/IR/Type.h" |
--- |
106 |
#include "llvm/IR/Type.h" |
--- |
| 107 |
#include "llvm/IR/Use.h" |
--- |
107 |
#include "llvm/IR/Use.h" |
--- |
| 108 |
#include "llvm/IR/User.h" |
--- |
108 |
#include "llvm/IR/User.h" |
--- |
| 109 |
#include "llvm/IR/Value.h" |
--- |
109 |
#include "llvm/IR/Value.h" |
--- |
| 110 |
#include "llvm/IR/Verifier.h" |
--- |
110 |
#include "llvm/IR/Verifier.h" |
--- |
| 111 |
#include "llvm/InitializePasses.h" |
--- |
111 |
#include "llvm/InitializePasses.h" |
--- |
| 112 |
#include "llvm/Pass.h" |
--- |
112 |
#include "llvm/Pass.h" |
--- |
| 113 |
#include "llvm/Support/Casting.h" |
--- |
113 |
#include "llvm/Support/Casting.h" |
--- |
| 114 |
#include "llvm/Support/CommandLine.h" |
--- |
114 |
#include "llvm/Support/CommandLine.h" |
--- |
| 115 |
#include "llvm/Support/Compiler.h" |
--- |
115 |
#include "llvm/Support/Compiler.h" |
--- |
| 116 |
#include "llvm/Support/Debug.h" |
--- |
116 |
#include "llvm/Support/Debug.h" |
--- |
| 117 |
#include "llvm/Support/ErrorHandling.h" |
--- |
117 |
#include "llvm/Support/ErrorHandling.h" |
--- |
| 118 |
#include "llvm/Support/KnownBits.h" |
--- |
118 |
#include "llvm/Support/KnownBits.h" |
--- |
| 119 |
#include "llvm/Support/SaveAndRestore.h" |
--- |
119 |
#include "llvm/Support/SaveAndRestore.h" |
--- |
| 120 |
#include "llvm/Support/raw_ostream.h" |
--- |
120 |
#include "llvm/Support/raw_ostream.h" |
--- |
| 121 |
#include |
--- |
121 |
#include |
--- |
| 122 |
#include |
--- |
122 |
#include |
--- |
| 123 |
#include |
--- |
123 |
#include |
--- |
| 124 |
#include |
--- |
124 |
#include |
--- |
| 125 |
#include |
--- |
125 |
#include |
--- |
| 126 |
#include |
--- |
126 |
#include |
--- |
| 127 |
#include |
--- |
127 |
#include |
--- |
| 128 |
#include |
--- |
128 |
#include |
--- |
| 129 |
#include |
--- |
129 |
#include |
--- |
| 130 |
#include |
--- |
130 |
#include |
--- |
| 131 |
#include |
--- |
131 |
#include |
--- |
| 132 |
#include |
--- |
132 |
#include |
--- |
| 133 |
|
--- |
133 |
|
--- |
| 134 |
using namespace llvm; |
--- |
134 |
using namespace llvm; |
--- |
| 135 |
using namespace PatternMatch; |
--- |
135 |
using namespace PatternMatch; |
--- |
| 136 |
|
--- |
136 |
|
--- |
| 137 |
#define DEBUG_TYPE "scalar-evolution" |
--- |
137 |
#define DEBUG_TYPE "scalar-evolution" |
--- |
| 138 |
|
--- |
138 |
|
--- |
| 139 |
STATISTIC(NumExitCountsComputed, |
--- |
139 |
STATISTIC(NumExitCountsComputed, |
--- |
| 140 |
"Number of loop exits with predictable exit counts"); |
--- |
140 |
"Number of loop exits with predictable exit counts"); |
--- |
| 141 |
STATISTIC(NumExitCountsNotComputed, |
--- |
141 |
STATISTIC(NumExitCountsNotComputed, |
--- |
| 142 |
"Number of loop exits without predictable exit counts"); |
--- |
142 |
"Number of loop exits without predictable exit counts"); |
--- |
| 143 |
STATISTIC(NumBruteForceTripCountsComputed, |
--- |
143 |
STATISTIC(NumBruteForceTripCountsComputed, |
--- |
| 144 |
"Number of loops with trip counts computed by force"); |
--- |
144 |
"Number of loops with trip counts computed by force"); |
--- |
| 145 |
|
--- |
145 |
|
--- |
| 146 |
#ifdef EXPENSIVE_CHECKS |
--- |
146 |
#ifdef EXPENSIVE_CHECKS |
--- |
| 147 |
bool llvm::VerifySCEV = true; |
--- |
147 |
bool llvm::VerifySCEV = true; |
--- |
| 148 |
#else |
--- |
148 |
#else |
--- |
| 149 |
bool llvm::VerifySCEV = false; |
--- |
149 |
bool llvm::VerifySCEV = false; |
--- |
| 150 |
#endif |
--- |
150 |
#endif |
--- |
| 151 |
|
--- |
151 |
|
--- |
| 152 |
static cl::opt |
--- |
152 |
static cl::opt |
--- |
| 153 |
MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, |
--- |
153 |
MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, |
--- |
| 154 |
cl::desc("Maximum number of iterations SCEV will " |
--- |
154 |
cl::desc("Maximum number of iterations SCEV will " |
--- |
| 155 |
"symbolically execute a constant " |
--- |
155 |
"symbolically execute a constant " |
--- |
| 156 |
"derived loop"), |
--- |
156 |
"derived loop"), |
--- |
| 157 |
cl::init(100)); |
--- |
157 |
cl::init(100)); |
--- |
| 158 |
|
--- |
158 |
|
--- |
| 159 |
static cl::opt VerifySCEVOpt( |
--- |
159 |
static cl::opt VerifySCEVOpt( |
--- |
| 160 |
"verify-scev", cl::Hidden, cl::location(VerifySCEV), |
--- |
160 |
"verify-scev", cl::Hidden, cl::location(VerifySCEV), |
--- |
| 161 |
cl::desc("Verify ScalarEvolution's backedge taken counts (slow)")); |
--- |
161 |
cl::desc("Verify ScalarEvolution's backedge taken counts (slow)")); |
--- |
| 162 |
static cl::opt VerifySCEVStrict( |
--- |
162 |
static cl::opt VerifySCEVStrict( |
--- |
| 163 |
"verify-scev-strict", cl::Hidden, |
--- |
163 |
"verify-scev-strict", cl::Hidden, |
--- |
| 164 |
cl::desc("Enable stricter verification with -verify-scev is passed")); |
--- |
164 |
cl::desc("Enable stricter verification with -verify-scev is passed")); |
--- |
| 165 |
|
--- |
165 |
|
--- |
| 166 |
static cl::opt VerifyIR( |
--- |
166 |
static cl::opt VerifyIR( |
--- |
| 167 |
"scev-verify-ir", cl::Hidden, |
--- |
167 |
"scev-verify-ir", cl::Hidden, |
--- |
| 168 |
cl::desc("Verify IR correctness when making sensitive SCEV queries (slow)"), |
--- |
168 |
cl::desc("Verify IR correctness when making sensitive SCEV queries (slow)"), |
--- |
| 169 |
cl::init(false)); |
--- |
169 |
cl::init(false)); |
--- |
| 170 |
|
--- |
170 |
|
--- |
| 171 |
static cl::opt MulOpsInlineThreshold( |
--- |
171 |
static cl::opt MulOpsInlineThreshold( |
--- |
| 172 |
"scev-mulops-inline-threshold", cl::Hidden, |
--- |
172 |
"scev-mulops-inline-threshold", cl::Hidden, |
--- |
| 173 |
cl::desc("Threshold for inlining multiplication operands into a SCEV"), |
--- |
173 |
cl::desc("Threshold for inlining multiplication operands into a SCEV"), |
--- |
| 174 |
cl::init(32)); |
--- |
174 |
cl::init(32)); |
--- |
| 175 |
|
--- |
175 |
|
--- |
| 176 |
static cl::opt AddOpsInlineThreshold( |
--- |
176 |
static cl::opt AddOpsInlineThreshold( |
--- |
| 177 |
"scev-addops-inline-threshold", cl::Hidden, |
--- |
177 |
"scev-addops-inline-threshold", cl::Hidden, |
--- |
| 178 |
cl::desc("Threshold for inlining addition operands into a SCEV"), |
--- |
178 |
cl::desc("Threshold for inlining addition operands into a SCEV"), |
--- |
| 179 |
cl::init(500)); |
--- |
179 |
cl::init(500)); |
--- |
| 180 |
|
--- |
180 |
|
--- |
| 181 |
static cl::opt MaxSCEVCompareDepth( |
--- |
181 |
static cl::opt MaxSCEVCompareDepth( |
--- |
| 182 |
"scalar-evolution-max-scev-compare-depth", cl::Hidden, |
--- |
182 |
"scalar-evolution-max-scev-compare-depth", cl::Hidden, |
--- |
| 183 |
cl::desc("Maximum depth of recursive SCEV complexity comparisons"), |
--- |
183 |
cl::desc("Maximum depth of recursive SCEV complexity comparisons"), |
--- |
| 184 |
cl::init(32)); |
--- |
184 |
cl::init(32)); |
--- |
| 185 |
|
--- |
185 |
|
--- |
| 186 |
static cl::opt MaxSCEVOperationsImplicationDepth( |
--- |
186 |
static cl::opt MaxSCEVOperationsImplicationDepth( |
--- |
| 187 |
"scalar-evolution-max-scev-operations-implication-depth", cl::Hidden, |
--- |
187 |
"scalar-evolution-max-scev-operations-implication-depth", cl::Hidden, |
--- |
| 188 |
cl::desc("Maximum depth of recursive SCEV operations implication analysis"), |
--- |
188 |
cl::desc("Maximum depth of recursive SCEV operations implication analysis"), |
--- |
| 189 |
cl::init(2)); |
--- |
189 |
cl::init(2)); |
--- |
| 190 |
|
--- |
190 |
|
--- |
| 191 |
static cl::opt MaxValueCompareDepth( |
--- |
191 |
static cl::opt MaxValueCompareDepth( |
--- |
| 192 |
"scalar-evolution-max-value-compare-depth", cl::Hidden, |
--- |
192 |
"scalar-evolution-max-value-compare-depth", cl::Hidden, |
--- |
| 193 |
cl::desc("Maximum depth of recursive value complexity comparisons"), |
--- |
193 |
cl::desc("Maximum depth of recursive value complexity comparisons"), |
--- |
| 194 |
cl::init(2)); |
--- |
194 |
cl::init(2)); |
--- |
| 195 |
|
--- |
195 |
|
--- |
| 196 |
static cl::opt |
--- |
196 |
static cl::opt |
--- |
| 197 |
MaxArithDepth("scalar-evolution-max-arith-depth", cl::Hidden, |
--- |
197 |
MaxArithDepth("scalar-evolution-max-arith-depth", cl::Hidden, |
--- |
| 198 |
cl::desc("Maximum depth of recursive arithmetics"), |
--- |
198 |
cl::desc("Maximum depth of recursive arithmetics"), |
--- |
| 199 |
cl::init(32)); |
--- |
199 |
cl::init(32)); |
--- |
| 200 |
|
--- |
200 |
|
--- |
| 201 |
static cl::opt MaxConstantEvolvingDepth( |
--- |
201 |
static cl::opt MaxConstantEvolvingDepth( |
--- |
| 202 |
"scalar-evolution-max-constant-evolving-depth", cl::Hidden, |
--- |
202 |
"scalar-evolution-max-constant-evolving-depth", cl::Hidden, |
--- |
| 203 |
cl::desc("Maximum depth of recursive constant evolving"), cl::init(32)); |
--- |
203 |
cl::desc("Maximum depth of recursive constant evolving"), cl::init(32)); |
--- |
| 204 |
|
--- |
204 |
|
--- |
| 205 |
static cl::opt |
--- |
205 |
static cl::opt |
--- |
| 206 |
MaxCastDepth("scalar-evolution-max-cast-depth", cl::Hidden, |
--- |
206 |
MaxCastDepth("scalar-evolution-max-cast-depth", cl::Hidden, |
--- |
| 207 |
cl::desc("Maximum depth of recursive SExt/ZExt/Trunc"), |
--- |
207 |
cl::desc("Maximum depth of recursive SExt/ZExt/Trunc"), |
--- |
| 208 |
cl::init(8)); |
--- |
208 |
cl::init(8)); |
--- |
| 209 |
|
--- |
209 |
|
--- |
| 210 |
static cl::opt |
--- |
210 |
static cl::opt |
--- |
| 211 |
MaxAddRecSize("scalar-evolution-max-add-rec-size", cl::Hidden, |
--- |
211 |
MaxAddRecSize("scalar-evolution-max-add-rec-size", cl::Hidden, |
--- |
| 212 |
cl::desc("Max coefficients in AddRec during evolving"), |
--- |
212 |
cl::desc("Max coefficients in AddRec during evolving"), |
--- |
| 213 |
cl::init(8)); |
--- |
213 |
cl::init(8)); |
--- |
| 214 |
|
--- |
214 |
|
--- |
| 215 |
static cl::opt |
--- |
215 |
static cl::opt |
--- |
| 216 |
HugeExprThreshold("scalar-evolution-huge-expr-threshold", cl::Hidden, |
--- |
216 |
HugeExprThreshold("scalar-evolution-huge-expr-threshold", cl::Hidden, |
--- |
| 217 |
cl::desc("Size of the expression which is considered huge"), |
--- |
217 |
cl::desc("Size of the expression which is considered huge"), |
--- |
| 218 |
cl::init(4096)); |
--- |
218 |
cl::init(4096)); |
--- |
| 219 |
|
--- |
219 |
|
--- |
| 220 |
static cl::opt RangeIterThreshold( |
--- |
220 |
static cl::opt RangeIterThreshold( |
--- |
| 221 |
"scev-range-iter-threshold", cl::Hidden, |
--- |
221 |
"scev-range-iter-threshold", cl::Hidden, |
--- |
| 222 |
cl::desc("Threshold for switching to iteratively computing SCEV ranges"), |
--- |
222 |
cl::desc("Threshold for switching to iteratively computing SCEV ranges"), |
--- |
| 223 |
cl::init(32)); |
--- |
223 |
cl::init(32)); |
--- |
| 224 |
|
--- |
224 |
|
--- |
| 225 |
static cl::opt |
--- |
225 |
static cl::opt |
--- |
| 226 |
ClassifyExpressions("scalar-evolution-classify-expressions", |
--- |
226 |
ClassifyExpressions("scalar-evolution-classify-expressions", |
--- |
| 227 |
cl::Hidden, cl::init(true), |
--- |
227 |
cl::Hidden, cl::init(true), |
--- |
| 228 |
cl::desc("When printing analysis, include information on every instruction")); |
--- |
228 |
cl::desc("When printing analysis, include information on every instruction")); |
--- |
| 229 |
|
--- |
229 |
|
--- |
| 230 |
static cl::opt UseExpensiveRangeSharpening( |
--- |
230 |
static cl::opt UseExpensiveRangeSharpening( |
--- |
| 231 |
"scalar-evolution-use-expensive-range-sharpening", cl::Hidden, |
--- |
231 |
"scalar-evolution-use-expensive-range-sharpening", cl::Hidden, |
--- |
| 232 |
cl::init(false), |
--- |
232 |
cl::init(false), |
--- |
| 233 |
cl::desc("Use more powerful methods of sharpening expression ranges. May " |
--- |
233 |
cl::desc("Use more powerful methods of sharpening expression ranges. May " |
--- |
| 234 |
"be costly in terms of compile time")); |
--- |
234 |
"be costly in terms of compile time")); |
--- |
| 235 |
|
--- |
235 |
|
--- |
| 236 |
static cl::opt MaxPhiSCCAnalysisSize( |
--- |
236 |
static cl::opt MaxPhiSCCAnalysisSize( |
--- |
| 237 |
"scalar-evolution-max-scc-analysis-depth", cl::Hidden, |
--- |
237 |
"scalar-evolution-max-scc-analysis-depth", cl::Hidden, |
--- |
| 238 |
cl::desc("Maximum amount of nodes to process while searching SCEVUnknown " |
--- |
238 |
cl::desc("Maximum amount of nodes to process while searching SCEVUnknown " |
--- |
| 239 |
"Phi strongly connected components"), |
--- |
239 |
"Phi strongly connected components"), |
--- |
| 240 |
cl::init(8)); |
--- |
240 |
cl::init(8)); |
--- |
| 241 |
|
--- |
241 |
|
--- |
| 242 |
static cl::opt |
--- |
242 |
static cl::opt |
--- |
| 243 |
EnableFiniteLoopControl("scalar-evolution-finite-loop", cl::Hidden, |
--- |
243 |
EnableFiniteLoopControl("scalar-evolution-finite-loop", cl::Hidden, |
--- |
| 244 |
cl::desc("Handle <= and >= in finite loops"), |
--- |
244 |
cl::desc("Handle <= and >= in finite loops"), |
--- |
| 245 |
cl::init(true)); |
--- |
245 |
cl::init(true)); |
--- |
| 246 |
|
--- |
246 |
|
--- |
| 247 |
static cl::opt UseContextForNoWrapFlagInference( |
--- |
247 |
static cl::opt UseContextForNoWrapFlagInference( |
--- |
| 248 |
"scalar-evolution-use-context-for-no-wrap-flag-strenghening", cl::Hidden, |
--- |
248 |
"scalar-evolution-use-context-for-no-wrap-flag-strenghening", cl::Hidden, |
--- |
| 249 |
cl::desc("Infer nuw/nsw flags using context where suitable"), |
--- |
249 |
cl::desc("Infer nuw/nsw flags using context where suitable"), |
--- |
| 250 |
cl::init(true)); |
--- |
250 |
cl::init(true)); |
--- |
| 251 |
|
--- |
251 |
|
--- |
| 252 |
//===----------------------------------------------------------------------===// |
--- |
252 |
//===----------------------------------------------------------------------===// |
--- |
| 253 |
// SCEV class definitions |
--- |
253 |
// SCEV class definitions |
--- |
| 254 |
//===----------------------------------------------------------------------===// |
--- |
254 |
//===----------------------------------------------------------------------===// |
--- |
| 255 |
|
--- |
255 |
|
--- |
| 256 |
//===----------------------------------------------------------------------===// |
--- |
256 |
//===----------------------------------------------------------------------===// |
--- |
| 257 |
// Implementation of the SCEV class. |
--- |
257 |
// Implementation of the SCEV class. |
--- |
| 258 |
// |
--- |
258 |
// |
--- |
| 259 |
|
--- |
259 |
|
--- |
| 260 |
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
--- |
260 |
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
--- |
| 261 |
LLVM_DUMP_METHOD void SCEV::dump() const { |
0 |
261 |
LLVM_DUMP_METHOD void SCEV::dump() const { |
0 |
| 262 |
print(dbgs()); |
0 |
262 |
print(dbgs()); |
0 |
| 263 |
dbgs() << '\n'; |
0 |
263 |
dbgs() << '\n'; |
0 |
| 264 |
} |
0 |
264 |
} |
0 |
| 265 |
#endif |
--- |
265 |
#endif |
--- |
| 266 |
|
--- |
266 |
|
--- |
| 267 |
void SCEV::print(raw_ostream &OS) const { |
0 |
267 |
void SCEV::print(raw_ostream &OS) const { |
0 |
| 268 |
switch (getSCEVType()) { |
0 |
268 |
switch (getSCEVType()) { |
0 |
| 269 |
case scConstant: |
0 |
269 |
case scConstant: |
0 |
| 270 |
cast(this)->getValue()->printAsOperand(OS, false); |
0 |
270 |
cast(this)->getValue()->printAsOperand(OS, false); |
0 |
| 271 |
return; |
0 |
271 |
return; |
0 |
| 272 |
case scVScale: |
0 |
272 |
case scVScale: |
0 |
| 273 |
OS << "vscale"; |
0 |
273 |
OS << "vscale"; |
0 |
| 274 |
return; |
0 |
274 |
return; |
0 |
| 275 |
case scPtrToInt: { |
0 |
275 |
case scPtrToInt: { |
0 |
| 276 |
const SCEVPtrToIntExpr *PtrToInt = cast(this); |
0 |
276 |
const SCEVPtrToIntExpr *PtrToInt = cast(this); |
0 |
| 277 |
const SCEV *Op = PtrToInt->getOperand(); |
0 |
277 |
const SCEV *Op = PtrToInt->getOperand(); |
0 |
| 278 |
OS << "(ptrtoint " << *Op->getType() << " " << *Op << " to " |
0 |
278 |
OS << "(ptrtoint " << *Op->getType() << " " << *Op << " to " |
0 |
| 279 |
<< *PtrToInt->getType() << ")"; |
0 |
279 |
<< *PtrToInt->getType() << ")"; |
0 |
| 280 |
return; |
0 |
280 |
return; |
0 |
| 281 |
} |
--- |
281 |
} |
--- |
| 282 |
case scTruncate: { |
0 |
282 |
case scTruncate: { |
0 |
| 283 |
const SCEVTruncateExpr *Trunc = cast(this); |
0 |
283 |
const SCEVTruncateExpr *Trunc = cast(this); |
0 |
| 284 |
const SCEV *Op = Trunc->getOperand(); |
0 |
284 |
const SCEV *Op = Trunc->getOperand(); |
0 |
| 285 |
OS << "(trunc " << *Op->getType() << " " << *Op << " to " |
0 |
285 |
OS << "(trunc " << *Op->getType() << " " << *Op << " to " |
0 |
| 286 |
<< *Trunc->getType() << ")"; |
0 |
286 |
<< *Trunc->getType() << ")"; |
0 |
| 287 |
return; |
0 |
287 |
return; |
0 |
| 288 |
} |
--- |
288 |
} |
--- |
| 289 |
case scZeroExtend: { |
0 |
289 |
case scZeroExtend: { |
0 |
| 290 |
const SCEVZeroExtendExpr *ZExt = cast(this); |
0 |
290 |
const SCEVZeroExtendExpr *ZExt = cast(this); |
0 |
| 291 |
const SCEV *Op = ZExt->getOperand(); |
0 |
291 |
const SCEV *Op = ZExt->getOperand(); |
0 |
| 292 |
OS << "(zext " << *Op->getType() << " " << *Op << " to " |
0 |
292 |
OS << "(zext " << *Op->getType() << " " << *Op << " to " |
0 |
| 293 |
<< *ZExt->getType() << ")"; |
0 |
293 |
<< *ZExt->getType() << ")"; |
0 |
| 294 |
return; |
0 |
294 |
return; |
0 |
| 295 |
} |
--- |
295 |
} |
--- |
| 296 |
case scSignExtend: { |
0 |
296 |
case scSignExtend: { |
0 |
| 297 |
const SCEVSignExtendExpr *SExt = cast(this); |
0 |
297 |
const SCEVSignExtendExpr *SExt = cast(this); |
0 |
| 298 |
const SCEV *Op = SExt->getOperand(); |
0 |
298 |
const SCEV *Op = SExt->getOperand(); |
0 |
| 299 |
OS << "(sext " << *Op->getType() << " " << *Op << " to " |
0 |
299 |
OS << "(sext " << *Op->getType() << " " << *Op << " to " |
0 |
| 300 |
<< *SExt->getType() << ")"; |
0 |
300 |
<< *SExt->getType() << ")"; |
0 |
| 301 |
return; |
0 |
301 |
return; |
0 |
| 302 |
} |
--- |
302 |
} |
--- |
| 303 |
case scAddRecExpr: { |
0 |
303 |
case scAddRecExpr: { |
0 |
| 304 |
const SCEVAddRecExpr *AR = cast(this); |
0 |
304 |
const SCEVAddRecExpr *AR = cast(this); |
0 |
| 305 |
OS << "{" << *AR->getOperand(0); |
0 |
305 |
OS << "{" << *AR->getOperand(0); |
0 |
| 306 |
for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i) |
0 |
306 |
for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i) |
0 |
| 307 |
OS << ",+," << *AR->getOperand(i); |
0 |
307 |
OS << ",+," << *AR->getOperand(i); |
0 |
| 308 |
OS << "}<"; |
0 |
308 |
OS << "}<"; |
0 |
| 309 |
if (AR->hasNoUnsignedWrap()) |
0 |
309 |
if (AR->hasNoUnsignedWrap()) |
0 |
| 310 |
OS << "nuw><"; |
0 |
310 |
OS << "nuw><"; |
0 |
| 311 |
if (AR->hasNoSignedWrap()) |
0 |
311 |
if (AR->hasNoSignedWrap()) |
0 |
| 312 |
OS << "nsw><"; |
0 |
312 |
OS << "nsw><"; |
0 |
| 313 |
if (AR->hasNoSelfWrap() && |
0 |
313 |
if (AR->hasNoSelfWrap() && |
0 |
| 314 |
!AR->getNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW))) |
0 |
314 |
!AR->getNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW))) |
0 |
| 315 |
OS << "nw><"; |
0 |
315 |
OS << "nw><"; |
0 |
| 316 |
AR->getLoop()->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
316 |
AR->getLoop()->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
| 317 |
OS << ">"; |
0 |
317 |
OS << ">"; |
0 |
| 318 |
return; |
0 |
318 |
return; |
0 |
| 319 |
} |
--- |
319 |
} |
--- |
| 320 |
case scAddExpr: |
0 |
320 |
case scAddExpr: |
0 |
| 321 |
case scMulExpr: |
--- |
321 |
case scMulExpr: |
--- |
| 322 |
case scUMaxExpr: |
--- |
322 |
case scUMaxExpr: |
--- |
| 323 |
case scSMaxExpr: |
--- |
323 |
case scSMaxExpr: |
--- |
| 324 |
case scUMinExpr: |
--- |
324 |
case scUMinExpr: |
--- |
| 325 |
case scSMinExpr: |
--- |
325 |
case scSMinExpr: |
--- |
| 326 |
case scSequentialUMinExpr: { |
--- |
326 |
case scSequentialUMinExpr: { |
--- |
| 327 |
const SCEVNAryExpr *NAry = cast(this); |
0 |
327 |
const SCEVNAryExpr *NAry = cast(this); |
0 |
| 328 |
const char *OpStr = nullptr; |
0 |
328 |
const char *OpStr = nullptr; |
0 |
| 329 |
switch (NAry->getSCEVType()) { |
0 |
329 |
switch (NAry->getSCEVType()) { |
0 |
| 330 |
case scAddExpr: OpStr = " + "; break; |
0 |
330 |
case scAddExpr: OpStr = " + "; break; |
0 |
| 331 |
case scMulExpr: OpStr = " * "; break; |
0 |
331 |
case scMulExpr: OpStr = " * "; break; |
0 |
| 332 |
case scUMaxExpr: OpStr = " umax "; break; |
0 |
332 |
case scUMaxExpr: OpStr = " umax "; break; |
0 |
| 333 |
case scSMaxExpr: OpStr = " smax "; break; |
0 |
333 |
case scSMaxExpr: OpStr = " smax "; break; |
0 |
| 334 |
case scUMinExpr: |
0 |
334 |
case scUMinExpr: |
0 |
| 335 |
OpStr = " umin "; |
0 |
335 |
OpStr = " umin "; |
0 |
| 336 |
break; |
0 |
336 |
break; |
0 |
| 337 |
case scSMinExpr: |
0 |
337 |
case scSMinExpr: |
0 |
| 338 |
OpStr = " smin "; |
0 |
338 |
OpStr = " smin "; |
0 |
| 339 |
break; |
0 |
339 |
break; |
0 |
| 340 |
case scSequentialUMinExpr: |
0 |
340 |
case scSequentialUMinExpr: |
0 |
| 341 |
OpStr = " umin_seq "; |
0 |
341 |
OpStr = " umin_seq "; |
0 |
| 342 |
break; |
0 |
342 |
break; |
0 |
| 343 |
default: |
0 |
343 |
default: |
0 |
| 344 |
llvm_unreachable("There are no other nary expression types."); |
0 |
344 |
llvm_unreachable("There are no other nary expression types."); |
0 |
| 345 |
} |
--- |
345 |
} |
--- |
| 346 |
OS << "("; |
0 |
346 |
OS << "("; |
0 |
| 347 |
ListSeparator LS(OpStr); |
0 |
347 |
ListSeparator LS(OpStr); |
0 |
| 348 |
for (const SCEV *Op : NAry->operands()) |
0 |
348 |
for (const SCEV *Op : NAry->operands()) |
0 |
| 349 |
OS << LS << *Op; |
0 |
349 |
OS << LS << *Op; |
0 |
| 350 |
OS << ")"; |
0 |
350 |
OS << ")"; |
0 |
| 351 |
switch (NAry->getSCEVType()) { |
0 |
351 |
switch (NAry->getSCEVType()) { |
0 |
| 352 |
case scAddExpr: |
0 |
352 |
case scAddExpr: |
0 |
| 353 |
case scMulExpr: |
--- |
353 |
case scMulExpr: |
--- |
| 354 |
if (NAry->hasNoUnsignedWrap()) |
0 |
354 |
if (NAry->hasNoUnsignedWrap()) |
0 |
| 355 |
OS << ""; |
0 |
355 |
OS << ""; |
0 |
| 356 |
if (NAry->hasNoSignedWrap()) |
0 |
356 |
if (NAry->hasNoSignedWrap()) |
0 |
| 357 |
OS << ""; |
0 |
357 |
OS << ""; |
0 |
| 358 |
break; |
0 |
358 |
break; |
0 |
| 359 |
default: |
0 |
359 |
default: |
0 |
| 360 |
// Nothing to print for other nary expressions. |
--- |
360 |
// Nothing to print for other nary expressions. |
--- |
| 361 |
break; |
0 |
361 |
break; |
0 |
| 362 |
} |
--- |
362 |
} |
--- |
| 363 |
return; |
0 |
363 |
return; |
0 |
| 364 |
} |
--- |
364 |
} |
--- |
| 365 |
case scUDivExpr: { |
0 |
365 |
case scUDivExpr: { |
0 |
| 366 |
const SCEVUDivExpr *UDiv = cast(this); |
0 |
366 |
const SCEVUDivExpr *UDiv = cast(this); |
0 |
| 367 |
OS << "(" << *UDiv->getLHS() << " /u " << *UDiv->getRHS() << ")"; |
0 |
367 |
OS << "(" << *UDiv->getLHS() << " /u " << *UDiv->getRHS() << ")"; |
0 |
| 368 |
return; |
0 |
368 |
return; |
0 |
| 369 |
} |
--- |
369 |
} |
--- |
| 370 |
case scUnknown: |
0 |
370 |
case scUnknown: |
0 |
| 371 |
cast(this)->getValue()->printAsOperand(OS, false); |
0 |
371 |
cast(this)->getValue()->printAsOperand(OS, false); |
0 |
| 372 |
return; |
0 |
372 |
return; |
0 |
| 373 |
case scCouldNotCompute: |
0 |
373 |
case scCouldNotCompute: |
0 |
| 374 |
OS << "***COULDNOTCOMPUTE***"; |
0 |
374 |
OS << "***COULDNOTCOMPUTE***"; |
0 |
| 375 |
return; |
0 |
375 |
return; |
0 |
| 376 |
} |
--- |
376 |
} |
--- |
| 377 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
377 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
| 378 |
} |
--- |
378 |
} |
--- |
| 379 |
|
--- |
379 |
|
--- |
| 380 |
Type *SCEV::getType() const { |
0 |
380 |
Type *SCEV::getType() const { |
0 |
| 381 |
switch (getSCEVType()) { |
0 |
381 |
switch (getSCEVType()) { |
0 |
| 382 |
case scConstant: |
0 |
382 |
case scConstant: |
0 |
| 383 |
return cast(this)->getType(); |
0 |
383 |
return cast(this)->getType(); |
0 |
| 384 |
case scVScale: |
0 |
384 |
case scVScale: |
0 |
| 385 |
return cast(this)->getType(); |
0 |
385 |
return cast(this)->getType(); |
0 |
| 386 |
case scPtrToInt: |
0 |
386 |
case scPtrToInt: |
0 |
| 387 |
case scTruncate: |
--- |
387 |
case scTruncate: |
--- |
| 388 |
case scZeroExtend: |
--- |
388 |
case scZeroExtend: |
--- |
| 389 |
case scSignExtend: |
--- |
389 |
case scSignExtend: |
--- |
| 390 |
return cast(this)->getType(); |
0 |
390 |
return cast(this)->getType(); |
0 |
| 391 |
case scAddRecExpr: |
0 |
391 |
case scAddRecExpr: |
0 |
| 392 |
return cast(this)->getType(); |
0 |
392 |
return cast(this)->getType(); |
0 |
| 393 |
case scMulExpr: |
0 |
393 |
case scMulExpr: |
0 |
| 394 |
return cast(this)->getType(); |
0 |
394 |
return cast(this)->getType(); |
0 |
| 395 |
case scUMaxExpr: |
0 |
395 |
case scUMaxExpr: |
0 |
| 396 |
case scSMaxExpr: |
--- |
396 |
case scSMaxExpr: |
--- |
| 397 |
case scUMinExpr: |
--- |
397 |
case scUMinExpr: |
--- |
| 398 |
case scSMinExpr: |
--- |
398 |
case scSMinExpr: |
--- |
| 399 |
return cast(this)->getType(); |
0 |
399 |
return cast(this)->getType(); |
0 |
| 400 |
case scSequentialUMinExpr: |
0 |
400 |
case scSequentialUMinExpr: |
0 |
| 401 |
return cast(this)->getType(); |
0 |
401 |
return cast(this)->getType(); |
0 |
| 402 |
case scAddExpr: |
0 |
402 |
case scAddExpr: |
0 |
| 403 |
return cast(this)->getType(); |
0 |
403 |
return cast(this)->getType(); |
0 |
| 404 |
case scUDivExpr: |
0 |
404 |
case scUDivExpr: |
0 |
| 405 |
return cast(this)->getType(); |
0 |
405 |
return cast(this)->getType(); |
0 |
| 406 |
case scUnknown: |
0 |
406 |
case scUnknown: |
0 |
| 407 |
return cast(this)->getType(); |
0 |
407 |
return cast(this)->getType(); |
0 |
| 408 |
case scCouldNotCompute: |
0 |
408 |
case scCouldNotCompute: |
0 |
| 409 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
409 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
| 410 |
} |
--- |
410 |
} |
--- |
| 411 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
411 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
| 412 |
} |
--- |
412 |
} |
--- |
| 413 |
|
--- |
413 |
|
--- |
| 414 |
ArrayRef SCEV::operands() const { |
0 |
414 |
ArrayRef SCEV::operands() const { |
0 |
| 415 |
switch (getSCEVType()) { |
0 |
415 |
switch (getSCEVType()) { |
0 |
| 416 |
case scConstant: |
0 |
416 |
case scConstant: |
0 |
| 417 |
case scVScale: |
--- |
417 |
case scVScale: |
--- |
| 418 |
case scUnknown: |
--- |
418 |
case scUnknown: |
--- |
| 419 |
return {}; |
0 |
419 |
return {}; |
0 |
| 420 |
case scPtrToInt: |
0 |
420 |
case scPtrToInt: |
0 |
| 421 |
case scTruncate: |
--- |
421 |
case scTruncate: |
--- |
| 422 |
case scZeroExtend: |
--- |
422 |
case scZeroExtend: |
--- |
| 423 |
case scSignExtend: |
--- |
423 |
case scSignExtend: |
--- |
| 424 |
return cast(this)->operands(); |
0 |
424 |
return cast(this)->operands(); |
0 |
| 425 |
case scAddRecExpr: |
0 |
425 |
case scAddRecExpr: |
0 |
| 426 |
case scAddExpr: |
--- |
426 |
case scAddExpr: |
--- |
| 427 |
case scMulExpr: |
--- |
427 |
case scMulExpr: |
--- |
| 428 |
case scUMaxExpr: |
--- |
428 |
case scUMaxExpr: |
--- |
| 429 |
case scSMaxExpr: |
--- |
429 |
case scSMaxExpr: |
--- |
| 430 |
case scUMinExpr: |
--- |
430 |
case scUMinExpr: |
--- |
| 431 |
case scSMinExpr: |
--- |
431 |
case scSMinExpr: |
--- |
| 432 |
case scSequentialUMinExpr: |
--- |
432 |
case scSequentialUMinExpr: |
--- |
| 433 |
return cast(this)->operands(); |
0 |
433 |
return cast(this)->operands(); |
0 |
| 434 |
case scUDivExpr: |
0 |
434 |
case scUDivExpr: |
0 |
| 435 |
return cast(this)->operands(); |
0 |
435 |
return cast(this)->operands(); |
0 |
| 436 |
case scCouldNotCompute: |
0 |
436 |
case scCouldNotCompute: |
0 |
| 437 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
437 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
| 438 |
} |
--- |
438 |
} |
--- |
| 439 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
439 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
| 440 |
} |
--- |
440 |
} |
--- |
| 441 |
|
--- |
441 |
|
--- |
| 442 |
bool SCEV::isZero() const { |
0 |
442 |
bool SCEV::isZero() const { |
0 |
| 443 |
if (const SCEVConstant *SC = dyn_cast(this)) |
0 |
443 |
if (const SCEVConstant *SC = dyn_cast(this)) |
0 |
| 444 |
return SC->getValue()->isZero(); |
0 |
444 |
return SC->getValue()->isZero(); |
0 |
| 445 |
return false; |
0 |
445 |
return false; |
0 |
| 446 |
} |
--- |
446 |
} |
--- |
| 447 |
|
--- |
447 |
|
--- |
| 448 |
bool SCEV::isOne() const { |
0 |
448 |
bool SCEV::isOne() const { |
0 |
| 449 |
if (const SCEVConstant *SC = dyn_cast(this)) |
0 |
449 |
if (const SCEVConstant *SC = dyn_cast(this)) |
0 |
| 450 |
return SC->getValue()->isOne(); |
0 |
450 |
return SC->getValue()->isOne(); |
0 |
| 451 |
return false; |
0 |
451 |
return false; |
0 |
| 452 |
} |
--- |
452 |
} |
--- |
| 453 |
|
--- |
453 |
|
--- |
| 454 |
bool SCEV::isAllOnesValue() const { |
0 |
454 |
bool SCEV::isAllOnesValue() const { |
0 |
| 455 |
if (const SCEVConstant *SC = dyn_cast(this)) |
0 |
455 |
if (const SCEVConstant *SC = dyn_cast(this)) |
0 |
| 456 |
return SC->getValue()->isMinusOne(); |
0 |
456 |
return SC->getValue()->isMinusOne(); |
0 |
| 457 |
return false; |
0 |
457 |
return false; |
0 |
| 458 |
} |
--- |
458 |
} |
--- |
| 459 |
|
--- |
459 |
|
--- |
| 460 |
bool SCEV::isNonConstantNegative() const { |
0 |
460 |
bool SCEV::isNonConstantNegative() const { |
0 |
| 461 |
const SCEVMulExpr *Mul = dyn_cast(this); |
0 |
461 |
const SCEVMulExpr *Mul = dyn_cast(this); |
0 |
| 462 |
if (!Mul) return false; |
0 |
462 |
if (!Mul) return false; |
0 |
| 463 |
|
--- |
463 |
|
--- |
| 464 |
// If there is a constant factor, it will be first. |
--- |
464 |
// If there is a constant factor, it will be first. |
--- |
| 465 |
const SCEVConstant *SC = dyn_cast(Mul->getOperand(0)); |
0 |
465 |
const SCEVConstant *SC = dyn_cast(Mul->getOperand(0)); |
0 |
| 466 |
if (!SC) return false; |
0 |
466 |
if (!SC) return false; |
0 |
| 467 |
|
--- |
467 |
|
--- |
| 468 |
// Return true if the value is negative, this matches things like (-42 * V). |
--- |
468 |
// Return true if the value is negative, this matches things like (-42 * V). |
--- |
| 469 |
return SC->getAPInt().isNegative(); |
0 |
469 |
return SC->getAPInt().isNegative(); |
0 |
| 470 |
} |
--- |
470 |
} |
--- |
| 471 |
|
--- |
471 |
|
--- |
| 472 |
SCEVCouldNotCompute::SCEVCouldNotCompute() : |
1 |
472 |
SCEVCouldNotCompute::SCEVCouldNotCompute() : |
1 |
| 473 |
SCEV(FoldingSetNodeIDRef(), scCouldNotCompute, 0) {} |
1 |
473 |
SCEV(FoldingSetNodeIDRef(), scCouldNotCompute, 0) {} |
1 |
| 474 |
|
--- |
474 |
|
--- |
| 475 |
bool SCEVCouldNotCompute::classof(const SCEV *S) { |
0 |
475 |
bool SCEVCouldNotCompute::classof(const SCEV *S) { |
0 |
| 476 |
return S->getSCEVType() == scCouldNotCompute; |
0 |
476 |
return S->getSCEVType() == scCouldNotCompute; |
0 |
| 477 |
} |
--- |
477 |
} |
--- |
| 478 |
|
--- |
478 |
|
--- |
| 479 |
const SCEV *ScalarEvolution::getConstant(ConstantInt *V) { |
0 |
479 |
const SCEV *ScalarEvolution::getConstant(ConstantInt *V) { |
0 |
| 480 |
FoldingSetNodeID ID; |
0 |
480 |
FoldingSetNodeID ID; |
0 |
| 481 |
ID.AddInteger(scConstant); |
0 |
481 |
ID.AddInteger(scConstant); |
0 |
| 482 |
ID.AddPointer(V); |
0 |
482 |
ID.AddPointer(V); |
0 |
| 483 |
void *IP = nullptr; |
0 |
483 |
void *IP = nullptr; |
0 |
| 484 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
0 |
484 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
0 |
| 485 |
SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V); |
0 |
485 |
SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V); |
0 |
| 486 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
486 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 487 |
return S; |
0 |
487 |
return S; |
0 |
| 488 |
} |
0 |
488 |
} |
0 |
| 489 |
|
--- |
489 |
|
--- |
| 490 |
const SCEV *ScalarEvolution::getConstant(const APInt &Val) { |
0 |
490 |
const SCEV *ScalarEvolution::getConstant(const APInt &Val) { |
0 |
| 491 |
return getConstant(ConstantInt::get(getContext(), Val)); |
0 |
491 |
return getConstant(ConstantInt::get(getContext(), Val)); |
0 |
| 492 |
} |
--- |
492 |
} |
--- |
| 493 |
|
--- |
493 |
|
--- |
| 494 |
const SCEV * |
--- |
494 |
const SCEV * |
--- |
| 495 |
ScalarEvolution::getConstant(Type *Ty, uint64_t V, bool isSigned) { |
0 |
495 |
ScalarEvolution::getConstant(Type *Ty, uint64_t V, bool isSigned) { |
0 |
| 496 |
IntegerType *ITy = cast(getEffectiveSCEVType(Ty)); |
0 |
496 |
IntegerType *ITy = cast(getEffectiveSCEVType(Ty)); |
0 |
| 497 |
return getConstant(ConstantInt::get(ITy, V, isSigned)); |
0 |
497 |
return getConstant(ConstantInt::get(ITy, V, isSigned)); |
0 |
| 498 |
} |
--- |
498 |
} |
--- |
| 499 |
|
--- |
499 |
|
--- |
| 500 |
const SCEV *ScalarEvolution::getVScale(Type *Ty) { |
0 |
500 |
const SCEV *ScalarEvolution::getVScale(Type *Ty) { |
0 |
| 501 |
FoldingSetNodeID ID; |
0 |
501 |
FoldingSetNodeID ID; |
0 |
| 502 |
ID.AddInteger(scVScale); |
0 |
502 |
ID.AddInteger(scVScale); |
0 |
| 503 |
ID.AddPointer(Ty); |
0 |
503 |
ID.AddPointer(Ty); |
0 |
| 504 |
void *IP = nullptr; |
0 |
504 |
void *IP = nullptr; |
0 |
| 505 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) |
0 |
505 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) |
0 |
| 506 |
return S; |
0 |
506 |
return S; |
0 |
| 507 |
SCEV *S = new (SCEVAllocator) SCEVVScale(ID.Intern(SCEVAllocator), Ty); |
0 |
507 |
SCEV *S = new (SCEVAllocator) SCEVVScale(ID.Intern(SCEVAllocator), Ty); |
0 |
| 508 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
508 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 509 |
return S; |
0 |
509 |
return S; |
0 |
| 510 |
} |
0 |
510 |
} |
0 |
| 511 |
|
--- |
511 |
|
--- |
| 512 |
SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy, |
0 |
512 |
SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy, |
0 |
| 513 |
const SCEV *op, Type *ty) |
--- |
513 |
const SCEV *op, Type *ty) |
--- |
| 514 |
: SCEV(ID, SCEVTy, computeExpressionSize(op)), Op(op), Ty(ty) {} |
0 |
514 |
: SCEV(ID, SCEVTy, computeExpressionSize(op)), Op(op), Ty(ty) {} |
0 |
| 515 |
|
--- |
515 |
|
--- |
| 516 |
SCEVPtrToIntExpr::SCEVPtrToIntExpr(const FoldingSetNodeIDRef ID, const SCEV *Op, |
0 |
516 |
SCEVPtrToIntExpr::SCEVPtrToIntExpr(const FoldingSetNodeIDRef ID, const SCEV *Op, |
0 |
| 517 |
Type *ITy) |
--- |
517 |
Type *ITy) |
--- |
| 518 |
: SCEVCastExpr(ID, scPtrToInt, Op, ITy) { |
0 |
518 |
: SCEVCastExpr(ID, scPtrToInt, Op, ITy) { |
0 |
| 519 |
assert(getOperand()->getType()->isPointerTy() && Ty->isIntegerTy() && |
0 |
519 |
assert(getOperand()->getType()->isPointerTy() && Ty->isIntegerTy() && |
0 |
| 520 |
"Must be a non-bit-width-changing pointer-to-integer cast!"); |
--- |
520 |
"Must be a non-bit-width-changing pointer-to-integer cast!"); |
--- |
| 521 |
} |
0 |
521 |
} |
0 |
| 522 |
|
--- |
522 |
|
--- |
| 523 |
SCEVIntegralCastExpr::SCEVIntegralCastExpr(const FoldingSetNodeIDRef ID, |
0 |
523 |
SCEVIntegralCastExpr::SCEVIntegralCastExpr(const FoldingSetNodeIDRef ID, |
0 |
| 524 |
SCEVTypes SCEVTy, const SCEV *op, |
--- |
524 |
SCEVTypes SCEVTy, const SCEV *op, |
--- |
| 525 |
Type *ty) |
--- |
525 |
Type *ty) |
--- |
| 526 |
: SCEVCastExpr(ID, SCEVTy, op, ty) {} |
0 |
526 |
: SCEVCastExpr(ID, SCEVTy, op, ty) {} |
0 |
| 527 |
|
--- |
527 |
|
--- |
| 528 |
SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID, const SCEV *op, |
0 |
528 |
SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID, const SCEV *op, |
0 |
| 529 |
Type *ty) |
--- |
529 |
Type *ty) |
--- |
| 530 |
: SCEVIntegralCastExpr(ID, scTruncate, op, ty) { |
0 |
530 |
: SCEVIntegralCastExpr(ID, scTruncate, op, ty) { |
0 |
| 531 |
assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
531 |
assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
| 532 |
"Cannot truncate non-integer value!"); |
--- |
532 |
"Cannot truncate non-integer value!"); |
--- |
| 533 |
} |
0 |
533 |
} |
0 |
| 534 |
|
--- |
534 |
|
--- |
| 535 |
SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID, |
0 |
535 |
SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID, |
0 |
| 536 |
const SCEV *op, Type *ty) |
--- |
536 |
const SCEV *op, Type *ty) |
--- |
| 537 |
: SCEVIntegralCastExpr(ID, scZeroExtend, op, ty) { |
0 |
537 |
: SCEVIntegralCastExpr(ID, scZeroExtend, op, ty) { |
0 |
| 538 |
assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
538 |
assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
| 539 |
"Cannot zero extend non-integer value!"); |
--- |
539 |
"Cannot zero extend non-integer value!"); |
--- |
| 540 |
} |
0 |
540 |
} |
0 |
| 541 |
|
--- |
541 |
|
--- |
| 542 |
SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID, |
0 |
542 |
SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID, |
0 |
| 543 |
const SCEV *op, Type *ty) |
--- |
543 |
const SCEV *op, Type *ty) |
--- |
| 544 |
: SCEVIntegralCastExpr(ID, scSignExtend, op, ty) { |
0 |
544 |
: SCEVIntegralCastExpr(ID, scSignExtend, op, ty) { |
0 |
| 545 |
assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
545 |
assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
| 546 |
"Cannot sign extend non-integer value!"); |
--- |
546 |
"Cannot sign extend non-integer value!"); |
--- |
| 547 |
} |
0 |
547 |
} |
0 |
| 548 |
|
--- |
548 |
|
--- |
| 549 |
void SCEVUnknown::deleted() { |
0 |
549 |
void SCEVUnknown::deleted() { |
0 |
| 550 |
// Clear this SCEVUnknown from various maps. |
--- |
550 |
// Clear this SCEVUnknown from various maps. |
--- |
| 551 |
SE->forgetMemoizedResults(this); |
0 |
551 |
SE->forgetMemoizedResults(this); |
0 |
| 552 |
|
--- |
552 |
|
--- |
| 553 |
// Remove this SCEVUnknown from the uniquing map. |
--- |
553 |
// Remove this SCEVUnknown from the uniquing map. |
--- |
| 554 |
SE->UniqueSCEVs.RemoveNode(this); |
0 |
554 |
SE->UniqueSCEVs.RemoveNode(this); |
0 |
| 555 |
|
--- |
555 |
|
--- |
| 556 |
// Release the value. |
--- |
556 |
// Release the value. |
--- |
| 557 |
setValPtr(nullptr); |
0 |
557 |
setValPtr(nullptr); |
0 |
| 558 |
} |
0 |
558 |
} |
0 |
| 559 |
|
--- |
559 |
|
--- |
| 560 |
void SCEVUnknown::allUsesReplacedWith(Value *New) { |
0 |
560 |
void SCEVUnknown::allUsesReplacedWith(Value *New) { |
0 |
| 561 |
// Clear this SCEVUnknown from various maps. |
--- |
561 |
// Clear this SCEVUnknown from various maps. |
--- |
| 562 |
SE->forgetMemoizedResults(this); |
0 |
562 |
SE->forgetMemoizedResults(this); |
0 |
| 563 |
|
--- |
563 |
|
--- |
| 564 |
// Remove this SCEVUnknown from the uniquing map. |
--- |
564 |
// Remove this SCEVUnknown from the uniquing map. |
--- |
| 565 |
SE->UniqueSCEVs.RemoveNode(this); |
0 |
565 |
SE->UniqueSCEVs.RemoveNode(this); |
0 |
| 566 |
|
--- |
566 |
|
--- |
| 567 |
// Replace the value pointer in case someone is still using this SCEVUnknown. |
--- |
567 |
// Replace the value pointer in case someone is still using this SCEVUnknown. |
--- |
| 568 |
setValPtr(New); |
0 |
568 |
setValPtr(New); |
0 |
| 569 |
} |
0 |
569 |
} |
0 |
| 570 |
|
--- |
570 |
|
--- |
| 571 |
//===----------------------------------------------------------------------===// |
--- |
571 |
//===----------------------------------------------------------------------===// |
--- |
| 572 |
// SCEV Utilities |
--- |
572 |
// SCEV Utilities |
--- |
| 573 |
//===----------------------------------------------------------------------===// |
--- |
573 |
//===----------------------------------------------------------------------===// |
--- |
| 574 |
|
--- |
574 |
|
--- |
| 575 |
/// Compare the two values \p LV and \p RV in terms of their "complexity" where |
--- |
575 |
/// Compare the two values \p LV and \p RV in terms of their "complexity" where |
--- |
| 576 |
/// "complexity" is a partial (and somewhat ad-hoc) relation used to order |
--- |
576 |
/// "complexity" is a partial (and somewhat ad-hoc) relation used to order |
--- |
| 577 |
/// operands in SCEV expressions. \p EqCache is a set of pairs of values that |
--- |
577 |
/// operands in SCEV expressions. \p EqCache is a set of pairs of values that |
--- |
| 578 |
/// have been previously deemed to be "equally complex" by this routine. It is |
--- |
578 |
/// have been previously deemed to be "equally complex" by this routine. It is |
--- |
| 579 |
/// intended to avoid exponential time complexity in cases like: |
--- |
579 |
/// intended to avoid exponential time complexity in cases like: |
--- |
| 580 |
/// |
--- |
580 |
/// |
--- |
| 581 |
/// %a = f(%x, %y) |
--- |
581 |
/// %a = f(%x, %y) |
--- |
| 582 |
/// %b = f(%a, %a) |
--- |
582 |
/// %b = f(%a, %a) |
--- |
| 583 |
/// %c = f(%b, %b) |
--- |
583 |
/// %c = f(%b, %b) |
--- |
| 584 |
/// |
--- |
584 |
/// |
--- |
| 585 |
/// %d = f(%x, %y) |
--- |
585 |
/// %d = f(%x, %y) |
--- |
| 586 |
/// %e = f(%d, %d) |
--- |
586 |
/// %e = f(%d, %d) |
--- |
| 587 |
/// %f = f(%e, %e) |
--- |
587 |
/// %f = f(%e, %e) |
--- |
| 588 |
/// |
--- |
588 |
/// |
--- |
| 589 |
/// CompareValueComplexity(%f, %c) |
--- |
589 |
/// CompareValueComplexity(%f, %c) |
--- |
| 590 |
/// |
--- |
590 |
/// |
--- |
| 591 |
/// Since we do not continue running this routine on expression trees once we |
--- |
591 |
/// Since we do not continue running this routine on expression trees once we |
--- |
| 592 |
/// have seen unequal values, there is no need to track them in the cache. |
--- |
592 |
/// have seen unequal values, there is no need to track them in the cache. |
--- |
| 593 |
static int |
--- |
593 |
static int |
--- |
| 594 |
CompareValueComplexity(EquivalenceClasses &EqCacheValue, |
0 |
594 |
CompareValueComplexity(EquivalenceClasses &EqCacheValue, |
0 |
| 595 |
const LoopInfo *const LI, Value *LV, Value *RV, |
--- |
595 |
const LoopInfo *const LI, Value *LV, Value *RV, |
--- |
| 596 |
unsigned Depth) { |
--- |
596 |
unsigned Depth) { |
--- |
| 597 |
if (Depth > MaxValueCompareDepth || EqCacheValue.isEquivalent(LV, RV)) |
0 |
597 |
if (Depth > MaxValueCompareDepth || EqCacheValue.isEquivalent(LV, RV)) |
0 |
| 598 |
return 0; |
0 |
598 |
return 0; |
0 |
| 599 |
|
--- |
599 |
|
--- |
| 600 |
// Order pointer values after integer values. This helps SCEVExpander form |
--- |
600 |
// Order pointer values after integer values. This helps SCEVExpander form |
--- |
| 601 |
// GEPs. |
--- |
601 |
// GEPs. |
--- |
| 602 |
bool LIsPointer = LV->getType()->isPointerTy(), |
0 |
602 |
bool LIsPointer = LV->getType()->isPointerTy(), |
0 |
| 603 |
RIsPointer = RV->getType()->isPointerTy(); |
0 |
603 |
RIsPointer = RV->getType()->isPointerTy(); |
0 |
| 604 |
if (LIsPointer != RIsPointer) |
0 |
604 |
if (LIsPointer != RIsPointer) |
0 |
| 605 |
return (int)LIsPointer - (int)RIsPointer; |
0 |
605 |
return (int)LIsPointer - (int)RIsPointer; |
0 |
| 606 |
|
--- |
606 |
|
--- |
| 607 |
// Compare getValueID values. |
--- |
607 |
// Compare getValueID values. |
--- |
| 608 |
unsigned LID = LV->getValueID(), RID = RV->getValueID(); |
0 |
608 |
unsigned LID = LV->getValueID(), RID = RV->getValueID(); |
0 |
| 609 |
if (LID != RID) |
0 |
609 |
if (LID != RID) |
0 |
| 610 |
return (int)LID - (int)RID; |
0 |
610 |
return (int)LID - (int)RID; |
0 |
| 611 |
|
--- |
611 |
|
--- |
| 612 |
// Sort arguments by their position. |
--- |
612 |
// Sort arguments by their position. |
--- |
| 613 |
if (const auto *LA = dyn_cast(LV)) { |
0 |
613 |
if (const auto *LA = dyn_cast(LV)) { |
0 |
| 614 |
const auto *RA = cast(RV); |
0 |
614 |
const auto *RA = cast(RV); |
0 |
| 615 |
unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo(); |
0 |
615 |
unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo(); |
0 |
| 616 |
return (int)LArgNo - (int)RArgNo; |
0 |
616 |
return (int)LArgNo - (int)RArgNo; |
0 |
| 617 |
} |
--- |
617 |
} |
--- |
| 618 |
|
--- |
618 |
|
--- |
| 619 |
if (const auto *LGV = dyn_cast(LV)) { |
0 |
619 |
if (const auto *LGV = dyn_cast(LV)) { |
0 |
| 620 |
const auto *RGV = cast(RV); |
0 |
620 |
const auto *RGV = cast(RV); |
0 |
| 621 |
|
--- |
621 |
|
--- |
| 622 |
const auto IsGVNameSemantic = [&](const GlobalValue *GV) { |
0 |
622 |
const auto IsGVNameSemantic = [&](const GlobalValue *GV) { |
0 |
| 623 |
auto LT = GV->getLinkage(); |
0 |
623 |
auto LT = GV->getLinkage(); |
0 |
| 624 |
return !(GlobalValue::isPrivateLinkage(LT) || |
0 |
624 |
return !(GlobalValue::isPrivateLinkage(LT) || |
0 |
| 625 |
GlobalValue::isInternalLinkage(LT)); |
0 |
625 |
GlobalValue::isInternalLinkage(LT)); |
0 |
| 626 |
}; |
--- |
626 |
}; |
--- |
| 627 |
|
--- |
627 |
|
--- |
| 628 |
// Use the names to distinguish the two values, but only if the |
--- |
628 |
// Use the names to distinguish the two values, but only if the |
--- |
| 629 |
// names are semantically important. |
--- |
629 |
// names are semantically important. |
--- |
| 630 |
if (IsGVNameSemantic(LGV) && IsGVNameSemantic(RGV)) |
0 |
630 |
if (IsGVNameSemantic(LGV) && IsGVNameSemantic(RGV)) |
0 |
| 631 |
return LGV->getName().compare(RGV->getName()); |
0 |
631 |
return LGV->getName().compare(RGV->getName()); |
0 |
| 632 |
} |
--- |
632 |
} |
--- |
| 633 |
|
--- |
633 |
|
--- |
| 634 |
// For instructions, compare their loop depth, and their operand count. This |
--- |
634 |
// For instructions, compare their loop depth, and their operand count. This |
--- |
| 635 |
// is pretty loose. |
--- |
635 |
// is pretty loose. |
--- |
| 636 |
if (const auto *LInst = dyn_cast(LV)) { |
0 |
636 |
if (const auto *LInst = dyn_cast(LV)) { |
0 |
| 637 |
const auto *RInst = cast(RV); |
0 |
637 |
const auto *RInst = cast(RV); |
0 |
| 638 |
|
--- |
638 |
|
--- |
| 639 |
// Compare loop depths. |
--- |
639 |
// Compare loop depths. |
--- |
| 640 |
const BasicBlock *LParent = LInst->getParent(), |
0 |
640 |
const BasicBlock *LParent = LInst->getParent(), |
0 |
| 641 |
*RParent = RInst->getParent(); |
0 |
641 |
*RParent = RInst->getParent(); |
0 |
| 642 |
if (LParent != RParent) { |
0 |
642 |
if (LParent != RParent) { |
0 |
| 643 |
unsigned LDepth = LI->getLoopDepth(LParent), |
0 |
643 |
unsigned LDepth = LI->getLoopDepth(LParent), |
0 |
| 644 |
RDepth = LI->getLoopDepth(RParent); |
0 |
644 |
RDepth = LI->getLoopDepth(RParent); |
0 |
| 645 |
if (LDepth != RDepth) |
0 |
645 |
if (LDepth != RDepth) |
0 |
| 646 |
return (int)LDepth - (int)RDepth; |
0 |
646 |
return (int)LDepth - (int)RDepth; |
0 |
| 647 |
} |
--- |
647 |
} |
--- |
| 648 |
|
--- |
648 |
|
--- |
| 649 |
// Compare the number of operands. |
--- |
649 |
// Compare the number of operands. |
--- |
| 650 |
unsigned LNumOps = LInst->getNumOperands(), |
0 |
650 |
unsigned LNumOps = LInst->getNumOperands(), |
0 |
| 651 |
RNumOps = RInst->getNumOperands(); |
0 |
651 |
RNumOps = RInst->getNumOperands(); |
0 |
| 652 |
if (LNumOps != RNumOps) |
0 |
652 |
if (LNumOps != RNumOps) |
0 |
| 653 |
return (int)LNumOps - (int)RNumOps; |
0 |
653 |
return (int)LNumOps - (int)RNumOps; |
0 |
| 654 |
|
--- |
654 |
|
--- |
| 655 |
for (unsigned Idx : seq(LNumOps)) { |
0 |
655 |
for (unsigned Idx : seq(LNumOps)) { |
0 |
| 656 |
int Result = |
--- |
656 |
int Result = |
--- |
| 657 |
CompareValueComplexity(EqCacheValue, LI, LInst->getOperand(Idx), |
0 |
657 |
CompareValueComplexity(EqCacheValue, LI, LInst->getOperand(Idx), |
0 |
| 658 |
RInst->getOperand(Idx), Depth + 1); |
--- |
658 |
RInst->getOperand(Idx), Depth + 1); |
--- |
| 659 |
if (Result != 0) |
0 |
659 |
if (Result != 0) |
0 |
| 660 |
return Result; |
0 |
660 |
return Result; |
0 |
| 661 |
} |
--- |
661 |
} |
--- |
| 662 |
} |
--- |
662 |
} |
--- |
| 663 |
|
--- |
663 |
|
--- |
| 664 |
EqCacheValue.unionSets(LV, RV); |
0 |
664 |
EqCacheValue.unionSets(LV, RV); |
0 |
| 665 |
return 0; |
0 |
665 |
return 0; |
0 |
| 666 |
} |
--- |
666 |
} |
--- |
| 667 |
|
--- |
667 |
|
--- |
| 668 |
// Return negative, zero, or positive, if LHS is less than, equal to, or greater |
--- |
668 |
// Return negative, zero, or positive, if LHS is less than, equal to, or greater |
--- |
| 669 |
// than RHS, respectively. A three-way result allows recursive comparisons to be |
--- |
669 |
// than RHS, respectively. A three-way result allows recursive comparisons to be |
--- |
| 670 |
// more efficient. |
--- |
670 |
// more efficient. |
--- |
| 671 |
// If the max analysis depth was reached, return std::nullopt, assuming we do |
--- |
671 |
// If the max analysis depth was reached, return std::nullopt, assuming we do |
--- |
| 672 |
// not know if they are equivalent for sure. |
--- |
672 |
// not know if they are equivalent for sure. |
--- |
| 673 |
static std::optional |
--- |
673 |
static std::optional |
--- |
| 674 |
CompareSCEVComplexity(EquivalenceClasses &EqCacheSCEV, |
0 |
674 |
CompareSCEVComplexity(EquivalenceClasses &EqCacheSCEV, |
0 |
| 675 |
EquivalenceClasses &EqCacheValue, |
--- |
675 |
EquivalenceClasses &EqCacheValue, |
--- |
| 676 |
const LoopInfo *const LI, const SCEV *LHS, |
--- |
676 |
const LoopInfo *const LI, const SCEV *LHS, |
--- |
| 677 |
const SCEV *RHS, DominatorTree &DT, unsigned Depth = 0) { |
--- |
677 |
const SCEV *RHS, DominatorTree &DT, unsigned Depth = 0) { |
--- |
| 678 |
// Fast-path: SCEVs are uniqued so we can do a quick equality check. |
--- |
678 |
// Fast-path: SCEVs are uniqued so we can do a quick equality check. |
--- |
| 679 |
if (LHS == RHS) |
0 |
679 |
if (LHS == RHS) |
0 |
| 680 |
return 0; |
0 |
680 |
return 0; |
0 |
| 681 |
|
--- |
681 |
|
--- |
| 682 |
// Primarily, sort the SCEVs by their getSCEVType(). |
--- |
682 |
// Primarily, sort the SCEVs by their getSCEVType(). |
--- |
| 683 |
SCEVTypes LType = LHS->getSCEVType(), RType = RHS->getSCEVType(); |
0 |
683 |
SCEVTypes LType = LHS->getSCEVType(), RType = RHS->getSCEVType(); |
0 |
| 684 |
if (LType != RType) |
0 |
684 |
if (LType != RType) |
0 |
| 685 |
return (int)LType - (int)RType; |
0 |
685 |
return (int)LType - (int)RType; |
0 |
| 686 |
|
--- |
686 |
|
--- |
| 687 |
if (EqCacheSCEV.isEquivalent(LHS, RHS)) |
0 |
687 |
if (EqCacheSCEV.isEquivalent(LHS, RHS)) |
0 |
| 688 |
return 0; |
0 |
688 |
return 0; |
0 |
| 689 |
|
--- |
689 |
|
--- |
| 690 |
if (Depth > MaxSCEVCompareDepth) |
0 |
690 |
if (Depth > MaxSCEVCompareDepth) |
0 |
| 691 |
return std::nullopt; |
0 |
691 |
return std::nullopt; |
0 |
| 692 |
|
--- |
692 |
|
--- |
| 693 |
// Aside from the getSCEVType() ordering, the particular ordering |
--- |
693 |
// Aside from the getSCEVType() ordering, the particular ordering |
--- |
| 694 |
// isn't very important except that it's beneficial to be consistent, |
--- |
694 |
// isn't very important except that it's beneficial to be consistent, |
--- |
| 695 |
// so that (a + b) and (b + a) don't end up as different expressions. |
--- |
695 |
// so that (a + b) and (b + a) don't end up as different expressions. |
--- |
| 696 |
switch (LType) { |
0 |
696 |
switch (LType) { |
0 |
| 697 |
case scUnknown: { |
0 |
697 |
case scUnknown: { |
0 |
| 698 |
const SCEVUnknown *LU = cast(LHS); |
0 |
698 |
const SCEVUnknown *LU = cast(LHS); |
0 |
| 699 |
const SCEVUnknown *RU = cast(RHS); |
0 |
699 |
const SCEVUnknown *RU = cast(RHS); |
0 |
| 700 |
|
--- |
700 |
|
--- |
| 701 |
int X = CompareValueComplexity(EqCacheValue, LI, LU->getValue(), |
0 |
701 |
int X = CompareValueComplexity(EqCacheValue, LI, LU->getValue(), |
0 |
| 702 |
RU->getValue(), Depth + 1); |
0 |
702 |
RU->getValue(), Depth + 1); |
0 |
| 703 |
if (X == 0) |
0 |
703 |
if (X == 0) |
0 |
| 704 |
EqCacheSCEV.unionSets(LHS, RHS); |
0 |
704 |
EqCacheSCEV.unionSets(LHS, RHS); |
0 |
| 705 |
return X; |
0 |
705 |
return X; |
0 |
| 706 |
} |
--- |
706 |
} |
--- |
| 707 |
|
--- |
707 |
|
--- |
| 708 |
case scConstant: { |
0 |
708 |
case scConstant: { |
0 |
| 709 |
const SCEVConstant *LC = cast(LHS); |
0 |
709 |
const SCEVConstant *LC = cast(LHS); |
0 |
| 710 |
const SCEVConstant *RC = cast(RHS); |
0 |
710 |
const SCEVConstant *RC = cast(RHS); |
0 |
| 711 |
|
--- |
711 |
|
--- |
| 712 |
// Compare constant values. |
--- |
712 |
// Compare constant values. |
--- |
| 713 |
const APInt &LA = LC->getAPInt(); |
0 |
713 |
const APInt &LA = LC->getAPInt(); |
0 |
| 714 |
const APInt &RA = RC->getAPInt(); |
0 |
714 |
const APInt &RA = RC->getAPInt(); |
0 |
| 715 |
unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth(); |
0 |
715 |
unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth(); |
0 |
| 716 |
if (LBitWidth != RBitWidth) |
0 |
716 |
if (LBitWidth != RBitWidth) |
0 |
| 717 |
return (int)LBitWidth - (int)RBitWidth; |
0 |
717 |
return (int)LBitWidth - (int)RBitWidth; |
0 |
| 718 |
return LA.ult(RA) ? -1 : 1; |
0 |
718 |
return LA.ult(RA) ? -1 : 1; |
0 |
| 719 |
} |
--- |
719 |
} |
--- |
| 720 |
|
--- |
720 |
|
--- |
| 721 |
case scVScale: { |
0 |
721 |
case scVScale: { |
0 |
| 722 |
const auto *LTy = cast(cast(LHS)->getType()); |
0 |
722 |
const auto *LTy = cast(cast(LHS)->getType()); |
0 |
| 723 |
const auto *RTy = cast(cast(RHS)->getType()); |
0 |
723 |
const auto *RTy = cast(cast(RHS)->getType()); |
0 |
| 724 |
return LTy->getBitWidth() - RTy->getBitWidth(); |
0 |
724 |
return LTy->getBitWidth() - RTy->getBitWidth(); |
0 |
| 725 |
} |
--- |
725 |
} |
--- |
| 726 |
|
--- |
726 |
|
--- |
| 727 |
case scAddRecExpr: { |
0 |
727 |
case scAddRecExpr: { |
0 |
| 728 |
const SCEVAddRecExpr *LA = cast(LHS); |
0 |
728 |
const SCEVAddRecExpr *LA = cast(LHS); |
0 |
| 729 |
const SCEVAddRecExpr *RA = cast(RHS); |
0 |
729 |
const SCEVAddRecExpr *RA = cast(RHS); |
0 |
| 730 |
|
--- |
730 |
|
--- |
| 731 |
// There is always a dominance between two recs that are used by one SCEV, |
--- |
731 |
// There is always a dominance between two recs that are used by one SCEV, |
--- |
| 732 |
// so we can safely sort recs by loop header dominance. We require such |
--- |
732 |
// so we can safely sort recs by loop header dominance. We require such |
--- |
| 733 |
// order in getAddExpr. |
--- |
733 |
// order in getAddExpr. |
--- |
| 734 |
const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop(); |
0 |
734 |
const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop(); |
0 |
| 735 |
if (LLoop != RLoop) { |
0 |
735 |
if (LLoop != RLoop) { |
0 |
| 736 |
const BasicBlock *LHead = LLoop->getHeader(), *RHead = RLoop->getHeader(); |
0 |
736 |
const BasicBlock *LHead = LLoop->getHeader(), *RHead = RLoop->getHeader(); |
0 |
| 737 |
assert(LHead != RHead && "Two loops share the same header?"); |
0 |
737 |
assert(LHead != RHead && "Two loops share the same header?"); |
0 |
| 738 |
if (DT.dominates(LHead, RHead)) |
0 |
738 |
if (DT.dominates(LHead, RHead)) |
0 |
| 739 |
return 1; |
0 |
739 |
return 1; |
0 |
| 740 |
assert(DT.dominates(RHead, LHead) && |
0 |
740 |
assert(DT.dominates(RHead, LHead) && |
0 |
| 741 |
"No dominance between recurrences used by one SCEV?"); |
--- |
741 |
"No dominance between recurrences used by one SCEV?"); |
--- |
| 742 |
return -1; |
0 |
742 |
return -1; |
0 |
| 743 |
} |
--- |
743 |
} |
--- |
| 744 |
|
--- |
744 |
|
--- |
| 745 |
[[fallthrough]]; |
--- |
745 |
[[fallthrough]]; |
--- |
| 746 |
} |
--- |
746 |
} |
--- |
| 747 |
|
--- |
747 |
|
--- |
| 748 |
case scTruncate: |
--- |
748 |
case scTruncate: |
--- |
| 749 |
case scZeroExtend: |
--- |
749 |
case scZeroExtend: |
--- |
| 750 |
case scSignExtend: |
--- |
750 |
case scSignExtend: |
--- |
| 751 |
case scPtrToInt: |
--- |
751 |
case scPtrToInt: |
--- |
| 752 |
case scAddExpr: |
--- |
752 |
case scAddExpr: |
--- |
| 753 |
case scMulExpr: |
--- |
753 |
case scMulExpr: |
--- |
| 754 |
case scUDivExpr: |
--- |
754 |
case scUDivExpr: |
--- |
| 755 |
case scSMaxExpr: |
--- |
755 |
case scSMaxExpr: |
--- |
| 756 |
case scUMaxExpr: |
--- |
756 |
case scUMaxExpr: |
--- |
| 757 |
case scSMinExpr: |
--- |
757 |
case scSMinExpr: |
--- |
| 758 |
case scUMinExpr: |
--- |
758 |
case scUMinExpr: |
--- |
| 759 |
case scSequentialUMinExpr: { |
--- |
759 |
case scSequentialUMinExpr: { |
--- |
| 760 |
ArrayRef LOps = LHS->operands(); |
0 |
760 |
ArrayRef LOps = LHS->operands(); |
0 |
| 761 |
ArrayRef ROps = RHS->operands(); |
0 |
761 |
ArrayRef ROps = RHS->operands(); |
0 |
| 762 |
|
--- |
762 |
|
--- |
| 763 |
// Lexicographically compare n-ary-like expressions. |
--- |
763 |
// Lexicographically compare n-ary-like expressions. |
--- |
| 764 |
unsigned LNumOps = LOps.size(), RNumOps = ROps.size(); |
0 |
764 |
unsigned LNumOps = LOps.size(), RNumOps = ROps.size(); |
0 |
| 765 |
if (LNumOps != RNumOps) |
0 |
765 |
if (LNumOps != RNumOps) |
0 |
| 766 |
return (int)LNumOps - (int)RNumOps; |
0 |
766 |
return (int)LNumOps - (int)RNumOps; |
0 |
| 767 |
|
--- |
767 |
|
--- |
| 768 |
for (unsigned i = 0; i != LNumOps; ++i) { |
0 |
768 |
for (unsigned i = 0; i != LNumOps; ++i) { |
0 |
| 769 |
auto X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LOps[i], |
0 |
769 |
auto X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LOps[i], |
0 |
| 770 |
ROps[i], DT, Depth + 1); |
0 |
770 |
ROps[i], DT, Depth + 1); |
0 |
| 771 |
if (X != 0) |
0 |
771 |
if (X != 0) |
0 |
| 772 |
return X; |
0 |
772 |
return X; |
0 |
| 773 |
} |
--- |
773 |
} |
--- |
| 774 |
EqCacheSCEV.unionSets(LHS, RHS); |
0 |
774 |
EqCacheSCEV.unionSets(LHS, RHS); |
0 |
| 775 |
return 0; |
0 |
775 |
return 0; |
0 |
| 776 |
} |
--- |
776 |
} |
--- |
| 777 |
|
--- |
777 |
|
--- |
| 778 |
case scCouldNotCompute: |
0 |
778 |
case scCouldNotCompute: |
0 |
| 779 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
779 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
| 780 |
} |
--- |
780 |
} |
--- |
| 781 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
781 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
| 782 |
} |
--- |
782 |
} |
--- |
| 783 |
|
--- |
783 |
|
--- |
| 784 |
/// Given a list of SCEV objects, order them by their complexity, and group |
--- |
784 |
/// Given a list of SCEV objects, order them by their complexity, and group |
--- |
| 785 |
/// objects of the same complexity together by value. When this routine is |
--- |
785 |
/// objects of the same complexity together by value. When this routine is |
--- |
| 786 |
/// finished, we know that any duplicates in the vector are consecutive and that |
--- |
786 |
/// finished, we know that any duplicates in the vector are consecutive and that |
--- |
| 787 |
/// complexity is monotonically increasing. |
--- |
787 |
/// complexity is monotonically increasing. |
--- |
| 788 |
/// |
--- |
788 |
/// |
--- |
| 789 |
/// Note that we go take special precautions to ensure that we get deterministic |
--- |
789 |
/// Note that we go take special precautions to ensure that we get deterministic |
--- |
| 790 |
/// results from this routine. In other words, we don't want the results of |
--- |
790 |
/// results from this routine. In other words, we don't want the results of |
--- |
| 791 |
/// this to depend on where the addresses of various SCEV objects happened to |
--- |
791 |
/// this to depend on where the addresses of various SCEV objects happened to |
--- |
| 792 |
/// land in memory. |
--- |
792 |
/// land in memory. |
--- |
| 793 |
static void GroupByComplexity(SmallVectorImpl &Ops, |
0 |
793 |
static void GroupByComplexity(SmallVectorImpl &Ops, |
0 |
| 794 |
LoopInfo *LI, DominatorTree &DT) { |
--- |
794 |
LoopInfo *LI, DominatorTree &DT) { |
--- |
| 795 |
if (Ops.size() < 2) return; // Noop |
0 |
795 |
if (Ops.size() < 2) return; // Noop |
0 |
| 796 |
|
--- |
796 |
|
--- |
| 797 |
EquivalenceClasses EqCacheSCEV; |
0 |
797 |
EquivalenceClasses EqCacheSCEV; |
0 |
| 798 |
EquivalenceClasses EqCacheValue; |
0 |
798 |
EquivalenceClasses EqCacheValue; |
0 |
| 799 |
|
--- |
799 |
|
--- |
| 800 |
// Whether LHS has provably less complexity than RHS. |
--- |
800 |
// Whether LHS has provably less complexity than RHS. |
--- |
| 801 |
auto IsLessComplex = [&](const SCEV *LHS, const SCEV *RHS) { |
0 |
801 |
auto IsLessComplex = [&](const SCEV *LHS, const SCEV *RHS) { |
0 |
| 802 |
auto Complexity = |
--- |
802 |
auto Complexity = |
--- |
| 803 |
CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LHS, RHS, DT); |
0 |
803 |
CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LHS, RHS, DT); |
0 |
| 804 |
return Complexity && *Complexity < 0; |
0 |
804 |
return Complexity && *Complexity < 0; |
0 |
| 805 |
}; |
0 |
805 |
}; |
0 |
| 806 |
if (Ops.size() == 2) { |
0 |
806 |
if (Ops.size() == 2) { |
0 |
| 807 |
// This is the common case, which also happens to be trivially simple. |
--- |
807 |
// This is the common case, which also happens to be trivially simple. |
--- |
| 808 |
// Special case it. |
--- |
808 |
// Special case it. |
--- |
| 809 |
const SCEV *&LHS = Ops[0], *&RHS = Ops[1]; |
0 |
809 |
const SCEV *&LHS = Ops[0], *&RHS = Ops[1]; |
0 |
| 810 |
if (IsLessComplex(RHS, LHS)) |
0 |
810 |
if (IsLessComplex(RHS, LHS)) |
0 |
| 811 |
std::swap(LHS, RHS); |
0 |
811 |
std::swap(LHS, RHS); |
0 |
| 812 |
return; |
0 |
812 |
return; |
0 |
| 813 |
} |
--- |
813 |
} |
--- |
| 814 |
|
--- |
814 |
|
--- |
| 815 |
// Do the rough sort by complexity. |
--- |
815 |
// Do the rough sort by complexity. |
--- |
| 816 |
llvm::stable_sort(Ops, [&](const SCEV *LHS, const SCEV *RHS) { |
0 |
816 |
llvm::stable_sort(Ops, [&](const SCEV *LHS, const SCEV *RHS) { |
0 |
| 817 |
return IsLessComplex(LHS, RHS); |
0 |
817 |
return IsLessComplex(LHS, RHS); |
0 |
| 818 |
}); |
--- |
818 |
}); |
--- |
| 819 |
|
--- |
819 |
|
--- |
| 820 |
// Now that we are sorted by complexity, group elements of the same |
--- |
820 |
// Now that we are sorted by complexity, group elements of the same |
--- |
| 821 |
// complexity. Note that this is, at worst, N^2, but the vector is likely to |
--- |
821 |
// complexity. Note that this is, at worst, N^2, but the vector is likely to |
--- |
| 822 |
// be extremely short in practice. Note that we take this approach because we |
--- |
822 |
// be extremely short in practice. Note that we take this approach because we |
--- |
| 823 |
// do not want to depend on the addresses of the objects we are grouping. |
--- |
823 |
// do not want to depend on the addresses of the objects we are grouping. |
--- |
| 824 |
for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { |
0 |
824 |
for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { |
0 |
| 825 |
const SCEV *S = Ops[i]; |
0 |
825 |
const SCEV *S = Ops[i]; |
0 |
| 826 |
unsigned Complexity = S->getSCEVType(); |
0 |
826 |
unsigned Complexity = S->getSCEVType(); |
0 |
| 827 |
|
--- |
827 |
|
--- |
| 828 |
// If there are any objects of the same complexity and same value as this |
--- |
828 |
// If there are any objects of the same complexity and same value as this |
--- |
| 829 |
// one, group them. |
--- |
829 |
// one, group them. |
--- |
| 830 |
for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { |
0 |
830 |
for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { |
0 |
| 831 |
if (Ops[j] == S) { // Found a duplicate. |
0 |
831 |
if (Ops[j] == S) { // Found a duplicate. |
0 |
| 832 |
// Move it to immediately after i'th element. |
--- |
832 |
// Move it to immediately after i'th element. |
--- |
| 833 |
std::swap(Ops[i+1], Ops[j]); |
0 |
833 |
std::swap(Ops[i+1], Ops[j]); |
0 |
| 834 |
++i; // no need to rescan it. |
0 |
834 |
++i; // no need to rescan it. |
0 |
| 835 |
if (i == e-2) return; // Done! |
0 |
835 |
if (i == e-2) return; // Done! |
0 |
| 836 |
} |
--- |
836 |
} |
--- |
| 837 |
} |
--- |
837 |
} |
--- |
| 838 |
} |
--- |
838 |
} |
--- |
| 839 |
} |
0 |
839 |
} |
0 |
| 840 |
|
--- |
840 |
|
--- |
| 841 |
/// Returns true if \p Ops contains a huge SCEV (the subtree of S contains at |
--- |
841 |
/// Returns true if \p Ops contains a huge SCEV (the subtree of S contains at |
--- |
| 842 |
/// least HugeExprThreshold nodes). |
--- |
842 |
/// least HugeExprThreshold nodes). |
--- |
| 843 |
static bool hasHugeExpression(ArrayRef Ops) { |
0 |
843 |
static bool hasHugeExpression(ArrayRef Ops) { |
0 |
| 844 |
return any_of(Ops, [](const SCEV *S) { |
0 |
844 |
return any_of(Ops, [](const SCEV *S) { |
0 |
| 845 |
return S->getExpressionSize() >= HugeExprThreshold; |
0 |
845 |
return S->getExpressionSize() >= HugeExprThreshold; |
0 |
| 846 |
}); |
0 |
846 |
}); |
0 |
| 847 |
} |
--- |
847 |
} |
--- |
| 848 |
|
--- |
848 |
|
--- |
| 849 |
//===----------------------------------------------------------------------===// |
--- |
849 |
//===----------------------------------------------------------------------===// |
--- |
| 850 |
// Simple SCEV method implementations |
--- |
850 |
// Simple SCEV method implementations |
--- |
| 851 |
//===----------------------------------------------------------------------===// |
--- |
851 |
//===----------------------------------------------------------------------===// |
--- |
| 852 |
|
--- |
852 |
|
--- |
| 853 |
/// Compute BC(It, K). The result has width W. Assume, K > 0. |
--- |
853 |
/// Compute BC(It, K). The result has width W. Assume, K > 0. |
--- |
| 854 |
static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K, |
0 |
854 |
static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K, |
0 |
| 855 |
ScalarEvolution &SE, |
--- |
855 |
ScalarEvolution &SE, |
--- |
| 856 |
Type *ResultTy) { |
--- |
856 |
Type *ResultTy) { |
--- |
| 857 |
// Handle the simplest case efficiently. |
--- |
857 |
// Handle the simplest case efficiently. |
--- |
| 858 |
if (K == 1) |
0 |
858 |
if (K == 1) |
0 |
| 859 |
return SE.getTruncateOrZeroExtend(It, ResultTy); |
0 |
859 |
return SE.getTruncateOrZeroExtend(It, ResultTy); |
0 |
| 860 |
|
--- |
860 |
|
--- |
| 861 |
// We are using the following formula for BC(It, K): |
--- |
861 |
// We are using the following formula for BC(It, K): |
--- |
| 862 |
// |
--- |
862 |
// |
--- |
| 863 |
// BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! |
--- |
863 |
// BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! |
--- |
| 864 |
// |
--- |
864 |
// |
--- |
| 865 |
// Suppose, W is the bitwidth of the return value. We must be prepared for |
--- |
865 |
// Suppose, W is the bitwidth of the return value. We must be prepared for |
--- |
| 866 |
// overflow. Hence, we must assure that the result of our computation is |
--- |
866 |
// overflow. Hence, we must assure that the result of our computation is |
--- |
| 867 |
// equal to the accurate one modulo 2^W. Unfortunately, division isn't |
--- |
867 |
// equal to the accurate one modulo 2^W. Unfortunately, division isn't |
--- |
| 868 |
// safe in modular arithmetic. |
--- |
868 |
// safe in modular arithmetic. |
--- |
| 869 |
// |
--- |
869 |
// |
--- |
| 870 |
// However, this code doesn't use exactly that formula; the formula it uses |
--- |
870 |
// However, this code doesn't use exactly that formula; the formula it uses |
--- |
| 871 |
// is something like the following, where T is the number of factors of 2 in |
--- |
871 |
// is something like the following, where T is the number of factors of 2 in |
--- |
| 872 |
// K! (i.e. trailing zeros in the binary representation of K!), and ^ is |
--- |
872 |
// K! (i.e. trailing zeros in the binary representation of K!), and ^ is |
--- |
| 873 |
// exponentiation: |
--- |
873 |
// exponentiation: |
--- |
| 874 |
// |
--- |
874 |
// |
--- |
| 875 |
// BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T) |
--- |
875 |
// BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T) |
--- |
| 876 |
// |
--- |
876 |
// |
--- |
| 877 |
// This formula is trivially equivalent to the previous formula. However, |
--- |
877 |
// This formula is trivially equivalent to the previous formula. However, |
--- |
| 878 |
// this formula can be implemented much more efficiently. The trick is that |
--- |
878 |
// this formula can be implemented much more efficiently. The trick is that |
--- |
| 879 |
// K! / 2^T is odd, and exact division by an odd number *is* safe in modular |
--- |
879 |
// K! / 2^T is odd, and exact division by an odd number *is* safe in modular |
--- |
| 880 |
// arithmetic. To do exact division in modular arithmetic, all we have |
--- |
880 |
// arithmetic. To do exact division in modular arithmetic, all we have |
--- |
| 881 |
// to do is multiply by the inverse. Therefore, this step can be done at |
--- |
881 |
// to do is multiply by the inverse. Therefore, this step can be done at |
--- |
| 882 |
// width W. |
--- |
882 |
// width W. |
--- |
| 883 |
// |
--- |
883 |
// |
--- |
| 884 |
// The next issue is how to safely do the division by 2^T. The way this |
--- |
884 |
// The next issue is how to safely do the division by 2^T. The way this |
--- |
| 885 |
// is done is by doing the multiplication step at a width of at least W + T |
--- |
885 |
// is done is by doing the multiplication step at a width of at least W + T |
--- |
| 886 |
// bits. This way, the bottom W+T bits of the product are accurate. Then, |
--- |
886 |
// bits. This way, the bottom W+T bits of the product are accurate. Then, |
--- |
| 887 |
// when we perform the division by 2^T (which is equivalent to a right shift |
--- |
887 |
// when we perform the division by 2^T (which is equivalent to a right shift |
--- |
| 888 |
// by T), the bottom W bits are accurate. Extra bits are okay; they'll get |
--- |
888 |
// by T), the bottom W bits are accurate. Extra bits are okay; they'll get |
--- |
| 889 |
// truncated out after the division by 2^T. |
--- |
889 |
// truncated out after the division by 2^T. |
--- |
| 890 |
// |
--- |
890 |
// |
--- |
| 891 |
// In comparison to just directly using the first formula, this technique |
--- |
891 |
// In comparison to just directly using the first formula, this technique |
--- |
| 892 |
// is much more efficient; using the first formula requires W * K bits, |
--- |
892 |
// is much more efficient; using the first formula requires W * K bits, |
--- |
| 893 |
// but this formula less than W + K bits. Also, the first formula requires |
--- |
893 |
// but this formula less than W + K bits. Also, the first formula requires |
--- |
| 894 |
// a division step, whereas this formula only requires multiplies and shifts. |
--- |
894 |
// a division step, whereas this formula only requires multiplies and shifts. |
--- |
| 895 |
// |
--- |
895 |
// |
--- |
| 896 |
// It doesn't matter whether the subtraction step is done in the calculation |
--- |
896 |
// It doesn't matter whether the subtraction step is done in the calculation |
--- |
| 897 |
// width or the input iteration count's width; if the subtraction overflows, |
--- |
897 |
// width or the input iteration count's width; if the subtraction overflows, |
--- |
| 898 |
// the result must be zero anyway. We prefer here to do it in the width of |
--- |
898 |
// the result must be zero anyway. We prefer here to do it in the width of |
--- |
| 899 |
// the induction variable because it helps a lot for certain cases; CodeGen |
--- |
899 |
// the induction variable because it helps a lot for certain cases; CodeGen |
--- |
| 900 |
// isn't smart enough to ignore the overflow, which leads to much less |
--- |
900 |
// isn't smart enough to ignore the overflow, which leads to much less |
--- |
| 901 |
// efficient code if the width of the subtraction is wider than the native |
--- |
901 |
// efficient code if the width of the subtraction is wider than the native |
--- |
| 902 |
// register width. |
--- |
902 |
// register width. |
--- |
| 903 |
// |
--- |
903 |
// |
--- |
| 904 |
// (It's possible to not widen at all by pulling out factors of 2 before |
--- |
904 |
// (It's possible to not widen at all by pulling out factors of 2 before |
--- |
| 905 |
// the multiplication; for example, K=2 can be calculated as |
--- |
905 |
// the multiplication; for example, K=2 can be calculated as |
--- |
| 906 |
// It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires |
--- |
906 |
// It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires |
--- |
| 907 |
// extra arithmetic, so it's not an obvious win, and it gets |
--- |
907 |
// extra arithmetic, so it's not an obvious win, and it gets |
--- |
| 908 |
// much more complicated for K > 3.) |
--- |
908 |
// much more complicated for K > 3.) |
--- |
| 909 |
|
--- |
909 |
|
--- |
| 910 |
// Protection from insane SCEVs; this bound is conservative, |
--- |
910 |
// Protection from insane SCEVs; this bound is conservative, |
--- |
| 911 |
// but it probably doesn't matter. |
--- |
911 |
// but it probably doesn't matter. |
--- |
| 912 |
if (K > 1000) |
0 |
912 |
if (K > 1000) |
0 |
| 913 |
return SE.getCouldNotCompute(); |
0 |
913 |
return SE.getCouldNotCompute(); |
0 |
| 914 |
|
--- |
914 |
|
--- |
| 915 |
unsigned W = SE.getTypeSizeInBits(ResultTy); |
0 |
915 |
unsigned W = SE.getTypeSizeInBits(ResultTy); |
0 |
| 916 |
|
--- |
916 |
|
--- |
| 917 |
// Calculate K! / 2^T and T; we divide out the factors of two before |
--- |
917 |
// Calculate K! / 2^T and T; we divide out the factors of two before |
--- |
| 918 |
// multiplying for calculating K! / 2^T to avoid overflow. |
--- |
918 |
// multiplying for calculating K! / 2^T to avoid overflow. |
--- |
| 919 |
// Other overflow doesn't matter because we only care about the bottom |
--- |
919 |
// Other overflow doesn't matter because we only care about the bottom |
--- |
| 920 |
// W bits of the result. |
--- |
920 |
// W bits of the result. |
--- |
| 921 |
APInt OddFactorial(W, 1); |
0 |
921 |
APInt OddFactorial(W, 1); |
0 |
| 922 |
unsigned T = 1; |
0 |
922 |
unsigned T = 1; |
0 |
| 923 |
for (unsigned i = 3; i <= K; ++i) { |
0 |
923 |
for (unsigned i = 3; i <= K; ++i) { |
0 |
| 924 |
APInt Mult(W, i); |
0 |
924 |
APInt Mult(W, i); |
0 |
| 925 |
unsigned TwoFactors = Mult.countr_zero(); |
0 |
925 |
unsigned TwoFactors = Mult.countr_zero(); |
0 |
| 926 |
T += TwoFactors; |
0 |
926 |
T += TwoFactors; |
0 |
| 927 |
Mult.lshrInPlace(TwoFactors); |
0 |
927 |
Mult.lshrInPlace(TwoFactors); |
0 |
| 928 |
OddFactorial *= Mult; |
0 |
928 |
OddFactorial *= Mult; |
0 |
| 929 |
} |
0 |
929 |
} |
0 |
| 930 |
|
--- |
930 |
|
--- |
| 931 |
// We need at least W + T bits for the multiplication step |
--- |
931 |
// We need at least W + T bits for the multiplication step |
--- |
| 932 |
unsigned CalculationBits = W + T; |
0 |
932 |
unsigned CalculationBits = W + T; |
0 |
| 933 |
|
--- |
933 |
|
--- |
| 934 |
// Calculate 2^T, at width T+W. |
--- |
934 |
// Calculate 2^T, at width T+W. |
--- |
| 935 |
APInt DivFactor = APInt::getOneBitSet(CalculationBits, T); |
0 |
935 |
APInt DivFactor = APInt::getOneBitSet(CalculationBits, T); |
0 |
| 936 |
|
--- |
936 |
|
--- |
| 937 |
// Calculate the multiplicative inverse of K! / 2^T; |
--- |
937 |
// Calculate the multiplicative inverse of K! / 2^T; |
--- |
| 938 |
// this multiplication factor will perform the exact division by |
--- |
938 |
// this multiplication factor will perform the exact division by |
--- |
| 939 |
// K! / 2^T. |
--- |
939 |
// K! / 2^T. |
--- |
| 940 |
APInt Mod = APInt::getSignedMinValue(W+1); |
0 |
940 |
APInt Mod = APInt::getSignedMinValue(W+1); |
0 |
| 941 |
APInt MultiplyFactor = OddFactorial.zext(W+1); |
0 |
941 |
APInt MultiplyFactor = OddFactorial.zext(W+1); |
0 |
| 942 |
MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); |
0 |
942 |
MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); |
0 |
| 943 |
MultiplyFactor = MultiplyFactor.trunc(W); |
0 |
943 |
MultiplyFactor = MultiplyFactor.trunc(W); |
0 |
| 944 |
|
--- |
944 |
|
--- |
| 945 |
// Calculate the product, at width T+W |
--- |
945 |
// Calculate the product, at width T+W |
--- |
| 946 |
IntegerType *CalculationTy = IntegerType::get(SE.getContext(), |
0 |
946 |
IntegerType *CalculationTy = IntegerType::get(SE.getContext(), |
0 |
| 947 |
CalculationBits); |
--- |
947 |
CalculationBits); |
--- |
| 948 |
const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); |
0 |
948 |
const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); |
0 |
| 949 |
for (unsigned i = 1; i != K; ++i) { |
0 |
949 |
for (unsigned i = 1; i != K; ++i) { |
0 |
| 950 |
const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i)); |
0 |
950 |
const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i)); |
0 |
| 951 |
Dividend = SE.getMulExpr(Dividend, |
0 |
951 |
Dividend = SE.getMulExpr(Dividend, |
0 |
| 952 |
SE.getTruncateOrZeroExtend(S, CalculationTy)); |
--- |
952 |
SE.getTruncateOrZeroExtend(S, CalculationTy)); |
--- |
| 953 |
} |
--- |
953 |
} |
--- |
| 954 |
|
--- |
954 |
|
--- |
| 955 |
// Divide by 2^T |
--- |
955 |
// Divide by 2^T |
--- |
| 956 |
const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); |
0 |
956 |
const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); |
0 |
| 957 |
|
--- |
957 |
|
--- |
| 958 |
// Truncate the result, and divide by K! / 2^T. |
--- |
958 |
// Truncate the result, and divide by K! / 2^T. |
--- |
| 959 |
|
--- |
959 |
|
--- |
| 960 |
return SE.getMulExpr(SE.getConstant(MultiplyFactor), |
0 |
960 |
return SE.getMulExpr(SE.getConstant(MultiplyFactor), |
0 |
| 961 |
SE.getTruncateOrZeroExtend(DivResult, ResultTy)); |
0 |
961 |
SE.getTruncateOrZeroExtend(DivResult, ResultTy)); |
0 |
| 962 |
} |
0 |
962 |
} |
0 |
| 963 |
|
--- |
963 |
|
--- |
| 964 |
/// Return the value of this chain of recurrences at the specified iteration |
--- |
964 |
/// Return the value of this chain of recurrences at the specified iteration |
--- |
| 965 |
/// number. We can evaluate this recurrence by multiplying each element in the |
--- |
965 |
/// number. We can evaluate this recurrence by multiplying each element in the |
--- |
| 966 |
/// chain by the binomial coefficient corresponding to it. In other words, we |
--- |
966 |
/// chain by the binomial coefficient corresponding to it. In other words, we |
--- |
| 967 |
/// can evaluate {A,+,B,+,C,+,D} as: |
--- |
967 |
/// can evaluate {A,+,B,+,C,+,D} as: |
--- |
| 968 |
/// |
--- |
968 |
/// |
--- |
| 969 |
/// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3) |
--- |
969 |
/// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3) |
--- |
| 970 |
/// |
--- |
970 |
/// |
--- |
| 971 |
/// where BC(It, k) stands for binomial coefficient. |
--- |
971 |
/// where BC(It, k) stands for binomial coefficient. |
--- |
| 972 |
const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It, |
0 |
972 |
const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It, |
0 |
| 973 |
ScalarEvolution &SE) const { |
--- |
973 |
ScalarEvolution &SE) const { |
--- |
| 974 |
return evaluateAtIteration(operands(), It, SE); |
0 |
974 |
return evaluateAtIteration(operands(), It, SE); |
0 |
| 975 |
} |
--- |
975 |
} |
--- |
| 976 |
|
--- |
976 |
|
--- |
| 977 |
const SCEV * |
--- |
977 |
const SCEV * |
--- |
| 978 |
SCEVAddRecExpr::evaluateAtIteration(ArrayRef Operands, |
0 |
978 |
SCEVAddRecExpr::evaluateAtIteration(ArrayRef Operands, |
0 |
| 979 |
const SCEV *It, ScalarEvolution &SE) { |
--- |
979 |
const SCEV *It, ScalarEvolution &SE) { |
--- |
| 980 |
assert(Operands.size() > 0); |
0 |
980 |
assert(Operands.size() > 0); |
0 |
| 981 |
const SCEV *Result = Operands[0]; |
0 |
981 |
const SCEV *Result = Operands[0]; |
0 |
| 982 |
for (unsigned i = 1, e = Operands.size(); i != e; ++i) { |
0 |
982 |
for (unsigned i = 1, e = Operands.size(); i != e; ++i) { |
0 |
| 983 |
// The computation is correct in the face of overflow provided that the |
--- |
983 |
// The computation is correct in the face of overflow provided that the |
--- |
| 984 |
// multiplication is performed _after_ the evaluation of the binomial |
--- |
984 |
// multiplication is performed _after_ the evaluation of the binomial |
--- |
| 985 |
// coefficient. |
--- |
985 |
// coefficient. |
--- |
| 986 |
const SCEV *Coeff = BinomialCoefficient(It, i, SE, Result->getType()); |
0 |
986 |
const SCEV *Coeff = BinomialCoefficient(It, i, SE, Result->getType()); |
0 |
| 987 |
if (isa(Coeff)) |
0 |
987 |
if (isa(Coeff)) |
0 |
| 988 |
return Coeff; |
0 |
988 |
return Coeff; |
0 |
| 989 |
|
--- |
989 |
|
--- |
| 990 |
Result = SE.getAddExpr(Result, SE.getMulExpr(Operands[i], Coeff)); |
0 |
990 |
Result = SE.getAddExpr(Result, SE.getMulExpr(Operands[i], Coeff)); |
0 |
| 991 |
} |
--- |
991 |
} |
--- |
| 992 |
return Result; |
0 |
992 |
return Result; |
0 |
| 993 |
} |
--- |
993 |
} |
--- |
| 994 |
|
--- |
994 |
|
--- |
| 995 |
//===----------------------------------------------------------------------===// |
--- |
995 |
//===----------------------------------------------------------------------===// |
--- |
| 996 |
// SCEV Expression folder implementations |
--- |
996 |
// SCEV Expression folder implementations |
--- |
| 997 |
//===----------------------------------------------------------------------===// |
--- |
997 |
//===----------------------------------------------------------------------===// |
--- |
| 998 |
|
--- |
998 |
|
--- |
| 999 |
const SCEV *ScalarEvolution::getLosslessPtrToIntExpr(const SCEV *Op, |
0 |
999 |
const SCEV *ScalarEvolution::getLosslessPtrToIntExpr(const SCEV *Op, |
0 |
| 1000 |
unsigned Depth) { |
--- |
1000 |
unsigned Depth) { |
--- |
| 1001 |
assert(Depth <= 1 && |
0 |
1001 |
assert(Depth <= 1 && |
0 |
| 1002 |
"getLosslessPtrToIntExpr() should self-recurse at most once."); |
--- |
1002 |
"getLosslessPtrToIntExpr() should self-recurse at most once."); |
--- |
| 1003 |
|
--- |
1003 |
|
--- |
| 1004 |
// We could be called with an integer-typed operands during SCEV rewrites. |
--- |
1004 |
// We could be called with an integer-typed operands during SCEV rewrites. |
--- |
| 1005 |
// Since the operand is an integer already, just perform zext/trunc/self cast. |
--- |
1005 |
// Since the operand is an integer already, just perform zext/trunc/self cast. |
--- |
| 1006 |
if (!Op->getType()->isPointerTy()) |
0 |
1006 |
if (!Op->getType()->isPointerTy()) |
0 |
| 1007 |
return Op; |
0 |
1007 |
return Op; |
0 |
| 1008 |
|
--- |
1008 |
|
--- |
| 1009 |
// What would be an ID for such a SCEV cast expression? |
--- |
1009 |
// What would be an ID for such a SCEV cast expression? |
--- |
| 1010 |
FoldingSetNodeID ID; |
0 |
1010 |
FoldingSetNodeID ID; |
0 |
| 1011 |
ID.AddInteger(scPtrToInt); |
0 |
1011 |
ID.AddInteger(scPtrToInt); |
0 |
| 1012 |
ID.AddPointer(Op); |
0 |
1012 |
ID.AddPointer(Op); |
0 |
| 1013 |
|
--- |
1013 |
|
--- |
| 1014 |
void *IP = nullptr; |
0 |
1014 |
void *IP = nullptr; |
0 |
| 1015 |
|
--- |
1015 |
|
--- |
| 1016 |
// Is there already an expression for such a cast? |
--- |
1016 |
// Is there already an expression for such a cast? |
--- |
| 1017 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) |
0 |
1017 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) |
0 |
| 1018 |
return S; |
0 |
1018 |
return S; |
0 |
| 1019 |
|
--- |
1019 |
|
--- |
| 1020 |
// It isn't legal for optimizations to construct new ptrtoint expressions |
--- |
1020 |
// It isn't legal for optimizations to construct new ptrtoint expressions |
--- |
| 1021 |
// for non-integral pointers. |
--- |
1021 |
// for non-integral pointers. |
--- |
| 1022 |
if (getDataLayout().isNonIntegralPointerType(Op->getType())) |
0 |
1022 |
if (getDataLayout().isNonIntegralPointerType(Op->getType())) |
0 |
| 1023 |
return getCouldNotCompute(); |
0 |
1023 |
return getCouldNotCompute(); |
0 |
| 1024 |
|
--- |
1024 |
|
--- |
| 1025 |
Type *IntPtrTy = getDataLayout().getIntPtrType(Op->getType()); |
0 |
1025 |
Type *IntPtrTy = getDataLayout().getIntPtrType(Op->getType()); |
0 |
| 1026 |
|
--- |
1026 |
|
--- |
| 1027 |
// We can only trivially model ptrtoint if SCEV's effective (integer) type |
--- |
1027 |
// We can only trivially model ptrtoint if SCEV's effective (integer) type |
--- |
| 1028 |
// is sufficiently wide to represent all possible pointer values. |
--- |
1028 |
// is sufficiently wide to represent all possible pointer values. |
--- |
| 1029 |
// We could theoretically teach SCEV to truncate wider pointers, but |
--- |
1029 |
// We could theoretically teach SCEV to truncate wider pointers, but |
--- |
| 1030 |
// that isn't implemented for now. |
--- |
1030 |
// that isn't implemented for now. |
--- |
| 1031 |
if (getDataLayout().getTypeSizeInBits(getEffectiveSCEVType(Op->getType())) != |
0 |
1031 |
if (getDataLayout().getTypeSizeInBits(getEffectiveSCEVType(Op->getType())) != |
0 |
| 1032 |
getDataLayout().getTypeSizeInBits(IntPtrTy)) |
0 |
1032 |
getDataLayout().getTypeSizeInBits(IntPtrTy)) |
0 |
| 1033 |
return getCouldNotCompute(); |
0 |
1033 |
return getCouldNotCompute(); |
0 |
| 1034 |
|
--- |
1034 |
|
--- |
| 1035 |
// If not, is this expression something we can't reduce any further? |
--- |
1035 |
// If not, is this expression something we can't reduce any further? |
--- |
| 1036 |
if (auto *U = dyn_cast(Op)) { |
0 |
1036 |
if (auto *U = dyn_cast(Op)) { |
0 |
| 1037 |
// Perform some basic constant folding. If the operand of the ptr2int cast |
--- |
1037 |
// Perform some basic constant folding. If the operand of the ptr2int cast |
--- |
| 1038 |
// is a null pointer, don't create a ptr2int SCEV expression (that will be |
--- |
1038 |
// is a null pointer, don't create a ptr2int SCEV expression (that will be |
--- |
| 1039 |
// left as-is), but produce a zero constant. |
--- |
1039 |
// left as-is), but produce a zero constant. |
--- |
| 1040 |
// NOTE: We could handle a more general case, but lack motivational cases. |
--- |
1040 |
// NOTE: We could handle a more general case, but lack motivational cases. |
--- |
| 1041 |
if (isa(U->getValue())) |
0 |
1041 |
if (isa(U->getValue())) |
0 |
| 1042 |
return getZero(IntPtrTy); |
0 |
1042 |
return getZero(IntPtrTy); |
0 |
| 1043 |
|
--- |
1043 |
|
--- |
| 1044 |
// Create an explicit cast node. |
--- |
1044 |
// Create an explicit cast node. |
--- |
| 1045 |
// We can reuse the existing insert position since if we get here, |
--- |
1045 |
// We can reuse the existing insert position since if we get here, |
--- |
| 1046 |
// we won't have made any changes which would invalidate it. |
--- |
1046 |
// we won't have made any changes which would invalidate it. |
--- |
| 1047 |
SCEV *S = new (SCEVAllocator) |
0 |
1047 |
SCEV *S = new (SCEVAllocator) |
0 |
| 1048 |
SCEVPtrToIntExpr(ID.Intern(SCEVAllocator), Op, IntPtrTy); |
0 |
1048 |
SCEVPtrToIntExpr(ID.Intern(SCEVAllocator), Op, IntPtrTy); |
0 |
| 1049 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
1049 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 1050 |
registerUser(S, Op); |
0 |
1050 |
registerUser(S, Op); |
0 |
| 1051 |
return S; |
0 |
1051 |
return S; |
0 |
| 1052 |
} |
--- |
1052 |
} |
--- |
| 1053 |
|
--- |
1053 |
|
--- |
| 1054 |
assert(Depth == 0 && "getLosslessPtrToIntExpr() should not self-recurse for " |
0 |
1054 |
assert(Depth == 0 && "getLosslessPtrToIntExpr() should not self-recurse for " |
0 |
| 1055 |
"non-SCEVUnknown's."); |
--- |
1055 |
"non-SCEVUnknown's."); |
--- |
| 1056 |
|
--- |
1056 |
|
--- |
| 1057 |
// Otherwise, we've got some expression that is more complex than just a |
--- |
1057 |
// Otherwise, we've got some expression that is more complex than just a |
--- |
| 1058 |
// single SCEVUnknown. But we don't want to have a SCEVPtrToIntExpr of an |
--- |
1058 |
// single SCEVUnknown. But we don't want to have a SCEVPtrToIntExpr of an |
--- |
| 1059 |
// arbitrary expression, we want to have SCEVPtrToIntExpr of an SCEVUnknown |
--- |
1059 |
// arbitrary expression, we want to have SCEVPtrToIntExpr of an SCEVUnknown |
--- |
| 1060 |
// only, and the expressions must otherwise be integer-typed. |
--- |
1060 |
// only, and the expressions must otherwise be integer-typed. |
--- |
| 1061 |
// So sink the cast down to the SCEVUnknown's. |
--- |
1061 |
// So sink the cast down to the SCEVUnknown's. |
--- |
| 1062 |
|
--- |
1062 |
|
--- |
| 1063 |
/// The SCEVPtrToIntSinkingRewriter takes a scalar evolution expression, |
--- |
1063 |
/// The SCEVPtrToIntSinkingRewriter takes a scalar evolution expression, |
--- |
| 1064 |
/// which computes a pointer-typed value, and rewrites the whole expression |
--- |
1064 |
/// which computes a pointer-typed value, and rewrites the whole expression |
--- |
| 1065 |
/// tree so that *all* the computations are done on integers, and the only |
--- |
1065 |
/// tree so that *all* the computations are done on integers, and the only |
--- |
| 1066 |
/// pointer-typed operands in the expression are SCEVUnknown. |
--- |
1066 |
/// pointer-typed operands in the expression are SCEVUnknown. |
--- |
| 1067 |
class SCEVPtrToIntSinkingRewriter |
--- |
1067 |
class SCEVPtrToIntSinkingRewriter |
--- |
| 1068 |
: public SCEVRewriteVisitor { |
--- |
1068 |
: public SCEVRewriteVisitor { |
--- |
| 1069 |
using Base = SCEVRewriteVisitor; |
--- |
1069 |
using Base = SCEVRewriteVisitor; |
--- |
| 1070 |
|
--- |
1070 |
|
--- |
| 1071 |
public: |
--- |
1071 |
public: |
--- |
| 1072 |
SCEVPtrToIntSinkingRewriter(ScalarEvolution &SE) : SCEVRewriteVisitor(SE) {} |
0 |
1072 |
SCEVPtrToIntSinkingRewriter(ScalarEvolution &SE) : SCEVRewriteVisitor(SE) {} |
0 |
| 1073 |
|
--- |
1073 |
|
--- |
| 1074 |
static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE) { |
0 |
1074 |
static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE) { |
0 |
| 1075 |
SCEVPtrToIntSinkingRewriter Rewriter(SE); |
0 |
1075 |
SCEVPtrToIntSinkingRewriter Rewriter(SE); |
0 |
| 1076 |
return Rewriter.visit(Scev); |
0 |
1076 |
return Rewriter.visit(Scev); |
0 |
| 1077 |
} |
0 |
1077 |
} |
0 |
| 1078 |
|
--- |
1078 |
|
--- |
| 1079 |
const SCEV *visit(const SCEV *S) { |
0 |
1079 |
const SCEV *visit(const SCEV *S) { |
0 |
| 1080 |
Type *STy = S->getType(); |
0 |
1080 |
Type *STy = S->getType(); |
0 |
| 1081 |
// If the expression is not pointer-typed, just keep it as-is. |
--- |
1081 |
// If the expression is not pointer-typed, just keep it as-is. |
--- |
| 1082 |
if (!STy->isPointerTy()) |
0 |
1082 |
if (!STy->isPointerTy()) |
0 |
| 1083 |
return S; |
0 |
1083 |
return S; |
0 |
| 1084 |
// Else, recursively sink the cast down into it. |
--- |
1084 |
// Else, recursively sink the cast down into it. |
--- |
| 1085 |
return Base::visit(S); |
0 |
1085 |
return Base::visit(S); |
0 |
| 1086 |
} |
--- |
1086 |
} |
--- |
| 1087 |
|
--- |
1087 |
|
--- |
| 1088 |
const SCEV *visitAddExpr(const SCEVAddExpr *Expr) { |
0 |
1088 |
const SCEV *visitAddExpr(const SCEVAddExpr *Expr) { |
0 |
| 1089 |
SmallVector Operands; |
0 |
1089 |
SmallVector Operands; |
0 |
| 1090 |
bool Changed = false; |
0 |
1090 |
bool Changed = false; |
0 |
| 1091 |
for (const auto *Op : Expr->operands()) { |
0 |
1091 |
for (const auto *Op : Expr->operands()) { |
0 |
| 1092 |
Operands.push_back(visit(Op)); |
0 |
1092 |
Operands.push_back(visit(Op)); |
0 |
| 1093 |
Changed |= Op != Operands.back(); |
0 |
1093 |
Changed |= Op != Operands.back(); |
0 |
| 1094 |
} |
--- |
1094 |
} |
--- |
| 1095 |
return !Changed ? Expr : SE.getAddExpr(Operands, Expr->getNoWrapFlags()); |
0 |
1095 |
return !Changed ? Expr : SE.getAddExpr(Operands, Expr->getNoWrapFlags()); |
0 |
| 1096 |
} |
0 |
1096 |
} |
0 |
| 1097 |
|
--- |
1097 |
|
--- |
| 1098 |
const SCEV *visitMulExpr(const SCEVMulExpr *Expr) { |
0 |
1098 |
const SCEV *visitMulExpr(const SCEVMulExpr *Expr) { |
0 |
| 1099 |
SmallVector Operands; |
0 |
1099 |
SmallVector Operands; |
0 |
| 1100 |
bool Changed = false; |
0 |
1100 |
bool Changed = false; |
0 |
| 1101 |
for (const auto *Op : Expr->operands()) { |
0 |
1101 |
for (const auto *Op : Expr->operands()) { |
0 |
| 1102 |
Operands.push_back(visit(Op)); |
0 |
1102 |
Operands.push_back(visit(Op)); |
0 |
| 1103 |
Changed |= Op != Operands.back(); |
0 |
1103 |
Changed |= Op != Operands.back(); |
0 |
| 1104 |
} |
--- |
1104 |
} |
--- |
| 1105 |
return !Changed ? Expr : SE.getMulExpr(Operands, Expr->getNoWrapFlags()); |
0 |
1105 |
return !Changed ? Expr : SE.getMulExpr(Operands, Expr->getNoWrapFlags()); |
0 |
| 1106 |
} |
0 |
1106 |
} |
0 |
| 1107 |
|
--- |
1107 |
|
--- |
| 1108 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
1108 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
| 1109 |
assert(Expr->getType()->isPointerTy() && |
0 |
1109 |
assert(Expr->getType()->isPointerTy() && |
0 |
| 1110 |
"Should only reach pointer-typed SCEVUnknown's."); |
--- |
1110 |
"Should only reach pointer-typed SCEVUnknown's."); |
--- |
| 1111 |
return SE.getLosslessPtrToIntExpr(Expr, /*Depth=*/1); |
0 |
1111 |
return SE.getLosslessPtrToIntExpr(Expr, /*Depth=*/1); |
0 |
| 1112 |
} |
--- |
1112 |
} |
--- |
| 1113 |
}; |
--- |
1113 |
}; |
--- |
| 1114 |
|
--- |
1114 |
|
--- |
| 1115 |
// And actually perform the cast sinking. |
--- |
1115 |
// And actually perform the cast sinking. |
--- |
| 1116 |
const SCEV *IntOp = SCEVPtrToIntSinkingRewriter::rewrite(Op, *this); |
0 |
1116 |
const SCEV *IntOp = SCEVPtrToIntSinkingRewriter::rewrite(Op, *this); |
0 |
| 1117 |
assert(IntOp->getType()->isIntegerTy() && |
0 |
1117 |
assert(IntOp->getType()->isIntegerTy() && |
0 |
| 1118 |
"We must have succeeded in sinking the cast, " |
--- |
1118 |
"We must have succeeded in sinking the cast, " |
--- |
| 1119 |
"and ending up with an integer-typed expression!"); |
--- |
1119 |
"and ending up with an integer-typed expression!"); |
--- |
| 1120 |
return IntOp; |
0 |
1120 |
return IntOp; |
0 |
| 1121 |
} |
0 |
1121 |
} |
0 |
| 1122 |
|
--- |
1122 |
|
--- |
| 1123 |
const SCEV *ScalarEvolution::getPtrToIntExpr(const SCEV *Op, Type *Ty) { |
0 |
1123 |
const SCEV *ScalarEvolution::getPtrToIntExpr(const SCEV *Op, Type *Ty) { |
0 |
| 1124 |
assert(Ty->isIntegerTy() && "Target type must be an integer type!"); |
0 |
1124 |
assert(Ty->isIntegerTy() && "Target type must be an integer type!"); |
0 |
| 1125 |
|
--- |
1125 |
|
--- |
| 1126 |
const SCEV *IntOp = getLosslessPtrToIntExpr(Op); |
0 |
1126 |
const SCEV *IntOp = getLosslessPtrToIntExpr(Op); |
0 |
| 1127 |
if (isa(IntOp)) |
0 |
1127 |
if (isa(IntOp)) |
0 |
| 1128 |
return IntOp; |
0 |
1128 |
return IntOp; |
0 |
| 1129 |
|
--- |
1129 |
|
--- |
| 1130 |
return getTruncateOrZeroExtend(IntOp, Ty); |
0 |
1130 |
return getTruncateOrZeroExtend(IntOp, Ty); |
0 |
| 1131 |
} |
--- |
1131 |
} |
--- |
| 1132 |
|
--- |
1132 |
|
--- |
| 1133 |
const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, Type *Ty, |
0 |
1133 |
const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, Type *Ty, |
0 |
| 1134 |
unsigned Depth) { |
--- |
1134 |
unsigned Depth) { |
--- |
| 1135 |
assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && |
0 |
1135 |
assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && |
0 |
| 1136 |
"This is not a truncating conversion!"); |
--- |
1136 |
"This is not a truncating conversion!"); |
--- |
| 1137 |
assert(isSCEVable(Ty) && |
0 |
1137 |
assert(isSCEVable(Ty) && |
0 |
| 1138 |
"This is not a conversion to a SCEVable type!"); |
--- |
1138 |
"This is not a conversion to a SCEVable type!"); |
--- |
| 1139 |
assert(!Op->getType()->isPointerTy() && "Can't truncate pointer!"); |
0 |
1139 |
assert(!Op->getType()->isPointerTy() && "Can't truncate pointer!"); |
0 |
| 1140 |
Ty = getEffectiveSCEVType(Ty); |
0 |
1140 |
Ty = getEffectiveSCEVType(Ty); |
0 |
| 1141 |
|
--- |
1141 |
|
--- |
| 1142 |
FoldingSetNodeID ID; |
0 |
1142 |
FoldingSetNodeID ID; |
0 |
| 1143 |
ID.AddInteger(scTruncate); |
0 |
1143 |
ID.AddInteger(scTruncate); |
0 |
| 1144 |
ID.AddPointer(Op); |
0 |
1144 |
ID.AddPointer(Op); |
0 |
| 1145 |
ID.AddPointer(Ty); |
0 |
1145 |
ID.AddPointer(Ty); |
0 |
| 1146 |
void *IP = nullptr; |
0 |
1146 |
void *IP = nullptr; |
0 |
| 1147 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
0 |
1147 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
0 |
| 1148 |
|
--- |
1148 |
|
--- |
| 1149 |
// Fold if the operand is constant. |
--- |
1149 |
// Fold if the operand is constant. |
--- |
| 1150 |
if (const SCEVConstant *SC = dyn_cast(Op)) |
0 |
1150 |
if (const SCEVConstant *SC = dyn_cast(Op)) |
0 |
| 1151 |
return getConstant( |
0 |
1151 |
return getConstant( |
0 |
| 1152 |
cast(ConstantExpr::getTrunc(SC->getValue(), Ty))); |
0 |
1152 |
cast(ConstantExpr::getTrunc(SC->getValue(), Ty))); |
0 |
| 1153 |
|
--- |
1153 |
|
--- |
| 1154 |
// trunc(trunc(x)) --> trunc(x) |
--- |
1154 |
// trunc(trunc(x)) --> trunc(x) |
--- |
| 1155 |
if (const SCEVTruncateExpr *ST = dyn_cast(Op)) |
0 |
1155 |
if (const SCEVTruncateExpr *ST = dyn_cast(Op)) |
0 |
| 1156 |
return getTruncateExpr(ST->getOperand(), Ty, Depth + 1); |
0 |
1156 |
return getTruncateExpr(ST->getOperand(), Ty, Depth + 1); |
0 |
| 1157 |
|
--- |
1157 |
|
--- |
| 1158 |
// trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing |
--- |
1158 |
// trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing |
--- |
| 1159 |
if (const SCEVSignExtendExpr *SS = dyn_cast(Op)) |
0 |
1159 |
if (const SCEVSignExtendExpr *SS = dyn_cast(Op)) |
0 |
| 1160 |
return getTruncateOrSignExtend(SS->getOperand(), Ty, Depth + 1); |
0 |
1160 |
return getTruncateOrSignExtend(SS->getOperand(), Ty, Depth + 1); |
0 |
| 1161 |
|
--- |
1161 |
|
--- |
| 1162 |
// trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing |
--- |
1162 |
// trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing |
--- |
| 1163 |
if (const SCEVZeroExtendExpr *SZ = dyn_cast(Op)) |
0 |
1163 |
if (const SCEVZeroExtendExpr *SZ = dyn_cast(Op)) |
0 |
| 1164 |
return getTruncateOrZeroExtend(SZ->getOperand(), Ty, Depth + 1); |
0 |
1164 |
return getTruncateOrZeroExtend(SZ->getOperand(), Ty, Depth + 1); |
0 |
| 1165 |
|
--- |
1165 |
|
--- |
| 1166 |
if (Depth > MaxCastDepth) { |
0 |
1166 |
if (Depth > MaxCastDepth) { |
0 |
| 1167 |
SCEV *S = |
--- |
1167 |
SCEV *S = |
--- |
| 1168 |
new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator), Op, Ty); |
0 |
1168 |
new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator), Op, Ty); |
0 |
| 1169 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
1169 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 1170 |
registerUser(S, Op); |
0 |
1170 |
registerUser(S, Op); |
0 |
| 1171 |
return S; |
0 |
1171 |
return S; |
0 |
| 1172 |
} |
--- |
1172 |
} |
--- |
| 1173 |
|
--- |
1173 |
|
--- |
| 1174 |
// trunc(x1 + ... + xN) --> trunc(x1) + ... + trunc(xN) and |
--- |
1174 |
// trunc(x1 + ... + xN) --> trunc(x1) + ... + trunc(xN) and |
--- |
| 1175 |
// trunc(x1 * ... * xN) --> trunc(x1) * ... * trunc(xN), |
--- |
1175 |
// trunc(x1 * ... * xN) --> trunc(x1) * ... * trunc(xN), |
--- |
| 1176 |
// if after transforming we have at most one truncate, not counting truncates |
--- |
1176 |
// if after transforming we have at most one truncate, not counting truncates |
--- |
| 1177 |
// that replace other casts. |
--- |
1177 |
// that replace other casts. |
--- |
| 1178 |
if (isa(Op) || isa(Op)) { |
0 |
1178 |
if (isa(Op) || isa(Op)) { |
0 |
| 1179 |
auto *CommOp = cast(Op); |
0 |
1179 |
auto *CommOp = cast(Op); |
0 |
| 1180 |
SmallVector Operands; |
0 |
1180 |
SmallVector Operands; |
0 |
| 1181 |
unsigned numTruncs = 0; |
0 |
1181 |
unsigned numTruncs = 0; |
0 |
| 1182 |
for (unsigned i = 0, e = CommOp->getNumOperands(); i != e && numTruncs < 2; |
0 |
1182 |
for (unsigned i = 0, e = CommOp->getNumOperands(); i != e && numTruncs < 2; |
0 |
| 1183 |
++i) { |
--- |
1183 |
++i) { |
--- |
| 1184 |
const SCEV *S = getTruncateExpr(CommOp->getOperand(i), Ty, Depth + 1); |
0 |
1184 |
const SCEV *S = getTruncateExpr(CommOp->getOperand(i), Ty, Depth + 1); |
0 |
| 1185 |
if (!isa(CommOp->getOperand(i)) && |
0 |
1185 |
if (!isa(CommOp->getOperand(i)) && |
0 |
| 1186 |
isa(S)) |
0 |
1186 |
isa(S)) |
0 |
| 1187 |
numTruncs++; |
0 |
1187 |
numTruncs++; |
0 |
| 1188 |
Operands.push_back(S); |
0 |
1188 |
Operands.push_back(S); |
0 |
| 1189 |
} |
--- |
1189 |
} |
--- |
| 1190 |
if (numTruncs < 2) { |
0 |
1190 |
if (numTruncs < 2) { |
0 |
| 1191 |
if (isa(Op)) |
0 |
1191 |
if (isa(Op)) |
0 |
| 1192 |
return getAddExpr(Operands); |
0 |
1192 |
return getAddExpr(Operands); |
0 |
| 1193 |
if (isa(Op)) |
0 |
1193 |
if (isa(Op)) |
0 |
| 1194 |
return getMulExpr(Operands); |
0 |
1194 |
return getMulExpr(Operands); |
0 |
| 1195 |
llvm_unreachable("Unexpected SCEV type for Op."); |
0 |
1195 |
llvm_unreachable("Unexpected SCEV type for Op."); |
0 |
| 1196 |
} |
--- |
1196 |
} |
--- |
| 1197 |
// Although we checked in the beginning that ID is not in the cache, it is |
--- |
1197 |
// Although we checked in the beginning that ID is not in the cache, it is |
--- |
| 1198 |
// possible that during recursion and different modification ID was inserted |
--- |
1198 |
// possible that during recursion and different modification ID was inserted |
--- |
| 1199 |
// into the cache. So if we find it, just return it. |
--- |
1199 |
// into the cache. So if we find it, just return it. |
--- |
| 1200 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) |
0 |
1200 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) |
0 |
| 1201 |
return S; |
0 |
1201 |
return S; |
0 |
| 1202 |
} |
0 |
1202 |
} |
0 |
| 1203 |
|
--- |
1203 |
|
--- |
| 1204 |
// If the input value is a chrec scev, truncate the chrec's operands. |
--- |
1204 |
// If the input value is a chrec scev, truncate the chrec's operands. |
--- |
| 1205 |
if (const SCEVAddRecExpr *AddRec = dyn_cast(Op)) { |
0 |
1205 |
if (const SCEVAddRecExpr *AddRec = dyn_cast(Op)) { |
0 |
| 1206 |
SmallVector Operands; |
0 |
1206 |
SmallVector Operands; |
0 |
| 1207 |
for (const SCEV *Op : AddRec->operands()) |
0 |
1207 |
for (const SCEV *Op : AddRec->operands()) |
0 |
| 1208 |
Operands.push_back(getTruncateExpr(Op, Ty, Depth + 1)); |
0 |
1208 |
Operands.push_back(getTruncateExpr(Op, Ty, Depth + 1)); |
0 |
| 1209 |
return getAddRecExpr(Operands, AddRec->getLoop(), SCEV::FlagAnyWrap); |
0 |
1209 |
return getAddRecExpr(Operands, AddRec->getLoop(), SCEV::FlagAnyWrap); |
0 |
| 1210 |
} |
0 |
1210 |
} |
0 |
| 1211 |
|
--- |
1211 |
|
--- |
| 1212 |
// Return zero if truncating to known zeros. |
--- |
1212 |
// Return zero if truncating to known zeros. |
--- |
| 1213 |
uint32_t MinTrailingZeros = getMinTrailingZeros(Op); |
0 |
1213 |
uint32_t MinTrailingZeros = getMinTrailingZeros(Op); |
0 |
| 1214 |
if (MinTrailingZeros >= getTypeSizeInBits(Ty)) |
0 |
1214 |
if (MinTrailingZeros >= getTypeSizeInBits(Ty)) |
0 |
| 1215 |
return getZero(Ty); |
0 |
1215 |
return getZero(Ty); |
0 |
| 1216 |
|
--- |
1216 |
|
--- |
| 1217 |
// The cast wasn't folded; create an explicit cast node. We can reuse |
--- |
1217 |
// The cast wasn't folded; create an explicit cast node. We can reuse |
--- |
| 1218 |
// the existing insert position since if we get here, we won't have |
--- |
1218 |
// the existing insert position since if we get here, we won't have |
--- |
| 1219 |
// made any changes which would invalidate it. |
--- |
1219 |
// made any changes which would invalidate it. |
--- |
| 1220 |
SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator), |
0 |
1220 |
SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator), |
0 |
| 1221 |
Op, Ty); |
0 |
1221 |
Op, Ty); |
0 |
| 1222 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
1222 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 1223 |
registerUser(S, Op); |
0 |
1223 |
registerUser(S, Op); |
0 |
| 1224 |
return S; |
0 |
1224 |
return S; |
0 |
| 1225 |
} |
0 |
1225 |
} |
0 |
| 1226 |
|
--- |
1226 |
|
--- |
| 1227 |
// Get the limit of a recurrence such that incrementing by Step cannot cause |
--- |
1227 |
// Get the limit of a recurrence such that incrementing by Step cannot cause |
--- |
| 1228 |
// signed overflow as long as the value of the recurrence within the |
--- |
1228 |
// signed overflow as long as the value of the recurrence within the |
--- |
| 1229 |
// loop does not exceed this limit before incrementing. |
--- |
1229 |
// loop does not exceed this limit before incrementing. |
--- |
| 1230 |
static const SCEV *getSignedOverflowLimitForStep(const SCEV *Step, |
0 |
1230 |
static const SCEV *getSignedOverflowLimitForStep(const SCEV *Step, |
0 |
| 1231 |
ICmpInst::Predicate *Pred, |
--- |
1231 |
ICmpInst::Predicate *Pred, |
--- |
| 1232 |
ScalarEvolution *SE) { |
--- |
1232 |
ScalarEvolution *SE) { |
--- |
| 1233 |
unsigned BitWidth = SE->getTypeSizeInBits(Step->getType()); |
0 |
1233 |
unsigned BitWidth = SE->getTypeSizeInBits(Step->getType()); |
0 |
| 1234 |
if (SE->isKnownPositive(Step)) { |
0 |
1234 |
if (SE->isKnownPositive(Step)) { |
0 |
| 1235 |
*Pred = ICmpInst::ICMP_SLT; |
0 |
1235 |
*Pred = ICmpInst::ICMP_SLT; |
0 |
| 1236 |
return SE->getConstant(APInt::getSignedMinValue(BitWidth) - |
0 |
1236 |
return SE->getConstant(APInt::getSignedMinValue(BitWidth) - |
0 |
| 1237 |
SE->getSignedRangeMax(Step)); |
0 |
1237 |
SE->getSignedRangeMax(Step)); |
0 |
| 1238 |
} |
--- |
1238 |
} |
--- |
| 1239 |
if (SE->isKnownNegative(Step)) { |
0 |
1239 |
if (SE->isKnownNegative(Step)) { |
0 |
| 1240 |
*Pred = ICmpInst::ICMP_SGT; |
0 |
1240 |
*Pred = ICmpInst::ICMP_SGT; |
0 |
| 1241 |
return SE->getConstant(APInt::getSignedMaxValue(BitWidth) - |
0 |
1241 |
return SE->getConstant(APInt::getSignedMaxValue(BitWidth) - |
0 |
| 1242 |
SE->getSignedRangeMin(Step)); |
0 |
1242 |
SE->getSignedRangeMin(Step)); |
0 |
| 1243 |
} |
--- |
1243 |
} |
--- |
| 1244 |
return nullptr; |
0 |
1244 |
return nullptr; |
0 |
| 1245 |
} |
--- |
1245 |
} |
--- |
| 1246 |
|
--- |
1246 |
|
--- |
| 1247 |
// Get the limit of a recurrence such that incrementing by Step cannot cause |
--- |
1247 |
// Get the limit of a recurrence such that incrementing by Step cannot cause |
--- |
| 1248 |
// unsigned overflow as long as the value of the recurrence within the loop does |
--- |
1248 |
// unsigned overflow as long as the value of the recurrence within the loop does |
--- |
| 1249 |
// not exceed this limit before incrementing. |
--- |
1249 |
// not exceed this limit before incrementing. |
--- |
| 1250 |
static const SCEV *getUnsignedOverflowLimitForStep(const SCEV *Step, |
0 |
1250 |
static const SCEV *getUnsignedOverflowLimitForStep(const SCEV *Step, |
0 |
| 1251 |
ICmpInst::Predicate *Pred, |
--- |
1251 |
ICmpInst::Predicate *Pred, |
--- |
| 1252 |
ScalarEvolution *SE) { |
--- |
1252 |
ScalarEvolution *SE) { |
--- |
| 1253 |
unsigned BitWidth = SE->getTypeSizeInBits(Step->getType()); |
0 |
1253 |
unsigned BitWidth = SE->getTypeSizeInBits(Step->getType()); |
0 |
| 1254 |
*Pred = ICmpInst::ICMP_ULT; |
0 |
1254 |
*Pred = ICmpInst::ICMP_ULT; |
0 |
| 1255 |
|
--- |
1255 |
|
--- |
| 1256 |
return SE->getConstant(APInt::getMinValue(BitWidth) - |
0 |
1256 |
return SE->getConstant(APInt::getMinValue(BitWidth) - |
0 |
| 1257 |
SE->getUnsignedRangeMax(Step)); |
0 |
1257 |
SE->getUnsignedRangeMax(Step)); |
0 |
| 1258 |
} |
--- |
1258 |
} |
--- |
| 1259 |
|
--- |
1259 |
|
--- |
| 1260 |
namespace { |
--- |
1260 |
namespace { |
--- |
| 1261 |
|
--- |
1261 |
|
--- |
| 1262 |
struct ExtendOpTraitsBase { |
--- |
1262 |
struct ExtendOpTraitsBase { |
--- |
| 1263 |
typedef const SCEV *(ScalarEvolution::*GetExtendExprTy)(const SCEV *, Type *, |
--- |
1263 |
typedef const SCEV *(ScalarEvolution::*GetExtendExprTy)(const SCEV *, Type *, |
--- |
| 1264 |
unsigned); |
--- |
1264 |
unsigned); |
--- |
| 1265 |
}; |
--- |
1265 |
}; |
--- |
| 1266 |
|
--- |
1266 |
|
--- |
| 1267 |
// Used to make code generic over signed and unsigned overflow. |
--- |
1267 |
// Used to make code generic over signed and unsigned overflow. |
--- |
| 1268 |
template struct ExtendOpTraits { |
--- |
1268 |
template struct ExtendOpTraits { |
--- |
| 1269 |
// Members present: |
--- |
1269 |
// Members present: |
--- |
| 1270 |
// |
--- |
1270 |
// |
--- |
| 1271 |
// static const SCEV::NoWrapFlags WrapType; |
--- |
1271 |
// static const SCEV::NoWrapFlags WrapType; |
--- |
| 1272 |
// |
--- |
1272 |
// |
--- |
| 1273 |
// static const ExtendOpTraitsBase::GetExtendExprTy GetExtendExpr; |
--- |
1273 |
// static const ExtendOpTraitsBase::GetExtendExprTy GetExtendExpr; |
--- |
| 1274 |
// |
--- |
1274 |
// |
--- |
| 1275 |
// static const SCEV *getOverflowLimitForStep(const SCEV *Step, |
--- |
1275 |
// static const SCEV *getOverflowLimitForStep(const SCEV *Step, |
--- |
| 1276 |
// ICmpInst::Predicate *Pred, |
--- |
1276 |
// ICmpInst::Predicate *Pred, |
--- |
| 1277 |
// ScalarEvolution *SE); |
--- |
1277 |
// ScalarEvolution *SE); |
--- |
| 1278 |
}; |
--- |
1278 |
}; |
--- |
| 1279 |
|
--- |
1279 |
|
--- |
| 1280 |
template <> |
--- |
1280 |
template <> |
--- |
| 1281 |
struct ExtendOpTraits : public ExtendOpTraitsBase { |
--- |
1281 |
struct ExtendOpTraits : public ExtendOpTraitsBase { |
--- |
| 1282 |
static const SCEV::NoWrapFlags WrapType = SCEV::FlagNSW; |
--- |
1282 |
static const SCEV::NoWrapFlags WrapType = SCEV::FlagNSW; |
--- |
| 1283 |
|
--- |
1283 |
|
--- |
| 1284 |
static const GetExtendExprTy GetExtendExpr; |
--- |
1284 |
static const GetExtendExprTy GetExtendExpr; |
--- |
| 1285 |
|
--- |
1285 |
|
--- |
| 1286 |
static const SCEV *getOverflowLimitForStep(const SCEV *Step, |
0 |
1286 |
static const SCEV *getOverflowLimitForStep(const SCEV *Step, |
0 |
| 1287 |
ICmpInst::Predicate *Pred, |
--- |
1287 |
ICmpInst::Predicate *Pred, |
--- |
| 1288 |
ScalarEvolution *SE) { |
--- |
1288 |
ScalarEvolution *SE) { |
--- |
| 1289 |
return getSignedOverflowLimitForStep(Step, Pred, SE); |
0 |
1289 |
return getSignedOverflowLimitForStep(Step, Pred, SE); |
0 |
| 1290 |
} |
--- |
1290 |
} |
--- |
| 1291 |
}; |
--- |
1291 |
}; |
--- |
| 1292 |
|
--- |
1292 |
|
--- |
| 1293 |
const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits< |
--- |
1293 |
const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits< |
--- |
| 1294 |
SCEVSignExtendExpr>::GetExtendExpr = &ScalarEvolution::getSignExtendExpr; |
--- |
1294 |
SCEVSignExtendExpr>::GetExtendExpr = &ScalarEvolution::getSignExtendExpr; |
--- |
| 1295 |
|
--- |
1295 |
|
--- |
| 1296 |
template <> |
--- |
1296 |
template <> |
--- |
| 1297 |
struct ExtendOpTraits : public ExtendOpTraitsBase { |
--- |
1297 |
struct ExtendOpTraits : public ExtendOpTraitsBase { |
--- |
| 1298 |
static const SCEV::NoWrapFlags WrapType = SCEV::FlagNUW; |
--- |
1298 |
static const SCEV::NoWrapFlags WrapType = SCEV::FlagNUW; |
--- |
| 1299 |
|
--- |
1299 |
|
--- |
| 1300 |
static const GetExtendExprTy GetExtendExpr; |
--- |
1300 |
static const GetExtendExprTy GetExtendExpr; |
--- |
| 1301 |
|
--- |
1301 |
|
--- |
| 1302 |
static const SCEV *getOverflowLimitForStep(const SCEV *Step, |
0 |
1302 |
static const SCEV *getOverflowLimitForStep(const SCEV *Step, |
0 |
| 1303 |
ICmpInst::Predicate *Pred, |
--- |
1303 |
ICmpInst::Predicate *Pred, |
--- |
| 1304 |
ScalarEvolution *SE) { |
--- |
1304 |
ScalarEvolution *SE) { |
--- |
| 1305 |
return getUnsignedOverflowLimitForStep(Step, Pred, SE); |
0 |
1305 |
return getUnsignedOverflowLimitForStep(Step, Pred, SE); |
0 |
| 1306 |
} |
--- |
1306 |
} |
--- |
| 1307 |
}; |
--- |
1307 |
}; |
--- |
| 1308 |
|
--- |
1308 |
|
--- |
| 1309 |
const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits< |
--- |
1309 |
const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits< |
--- |
| 1310 |
SCEVZeroExtendExpr>::GetExtendExpr = &ScalarEvolution::getZeroExtendExpr; |
--- |
1310 |
SCEVZeroExtendExpr>::GetExtendExpr = &ScalarEvolution::getZeroExtendExpr; |
--- |
| 1311 |
|
--- |
1311 |
|
--- |
| 1312 |
} // end anonymous namespace |
--- |
1312 |
} // end anonymous namespace |
--- |
| 1313 |
|
--- |
1313 |
|
--- |
| 1314 |
// The recurrence AR has been shown to have no signed/unsigned wrap or something |
--- |
1314 |
// The recurrence AR has been shown to have no signed/unsigned wrap or something |
--- |
| 1315 |
// close to it. Typically, if we can prove NSW/NUW for AR, then we can just as |
--- |
1315 |
// close to it. Typically, if we can prove NSW/NUW for AR, then we can just as |
--- |
| 1316 |
// easily prove NSW/NUW for its preincrement or postincrement sibling. This |
--- |
1316 |
// easily prove NSW/NUW for its preincrement or postincrement sibling. This |
--- |
| 1317 |
// allows normalizing a sign/zero extended AddRec as such: {sext/zext(Step + |
--- |
1317 |
// allows normalizing a sign/zero extended AddRec as such: {sext/zext(Step + |
--- |
| 1318 |
// Start),+,Step} => {(Step + sext/zext(Start),+,Step} As a result, the |
--- |
1318 |
// Start),+,Step} => {(Step + sext/zext(Start),+,Step} As a result, the |
--- |
| 1319 |
// expression "Step + sext/zext(PreIncAR)" is congruent with |
--- |
1319 |
// expression "Step + sext/zext(PreIncAR)" is congruent with |
--- |
| 1320 |
// "sext/zext(PostIncAR)" |
--- |
1320 |
// "sext/zext(PostIncAR)" |
--- |
| 1321 |
template |
--- |
1321 |
template |
--- |
| 1322 |
static const SCEV *getPreStartForExtend(const SCEVAddRecExpr *AR, Type *Ty, |
0 |
1322 |
static const SCEV *getPreStartForExtend(const SCEVAddRecExpr *AR, Type *Ty, |
0 |
| 1323 |
ScalarEvolution *SE, unsigned Depth) { |
--- |
1323 |
ScalarEvolution *SE, unsigned Depth) { |
--- |
| 1324 |
auto WrapType = ExtendOpTraits::WrapType; |
0 |
1324 |
auto WrapType = ExtendOpTraits::WrapType; |
0 |
| 1325 |
auto GetExtendExpr = ExtendOpTraits::GetExtendExpr; |
0 |
1325 |
auto GetExtendExpr = ExtendOpTraits::GetExtendExpr; |
0 |
| 1326 |
|
--- |
1326 |
|
--- |
| 1327 |
const Loop *L = AR->getLoop(); |
0 |
1327 |
const Loop *L = AR->getLoop(); |
0 |
| 1328 |
const SCEV *Start = AR->getStart(); |
0 |
1328 |
const SCEV *Start = AR->getStart(); |
0 |
| 1329 |
const SCEV *Step = AR->getStepRecurrence(*SE); |
0 |
1329 |
const SCEV *Step = AR->getStepRecurrence(*SE); |
0 |
| 1330 |
|
--- |
1330 |
|
--- |
| 1331 |
// Check for a simple looking step prior to loop entry. |
--- |
1331 |
// Check for a simple looking step prior to loop entry. |
--- |
| 1332 |
const SCEVAddExpr *SA = dyn_cast(Start); |
0 |
1332 |
const SCEVAddExpr *SA = dyn_cast(Start); |
0 |
| 1333 |
if (!SA) |
0 |
1333 |
if (!SA) |
0 |
| 1334 |
return nullptr; |
0 |
1334 |
return nullptr; |
0 |
| 1335 |
|
--- |
1335 |
|
--- |
| 1336 |
// Create an AddExpr for "PreStart" after subtracting Step. Full SCEV |
--- |
1336 |
// Create an AddExpr for "PreStart" after subtracting Step. Full SCEV |
--- |
| 1337 |
// subtraction is expensive. For this purpose, perform a quick and dirty |
--- |
1337 |
// subtraction is expensive. For this purpose, perform a quick and dirty |
--- |
| 1338 |
// difference, by checking for Step in the operand list. |
--- |
1338 |
// difference, by checking for Step in the operand list. |
--- |
| 1339 |
SmallVector DiffOps; |
0 |
1339 |
SmallVector DiffOps; |
0 |
| 1340 |
for (const SCEV *Op : SA->operands()) |
0 |
1340 |
for (const SCEV *Op : SA->operands()) |
0 |
| 1341 |
if (Op != Step) |
0 |
1341 |
if (Op != Step) |
0 |
| 1342 |
DiffOps.push_back(Op); |
0 |
1342 |
DiffOps.push_back(Op); |
0 |
| 1343 |
|
--- |
1343 |
|
--- |
| 1344 |
if (DiffOps.size() == SA->getNumOperands()) |
0 |
1344 |
if (DiffOps.size() == SA->getNumOperands()) |
0 |
| 1345 |
return nullptr; |
0 |
1345 |
return nullptr; |
0 |
| 1346 |
|
--- |
1346 |
|
--- |
| 1347 |
// Try to prove `WrapType` (SCEV::FlagNSW or SCEV::FlagNUW) on `PreStart` + |
--- |
1347 |
// Try to prove `WrapType` (SCEV::FlagNSW or SCEV::FlagNUW) on `PreStart` + |
--- |
| 1348 |
// `Step`: |
--- |
1348 |
// `Step`: |
--- |
| 1349 |
|
--- |
1349 |
|
--- |
| 1350 |
// 1. NSW/NUW flags on the step increment. |
--- |
1350 |
// 1. NSW/NUW flags on the step increment. |
--- |
| 1351 |
auto PreStartFlags = |
--- |
1351 |
auto PreStartFlags = |
--- |
| 1352 |
ScalarEvolution::maskFlags(SA->getNoWrapFlags(), SCEV::FlagNUW); |
0 |
1352 |
ScalarEvolution::maskFlags(SA->getNoWrapFlags(), SCEV::FlagNUW); |
0 |
| 1353 |
const SCEV *PreStart = SE->getAddExpr(DiffOps, PreStartFlags); |
0 |
1353 |
const SCEV *PreStart = SE->getAddExpr(DiffOps, PreStartFlags); |
0 |
| 1354 |
const SCEVAddRecExpr *PreAR = dyn_cast( |
0 |
1354 |
const SCEVAddRecExpr *PreAR = dyn_cast( |
0 |
| 1355 |
SE->getAddRecExpr(PreStart, Step, L, SCEV::FlagAnyWrap)); |
--- |
1355 |
SE->getAddRecExpr(PreStart, Step, L, SCEV::FlagAnyWrap)); |
--- |
| 1356 |
|
--- |
1356 |
|
--- |
| 1357 |
// "{S,+,X} is /" and "the backedge is taken at least once" implies |
--- |
1357 |
// "{S,+,X} is /" and "the backedge is taken at least once" implies |
--- |
| 1358 |
// "S+X does not sign/unsign-overflow". |
--- |
1358 |
// "S+X does not sign/unsign-overflow". |
--- |
| 1359 |
// |
--- |
1359 |
// |
--- |
| 1360 |
|
--- |
1360 |
|
--- |
| 1361 |
const SCEV *BECount = SE->getBackedgeTakenCount(L); |
0 |
1361 |
const SCEV *BECount = SE->getBackedgeTakenCount(L); |
0 |
| 1362 |
if (PreAR && PreAR->getNoWrapFlags(WrapType) && |
0 |
1362 |
if (PreAR && PreAR->getNoWrapFlags(WrapType) && |
0 |
| 1363 |
!isa(BECount) && SE->isKnownPositive(BECount)) |
0 |
1363 |
!isa(BECount) && SE->isKnownPositive(BECount)) |
0 |
| 1364 |
return PreStart; |
0 |
1364 |
return PreStart; |
0 |
| 1365 |
|
--- |
1365 |
|
--- |
| 1366 |
// 2. Direct overflow check on the step operation's expression. |
--- |
1366 |
// 2. Direct overflow check on the step operation's expression. |
--- |
| 1367 |
unsigned BitWidth = SE->getTypeSizeInBits(AR->getType()); |
0 |
1367 |
unsigned BitWidth = SE->getTypeSizeInBits(AR->getType()); |
0 |
| 1368 |
Type *WideTy = IntegerType::get(SE->getContext(), BitWidth * 2); |
0 |
1368 |
Type *WideTy = IntegerType::get(SE->getContext(), BitWidth * 2); |
0 |
| 1369 |
const SCEV *OperandExtendedStart = |
--- |
1369 |
const SCEV *OperandExtendedStart = |
--- |
| 1370 |
SE->getAddExpr((SE->*GetExtendExpr)(PreStart, WideTy, Depth), |
0 |
1370 |
SE->getAddExpr((SE->*GetExtendExpr)(PreStart, WideTy, Depth), |
0 |
| 1371 |
(SE->*GetExtendExpr)(Step, WideTy, Depth)); |
0 |
1371 |
(SE->*GetExtendExpr)(Step, WideTy, Depth)); |
0 |
| 1372 |
if ((SE->*GetExtendExpr)(Start, WideTy, Depth) == OperandExtendedStart) { |
0 |
1372 |
if ((SE->*GetExtendExpr)(Start, WideTy, Depth) == OperandExtendedStart) { |
0 |
| 1373 |
if (PreAR && AR->getNoWrapFlags(WrapType)) { |
0 |
1373 |
if (PreAR && AR->getNoWrapFlags(WrapType)) { |
0 |
| 1374 |
// If we know `AR` == {`PreStart`+`Step`,+,`Step`} is `WrapType` (FlagNSW |
--- |
1374 |
// If we know `AR` == {`PreStart`+`Step`,+,`Step`} is `WrapType` (FlagNSW |
--- |
| 1375 |
// or FlagNUW) and that `PreStart` + `Step` is `WrapType` too, then |
--- |
1375 |
// or FlagNUW) and that `PreStart` + `Step` is `WrapType` too, then |
--- |
| 1376 |
// `PreAR` == {`PreStart`,+,`Step`} is also `WrapType`. Cache this fact. |
--- |
1376 |
// `PreAR` == {`PreStart`,+,`Step`} is also `WrapType`. Cache this fact. |
--- |
| 1377 |
SE->setNoWrapFlags(const_cast(PreAR), WrapType); |
0 |
1377 |
SE->setNoWrapFlags(const_cast(PreAR), WrapType); |
0 |
| 1378 |
} |
--- |
1378 |
} |
--- |
| 1379 |
return PreStart; |
0 |
1379 |
return PreStart; |
0 |
| 1380 |
} |
--- |
1380 |
} |
--- |
| 1381 |
|
--- |
1381 |
|
--- |
| 1382 |
// 3. Loop precondition. |
--- |
1382 |
// 3. Loop precondition. |
--- |
| 1383 |
ICmpInst::Predicate Pred; |
--- |
1383 |
ICmpInst::Predicate Pred; |
--- |
| 1384 |
const SCEV *OverflowLimit = |
--- |
1384 |
const SCEV *OverflowLimit = |
--- |
| 1385 |
ExtendOpTraits::getOverflowLimitForStep(Step, &Pred, SE); |
0 |
1385 |
ExtendOpTraits::getOverflowLimitForStep(Step, &Pred, SE); |
0 |
| 1386 |
|
--- |
1386 |
|
--- |
| 1387 |
if (OverflowLimit && |
0 |
1387 |
if (OverflowLimit && |
0 |
| 1388 |
SE->isLoopEntryGuardedByCond(L, Pred, PreStart, OverflowLimit)) |
0 |
1388 |
SE->isLoopEntryGuardedByCond(L, Pred, PreStart, OverflowLimit)) |
0 |
| 1389 |
return PreStart; |
0 |
1389 |
return PreStart; |
0 |
| 1390 |
|
--- |
1390 |
|
--- |
| 1391 |
return nullptr; |
0 |
1391 |
return nullptr; |
0 |
| 1392 |
} |
0 |
1392 |
} |
0 |
| 1393 |
|
--- |
1393 |
|
--- |
| 1394 |
// Get the normalized zero or sign extended expression for this AddRec's Start. |
--- |
1394 |
// Get the normalized zero or sign extended expression for this AddRec's Start. |
--- |
| 1395 |
template |
--- |
1395 |
template |
--- |
| 1396 |
static const SCEV *getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty, |
0 |
1396 |
static const SCEV *getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty, |
0 |
| 1397 |
ScalarEvolution *SE, |
--- |
1397 |
ScalarEvolution *SE, |
--- |
| 1398 |
unsigned Depth) { |
--- |
1398 |
unsigned Depth) { |
--- |
| 1399 |
auto GetExtendExpr = ExtendOpTraits::GetExtendExpr; |
0 |
1399 |
auto GetExtendExpr = ExtendOpTraits::GetExtendExpr; |
0 |
| 1400 |
|
--- |
1400 |
|
--- |
| 1401 |
const SCEV *PreStart = getPreStartForExtend(AR, Ty, SE, Depth); |
0 |
1401 |
const SCEV *PreStart = getPreStartForExtend(AR, Ty, SE, Depth); |
0 |
| 1402 |
if (!PreStart) |
0 |
1402 |
if (!PreStart) |
0 |
| 1403 |
return (SE->*GetExtendExpr)(AR->getStart(), Ty, Depth); |
0 |
1403 |
return (SE->*GetExtendExpr)(AR->getStart(), Ty, Depth); |
0 |
| 1404 |
|
--- |
1404 |
|
--- |
| 1405 |
return SE->getAddExpr((SE->*GetExtendExpr)(AR->getStepRecurrence(*SE), Ty, |
0 |
1405 |
return SE->getAddExpr((SE->*GetExtendExpr)(AR->getStepRecurrence(*SE), Ty, |
0 |
| 1406 |
Depth), |
--- |
1406 |
Depth), |
--- |
| 1407 |
(SE->*GetExtendExpr)(PreStart, Ty, Depth)); |
0 |
1407 |
(SE->*GetExtendExpr)(PreStart, Ty, Depth)); |
0 |
| 1408 |
} |
--- |
1408 |
} |
--- |
| 1409 |
|
--- |
1409 |
|
--- |
| 1410 |
// Try to prove away overflow by looking at "nearby" add recurrences. A |
--- |
1410 |
// Try to prove away overflow by looking at "nearby" add recurrences. A |
--- |
| 1411 |
// motivating example for this rule: if we know `{0,+,4}` is `ult` `-1` and it |
--- |
1411 |
// motivating example for this rule: if we know `{0,+,4}` is `ult` `-1` and it |
--- |
| 1412 |
// does not itself wrap then we can conclude that `{1,+,4}` is `nuw`. |
--- |
1412 |
// does not itself wrap then we can conclude that `{1,+,4}` is `nuw`. |
--- |
| 1413 |
// |
--- |
1413 |
// |
--- |
| 1414 |
// Formally: |
--- |
1414 |
// Formally: |
--- |
| 1415 |
// |
--- |
1415 |
// |
--- |
| 1416 |
// {S,+,X} == {S-T,+,X} + T |
--- |
1416 |
// {S,+,X} == {S-T,+,X} + T |
--- |
| 1417 |
// => Ext({S,+,X}) == Ext({S-T,+,X} + T) |
--- |
1417 |
// => Ext({S,+,X}) == Ext({S-T,+,X} + T) |
--- |
| 1418 |
// |
--- |
1418 |
// |
--- |
| 1419 |
// If ({S-T,+,X} + T) does not overflow ... (1) |
--- |
1419 |
// If ({S-T,+,X} + T) does not overflow ... (1) |
--- |
| 1420 |
// |
--- |
1420 |
// |
--- |
| 1421 |
// RHS == Ext({S-T,+,X} + T) == Ext({S-T,+,X}) + Ext(T) |
--- |
1421 |
// RHS == Ext({S-T,+,X} + T) == Ext({S-T,+,X}) + Ext(T) |
--- |
| 1422 |
// |
--- |
1422 |
// |
--- |
| 1423 |
// If {S-T,+,X} does not overflow ... (2) |
--- |
1423 |
// If {S-T,+,X} does not overflow ... (2) |
--- |
| 1424 |
// |
--- |
1424 |
// |
--- |
| 1425 |
// RHS == Ext({S-T,+,X}) + Ext(T) == {Ext(S-T),+,Ext(X)} + Ext(T) |
--- |
1425 |
// RHS == Ext({S-T,+,X}) + Ext(T) == {Ext(S-T),+,Ext(X)} + Ext(T) |
--- |
| 1426 |
// == {Ext(S-T)+Ext(T),+,Ext(X)} |
--- |
1426 |
// == {Ext(S-T)+Ext(T),+,Ext(X)} |
--- |
| 1427 |
// |
--- |
1427 |
// |
--- |
| 1428 |
// If (S-T)+T does not overflow ... (3) |
--- |
1428 |
// If (S-T)+T does not overflow ... (3) |
--- |
| 1429 |
// |
--- |
1429 |
// |
--- |
| 1430 |
// RHS == {Ext(S-T)+Ext(T),+,Ext(X)} == {Ext(S-T+T),+,Ext(X)} |
--- |
1430 |
// RHS == {Ext(S-T)+Ext(T),+,Ext(X)} == {Ext(S-T+T),+,Ext(X)} |
--- |
| 1431 |
// == {Ext(S),+,Ext(X)} == LHS |
--- |
1431 |
// == {Ext(S),+,Ext(X)} == LHS |
--- |
| 1432 |
// |
--- |
1432 |
// |
--- |
| 1433 |
// Thus, if (1), (2) and (3) are true for some T, then |
--- |
1433 |
// Thus, if (1), (2) and (3) are true for some T, then |
--- |
| 1434 |
// Ext({S,+,X}) == {Ext(S),+,Ext(X)} |
--- |
1434 |
// Ext({S,+,X}) == {Ext(S),+,Ext(X)} |
--- |
| 1435 |
// |
--- |
1435 |
// |
--- |
| 1436 |
// (3) is implied by (1) -- "(S-T)+T does not overflow" is simply "({S-T,+,X}+T) |
--- |
1436 |
// (3) is implied by (1) -- "(S-T)+T does not overflow" is simply "({S-T,+,X}+T) |
--- |
| 1437 |
// does not overflow" restricted to the 0th iteration. Therefore we only need |
--- |
1437 |
// does not overflow" restricted to the 0th iteration. Therefore we only need |
--- |
| 1438 |
// to check for (1) and (2). |
--- |
1438 |
// to check for (1) and (2). |
--- |
| 1439 |
// |
--- |
1439 |
// |
--- |
| 1440 |
// In the current context, S is `Start`, X is `Step`, Ext is `ExtendOpTy` and T |
--- |
1440 |
// In the current context, S is `Start`, X is `Step`, Ext is `ExtendOpTy` and T |
--- |
| 1441 |
// is `Delta` (defined below). |
--- |
1441 |
// is `Delta` (defined below). |
--- |
| 1442 |
template |
--- |
1442 |
template |
--- |
| 1443 |
bool ScalarEvolution::proveNoWrapByVaryingStart(const SCEV *Start, |
0 |
1443 |
bool ScalarEvolution::proveNoWrapByVaryingStart(const SCEV *Start, |
0 |
| 1444 |
const SCEV *Step, |
--- |
1444 |
const SCEV *Step, |
--- |
| 1445 |
const Loop *L) { |
--- |
1445 |
const Loop *L) { |
--- |
| 1446 |
auto WrapType = ExtendOpTraits::WrapType; |
0 |
1446 |
auto WrapType = ExtendOpTraits::WrapType; |
0 |
| 1447 |
|
--- |
1447 |
|
--- |
| 1448 |
// We restrict `Start` to a constant to prevent SCEV from spending too much |
--- |
1448 |
// We restrict `Start` to a constant to prevent SCEV from spending too much |
--- |
| 1449 |
// time here. It is correct (but more expensive) to continue with a |
--- |
1449 |
// time here. It is correct (but more expensive) to continue with a |
--- |
| 1450 |
// non-constant `Start` and do a general SCEV subtraction to compute |
--- |
1450 |
// non-constant `Start` and do a general SCEV subtraction to compute |
--- |
| 1451 |
// `PreStart` below. |
--- |
1451 |
// `PreStart` below. |
--- |
| 1452 |
const SCEVConstant *StartC = dyn_cast(Start); |
0 |
1452 |
const SCEVConstant *StartC = dyn_cast(Start); |
0 |
| 1453 |
if (!StartC) |
0 |
1453 |
if (!StartC) |
0 |
| 1454 |
return false; |
0 |
1454 |
return false; |
0 |
| 1455 |
|
--- |
1455 |
|
--- |
| 1456 |
APInt StartAI = StartC->getAPInt(); |
0 |
1456 |
APInt StartAI = StartC->getAPInt(); |
0 |
| 1457 |
|
--- |
1457 |
|
--- |
| 1458 |
for (unsigned Delta : {-2, -1, 1, 2}) { |
0 |
1458 |
for (unsigned Delta : {-2, -1, 1, 2}) { |
0 |
| 1459 |
const SCEV *PreStart = getConstant(StartAI - Delta); |
0 |
1459 |
const SCEV *PreStart = getConstant(StartAI - Delta); |
0 |
| 1460 |
|
--- |
1460 |
|
--- |
| 1461 |
FoldingSetNodeID ID; |
0 |
1461 |
FoldingSetNodeID ID; |
0 |
| 1462 |
ID.AddInteger(scAddRecExpr); |
0 |
1462 |
ID.AddInteger(scAddRecExpr); |
0 |
| 1463 |
ID.AddPointer(PreStart); |
0 |
1463 |
ID.AddPointer(PreStart); |
0 |
| 1464 |
ID.AddPointer(Step); |
0 |
1464 |
ID.AddPointer(Step); |
0 |
| 1465 |
ID.AddPointer(L); |
0 |
1465 |
ID.AddPointer(L); |
0 |
| 1466 |
void *IP = nullptr; |
0 |
1466 |
void *IP = nullptr; |
0 |
| 1467 |
const auto *PreAR = |
--- |
1467 |
const auto *PreAR = |
--- |
| 1468 |
static_cast(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
0 |
1468 |
static_cast(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
0 |
| 1469 |
|
--- |
1469 |
|
--- |
| 1470 |
// Give up if we don't already have the add recurrence we need because |
--- |
1470 |
// Give up if we don't already have the add recurrence we need because |
--- |
| 1471 |
// actually constructing an add recurrence is relatively expensive. |
--- |
1471 |
// actually constructing an add recurrence is relatively expensive. |
--- |
| 1472 |
if (PreAR && PreAR->getNoWrapFlags(WrapType)) { // proves (2) |
0 |
1472 |
if (PreAR && PreAR->getNoWrapFlags(WrapType)) { // proves (2) |
0 |
| 1473 |
const SCEV *DeltaS = getConstant(StartC->getType(), Delta); |
0 |
1473 |
const SCEV *DeltaS = getConstant(StartC->getType(), Delta); |
0 |
| 1474 |
ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE; |
0 |
1474 |
ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE; |
0 |
| 1475 |
const SCEV *Limit = ExtendOpTraits::getOverflowLimitForStep( |
0 |
1475 |
const SCEV *Limit = ExtendOpTraits::getOverflowLimitForStep( |
0 |
| 1476 |
DeltaS, &Pred, this); |
--- |
1476 |
DeltaS, &Pred, this); |
--- |
| 1477 |
if (Limit && isKnownPredicate(Pred, PreAR, Limit)) // proves (1) |
0 |
1477 |
if (Limit && isKnownPredicate(Pred, PreAR, Limit)) // proves (1) |
0 |
| 1478 |
return true; |
0 |
1478 |
return true; |
0 |
| 1479 |
} |
--- |
1479 |
} |
--- |
| 1480 |
} |
--- |
1480 |
} |
--- |
| 1481 |
|
--- |
1481 |
|
--- |
| 1482 |
return false; |
0 |
1482 |
return false; |
0 |
| 1483 |
} |
0 |
1483 |
} |
0 |
| 1484 |
|
--- |
1484 |
|
--- |
| 1485 |
// Finds an integer D for an expression (C + x + y + ...) such that the top |
--- |
1485 |
// Finds an integer D for an expression (C + x + y + ...) such that the top |
--- |
| 1486 |
// level addition in (D + (C - D + x + y + ...)) would not wrap (signed or |
--- |
1486 |
// level addition in (D + (C - D + x + y + ...)) would not wrap (signed or |
--- |
| 1487 |
// unsigned) and the number of trailing zeros of (C - D + x + y + ...) is |
--- |
1487 |
// unsigned) and the number of trailing zeros of (C - D + x + y + ...) is |
--- |
| 1488 |
// maximized, where C is the \p ConstantTerm, x, y, ... are arbitrary SCEVs, and |
--- |
1488 |
// maximized, where C is the \p ConstantTerm, x, y, ... are arbitrary SCEVs, and |
--- |
| 1489 |
// the (C + x + y + ...) expression is \p WholeAddExpr. |
--- |
1489 |
// the (C + x + y + ...) expression is \p WholeAddExpr. |
--- |
| 1490 |
static APInt extractConstantWithoutWrapping(ScalarEvolution &SE, |
0 |
1490 |
static APInt extractConstantWithoutWrapping(ScalarEvolution &SE, |
0 |
| 1491 |
const SCEVConstant *ConstantTerm, |
--- |
1491 |
const SCEVConstant *ConstantTerm, |
--- |
| 1492 |
const SCEVAddExpr *WholeAddExpr) { |
--- |
1492 |
const SCEVAddExpr *WholeAddExpr) { |
--- |
| 1493 |
const APInt &C = ConstantTerm->getAPInt(); |
0 |
1493 |
const APInt &C = ConstantTerm->getAPInt(); |
0 |
| 1494 |
const unsigned BitWidth = C.getBitWidth(); |
0 |
1494 |
const unsigned BitWidth = C.getBitWidth(); |
0 |
| 1495 |
// Find number of trailing zeros of (x + y + ...) w/o the C first: |
--- |
1495 |
// Find number of trailing zeros of (x + y + ...) w/o the C first: |
--- |
| 1496 |
uint32_t TZ = BitWidth; |
0 |
1496 |
uint32_t TZ = BitWidth; |
0 |
| 1497 |
for (unsigned I = 1, E = WholeAddExpr->getNumOperands(); I < E && TZ; ++I) |
0 |
1497 |
for (unsigned I = 1, E = WholeAddExpr->getNumOperands(); I < E && TZ; ++I) |
0 |
| 1498 |
TZ = std::min(TZ, SE.getMinTrailingZeros(WholeAddExpr->getOperand(I))); |
0 |
1498 |
TZ = std::min(TZ, SE.getMinTrailingZeros(WholeAddExpr->getOperand(I))); |
0 |
| 1499 |
if (TZ) { |
0 |
1499 |
if (TZ) { |
0 |
| 1500 |
// Set D to be as many least significant bits of C as possible while still |
--- |
1500 |
// Set D to be as many least significant bits of C as possible while still |
--- |
| 1501 |
// guaranteeing that adding D to (C - D + x + y + ...) won't cause a wrap: |
--- |
1501 |
// guaranteeing that adding D to (C - D + x + y + ...) won't cause a wrap: |
--- |
| 1502 |
return TZ < BitWidth ? C.trunc(TZ).zext(BitWidth) : C; |
0 |
1502 |
return TZ < BitWidth ? C.trunc(TZ).zext(BitWidth) : C; |
0 |
| 1503 |
} |
--- |
1503 |
} |
--- |
| 1504 |
return APInt(BitWidth, 0); |
0 |
1504 |
return APInt(BitWidth, 0); |
0 |
| 1505 |
} |
--- |
1505 |
} |
--- |
| 1506 |
|
--- |
1506 |
|
--- |
| 1507 |
// Finds an integer D for an affine AddRec expression {C,+,x} such that the top |
--- |
1507 |
// Finds an integer D for an affine AddRec expression {C,+,x} such that the top |
--- |
| 1508 |
// level addition in (D + {C-D,+,x}) would not wrap (signed or unsigned) and the |
--- |
1508 |
// level addition in (D + {C-D,+,x}) would not wrap (signed or unsigned) and the |
--- |
| 1509 |
// number of trailing zeros of (C - D + x * n) is maximized, where C is the \p |
--- |
1509 |
// number of trailing zeros of (C - D + x * n) is maximized, where C is the \p |
--- |
| 1510 |
// ConstantStart, x is an arbitrary \p Step, and n is the loop trip count. |
--- |
1510 |
// ConstantStart, x is an arbitrary \p Step, and n is the loop trip count. |
--- |
| 1511 |
static APInt extractConstantWithoutWrapping(ScalarEvolution &SE, |
0 |
1511 |
static APInt extractConstantWithoutWrapping(ScalarEvolution &SE, |
0 |
| 1512 |
const APInt &ConstantStart, |
--- |
1512 |
const APInt &ConstantStart, |
--- |
| 1513 |
const SCEV *Step) { |
--- |
1513 |
const SCEV *Step) { |
--- |
| 1514 |
const unsigned BitWidth = ConstantStart.getBitWidth(); |
0 |
1514 |
const unsigned BitWidth = ConstantStart.getBitWidth(); |
0 |
| 1515 |
const uint32_t TZ = SE.getMinTrailingZeros(Step); |
0 |
1515 |
const uint32_t TZ = SE.getMinTrailingZeros(Step); |
0 |
| 1516 |
if (TZ) |
0 |
1516 |
if (TZ) |
0 |
| 1517 |
return TZ < BitWidth ? ConstantStart.trunc(TZ).zext(BitWidth) |
0 |
1517 |
return TZ < BitWidth ? ConstantStart.trunc(TZ).zext(BitWidth) |
0 |
| 1518 |
: ConstantStart; |
0 |
1518 |
: ConstantStart; |
0 |
| 1519 |
return APInt(BitWidth, 0); |
0 |
1519 |
return APInt(BitWidth, 0); |
0 |
| 1520 |
} |
--- |
1520 |
} |
--- |
| 1521 |
|
--- |
1521 |
|
--- |
| 1522 |
static void insertFoldCacheEntry( |
0 |
1522 |
static void insertFoldCacheEntry( |
0 |
| 1523 |
const ScalarEvolution::FoldID &ID, const SCEV *S, |
--- |
1523 |
const ScalarEvolution::FoldID &ID, const SCEV *S, |
--- |
| 1524 |
DenseMap &FoldCache, |
--- |
1524 |
DenseMap &FoldCache, |
--- |
| 1525 |
DenseMap> |
--- |
1525 |
DenseMap> |
--- |
| 1526 |
&FoldCacheUser) { |
--- |
1526 |
&FoldCacheUser) { |
--- |
| 1527 |
auto I = FoldCache.insert({ID, S}); |
0 |
1527 |
auto I = FoldCache.insert({ID, S}); |
0 |
| 1528 |
if (!I.second) { |
0 |
1528 |
if (!I.second) { |
0 |
| 1529 |
// Remove FoldCacheUser entry for ID when replacing an existing FoldCache |
--- |
1529 |
// Remove FoldCacheUser entry for ID when replacing an existing FoldCache |
--- |
| 1530 |
// entry. |
--- |
1530 |
// entry. |
--- |
| 1531 |
auto &UserIDs = FoldCacheUser[I.first->second]; |
0 |
1531 |
auto &UserIDs = FoldCacheUser[I.first->second]; |
0 |
| 1532 |
assert(count(UserIDs, ID) == 1 && "unexpected duplicates in UserIDs"); |
0 |
1532 |
assert(count(UserIDs, ID) == 1 && "unexpected duplicates in UserIDs"); |
0 |
| 1533 |
for (unsigned I = 0; I != UserIDs.size(); ++I) |
0 |
1533 |
for (unsigned I = 0; I != UserIDs.size(); ++I) |
0 |
| 1534 |
if (UserIDs[I] == ID) { |
0 |
1534 |
if (UserIDs[I] == ID) { |
0 |
| 1535 |
std::swap(UserIDs[I], UserIDs.back()); |
0 |
1535 |
std::swap(UserIDs[I], UserIDs.back()); |
0 |
| 1536 |
break; |
0 |
1536 |
break; |
0 |
| 1537 |
} |
--- |
1537 |
} |
--- |
| 1538 |
UserIDs.pop_back(); |
0 |
1538 |
UserIDs.pop_back(); |
0 |
| 1539 |
I.first->second = S; |
0 |
1539 |
I.first->second = S; |
0 |
| 1540 |
} |
--- |
1540 |
} |
--- |
| 1541 |
auto R = FoldCacheUser.insert({S, {}}); |
0 |
1541 |
auto R = FoldCacheUser.insert({S, {}}); |
0 |
| 1542 |
R.first->second.push_back(ID); |
0 |
1542 |
R.first->second.push_back(ID); |
0 |
| 1543 |
} |
0 |
1543 |
} |
0 |
| 1544 |
|
--- |
1544 |
|
--- |
| 1545 |
const SCEV * |
--- |
1545 |
const SCEV * |
--- |
| 1546 |
ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) { |
0 |
1546 |
ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) { |
0 |
| 1547 |
assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
0 |
1547 |
assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
0 |
| 1548 |
"This is not an extending conversion!"); |
--- |
1548 |
"This is not an extending conversion!"); |
--- |
| 1549 |
assert(isSCEVable(Ty) && |
0 |
1549 |
assert(isSCEVable(Ty) && |
0 |
| 1550 |
"This is not a conversion to a SCEVable type!"); |
--- |
1550 |
"This is not a conversion to a SCEVable type!"); |
--- |
| 1551 |
assert(!Op->getType()->isPointerTy() && "Can't extend pointer!"); |
0 |
1551 |
assert(!Op->getType()->isPointerTy() && "Can't extend pointer!"); |
0 |
| 1552 |
Ty = getEffectiveSCEVType(Ty); |
0 |
1552 |
Ty = getEffectiveSCEVType(Ty); |
0 |
| 1553 |
|
--- |
1553 |
|
--- |
| 1554 |
FoldID ID(scZeroExtend, Op, Ty); |
0 |
1554 |
FoldID ID(scZeroExtend, Op, Ty); |
0 |
| 1555 |
auto Iter = FoldCache.find(ID); |
0 |
1555 |
auto Iter = FoldCache.find(ID); |
0 |
| 1556 |
if (Iter != FoldCache.end()) |
0 |
1556 |
if (Iter != FoldCache.end()) |
0 |
| 1557 |
return Iter->second; |
0 |
1557 |
return Iter->second; |
0 |
| 1558 |
|
--- |
1558 |
|
--- |
| 1559 |
const SCEV *S = getZeroExtendExprImpl(Op, Ty, Depth); |
0 |
1559 |
const SCEV *S = getZeroExtendExprImpl(Op, Ty, Depth); |
0 |
| 1560 |
if (!isa(S)) |
0 |
1560 |
if (!isa(S)) |
0 |
| 1561 |
insertFoldCacheEntry(ID, S, FoldCache, FoldCacheUser); |
0 |
1561 |
insertFoldCacheEntry(ID, S, FoldCache, FoldCacheUser); |
0 |
| 1562 |
return S; |
0 |
1562 |
return S; |
0 |
| 1563 |
} |
--- |
1563 |
} |
--- |
| 1564 |
|
--- |
1564 |
|
--- |
| 1565 |
const SCEV *ScalarEvolution::getZeroExtendExprImpl(const SCEV *Op, Type *Ty, |
0 |
1565 |
const SCEV *ScalarEvolution::getZeroExtendExprImpl(const SCEV *Op, Type *Ty, |
0 |
| 1566 |
unsigned Depth) { |
--- |
1566 |
unsigned Depth) { |
--- |
| 1567 |
assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
0 |
1567 |
assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
0 |
| 1568 |
"This is not an extending conversion!"); |
--- |
1568 |
"This is not an extending conversion!"); |
--- |
| 1569 |
assert(isSCEVable(Ty) && "This is not a conversion to a SCEVable type!"); |
0 |
1569 |
assert(isSCEVable(Ty) && "This is not a conversion to a SCEVable type!"); |
0 |
| 1570 |
assert(!Op->getType()->isPointerTy() && "Can't extend pointer!"); |
0 |
1570 |
assert(!Op->getType()->isPointerTy() && "Can't extend pointer!"); |
0 |
| 1571 |
|
--- |
1571 |
|
--- |
| 1572 |
// Fold if the operand is constant. |
--- |
1572 |
// Fold if the operand is constant. |
--- |
| 1573 |
if (const SCEVConstant *SC = dyn_cast(Op)) |
0 |
1573 |
if (const SCEVConstant *SC = dyn_cast(Op)) |
0 |
| 1574 |
return getConstant( |
0 |
1574 |
return getConstant( |
0 |
| 1575 |
cast(ConstantExpr::getZExt(SC->getValue(), Ty))); |
0 |
1575 |
cast(ConstantExpr::getZExt(SC->getValue(), Ty))); |
0 |
| 1576 |
|
--- |
1576 |
|
--- |
| 1577 |
// zext(zext(x)) --> zext(x) |
--- |
1577 |
// zext(zext(x)) --> zext(x) |
--- |
| 1578 |
if (const SCEVZeroExtendExpr *SZ = dyn_cast(Op)) |
0 |
1578 |
if (const SCEVZeroExtendExpr *SZ = dyn_cast(Op)) |
0 |
| 1579 |
return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1); |
0 |
1579 |
return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1); |
0 |
| 1580 |
|
--- |
1580 |
|
--- |
| 1581 |
// Before doing any expensive analysis, check to see if we've already |
--- |
1581 |
// Before doing any expensive analysis, check to see if we've already |
--- |
| 1582 |
// computed a SCEV for this Op and Ty. |
--- |
1582 |
// computed a SCEV for this Op and Ty. |
--- |
| 1583 |
FoldingSetNodeID ID; |
0 |
1583 |
FoldingSetNodeID ID; |
0 |
| 1584 |
ID.AddInteger(scZeroExtend); |
0 |
1584 |
ID.AddInteger(scZeroExtend); |
0 |
| 1585 |
ID.AddPointer(Op); |
0 |
1585 |
ID.AddPointer(Op); |
0 |
| 1586 |
ID.AddPointer(Ty); |
0 |
1586 |
ID.AddPointer(Ty); |
0 |
| 1587 |
void *IP = nullptr; |
0 |
1587 |
void *IP = nullptr; |
0 |
| 1588 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
0 |
1588 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
0 |
| 1589 |
if (Depth > MaxCastDepth) { |
0 |
1589 |
if (Depth > MaxCastDepth) { |
0 |
| 1590 |
SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator), |
0 |
1590 |
SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator), |
0 |
| 1591 |
Op, Ty); |
0 |
1591 |
Op, Ty); |
0 |
| 1592 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
1592 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 1593 |
registerUser(S, Op); |
0 |
1593 |
registerUser(S, Op); |
0 |
| 1594 |
return S; |
0 |
1594 |
return S; |
0 |
| 1595 |
} |
--- |
1595 |
} |
--- |
| 1596 |
|
--- |
1596 |
|
--- |
| 1597 |
// zext(trunc(x)) --> zext(x) or x or trunc(x) |
--- |
1597 |
// zext(trunc(x)) --> zext(x) or x or trunc(x) |
--- |
| 1598 |
if (const SCEVTruncateExpr *ST = dyn_cast(Op)) { |
0 |
1598 |
if (const SCEVTruncateExpr *ST = dyn_cast(Op)) { |
0 |
| 1599 |
// It's possible the bits taken off by the truncate were all zero bits. If |
--- |
1599 |
// It's possible the bits taken off by the truncate were all zero bits. If |
--- |
| 1600 |
// so, we should be able to simplify this further. |
--- |
1600 |
// so, we should be able to simplify this further. |
--- |
| 1601 |
const SCEV *X = ST->getOperand(); |
0 |
1601 |
const SCEV *X = ST->getOperand(); |
0 |
| 1602 |
ConstantRange CR = getUnsignedRange(X); |
0 |
1602 |
ConstantRange CR = getUnsignedRange(X); |
0 |
| 1603 |
unsigned TruncBits = getTypeSizeInBits(ST->getType()); |
0 |
1603 |
unsigned TruncBits = getTypeSizeInBits(ST->getType()); |
0 |
| 1604 |
unsigned NewBits = getTypeSizeInBits(Ty); |
0 |
1604 |
unsigned NewBits = getTypeSizeInBits(Ty); |
0 |
| 1605 |
if (CR.truncate(TruncBits).zeroExtend(NewBits).contains( |
0 |
1605 |
if (CR.truncate(TruncBits).zeroExtend(NewBits).contains( |
0 |
| 1606 |
CR.zextOrTrunc(NewBits))) |
0 |
1606 |
CR.zextOrTrunc(NewBits))) |
0 |
| 1607 |
return getTruncateOrZeroExtend(X, Ty, Depth); |
0 |
1607 |
return getTruncateOrZeroExtend(X, Ty, Depth); |
0 |
| 1608 |
} |
0 |
1608 |
} |
0 |
| 1609 |
|
--- |
1609 |
|
--- |
| 1610 |
// If the input value is a chrec scev, and we can prove that the value |
--- |
1610 |
// If the input value is a chrec scev, and we can prove that the value |
--- |
| 1611 |
// did not overflow the old, smaller, value, we can zero extend all of the |
--- |
1611 |
// did not overflow the old, smaller, value, we can zero extend all of the |
--- |
| 1612 |
// operands (often constants). This allows analysis of something like |
--- |
1612 |
// operands (often constants). This allows analysis of something like |
--- |
| 1613 |
// this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } |
--- |
1613 |
// this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } |
--- |
| 1614 |
if (const SCEVAddRecExpr *AR = dyn_cast(Op)) |
0 |
1614 |
if (const SCEVAddRecExpr *AR = dyn_cast(Op)) |
0 |
| 1615 |
if (AR->isAffine()) { |
0 |
1615 |
if (AR->isAffine()) { |
0 |
| 1616 |
const SCEV *Start = AR->getStart(); |
0 |
1616 |
const SCEV *Start = AR->getStart(); |
0 |
| 1617 |
const SCEV *Step = AR->getStepRecurrence(*this); |
0 |
1617 |
const SCEV *Step = AR->getStepRecurrence(*this); |
0 |
| 1618 |
unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
0 |
1618 |
unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
0 |
| 1619 |
const Loop *L = AR->getLoop(); |
0 |
1619 |
const Loop *L = AR->getLoop(); |
0 |
| 1620 |
|
--- |
1620 |
|
--- |
| 1621 |
// If we have special knowledge that this addrec won't overflow, |
--- |
1621 |
// If we have special knowledge that this addrec won't overflow, |
--- |
| 1622 |
// we don't need to do any further analysis. |
--- |
1622 |
// we don't need to do any further analysis. |
--- |
| 1623 |
if (AR->hasNoUnsignedWrap()) { |
0 |
1623 |
if (AR->hasNoUnsignedWrap()) { |
0 |
| 1624 |
Start = |
--- |
1624 |
Start = |
--- |
| 1625 |
getExtendAddRecStart(AR, Ty, this, Depth + 1); |
0 |
1625 |
getExtendAddRecStart(AR, Ty, this, Depth + 1); |
0 |
| 1626 |
Step = getZeroExtendExpr(Step, Ty, Depth + 1); |
0 |
1626 |
Step = getZeroExtendExpr(Step, Ty, Depth + 1); |
0 |
| 1627 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
1627 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
| 1628 |
} |
--- |
1628 |
} |
--- |
| 1629 |
|
--- |
1629 |
|
--- |
| 1630 |
// Check whether the backedge-taken count is SCEVCouldNotCompute. |
--- |
1630 |
// Check whether the backedge-taken count is SCEVCouldNotCompute. |
--- |
| 1631 |
// Note that this serves two purposes: It filters out loops that are |
--- |
1631 |
// Note that this serves two purposes: It filters out loops that are |
--- |
| 1632 |
// simply not analyzable, and it covers the case where this code is |
--- |
1632 |
// simply not analyzable, and it covers the case where this code is |
--- |
| 1633 |
// being called from within backedge-taken count analysis, such that |
--- |
1633 |
// being called from within backedge-taken count analysis, such that |
--- |
| 1634 |
// attempting to ask for the backedge-taken count would likely result |
--- |
1634 |
// attempting to ask for the backedge-taken count would likely result |
--- |
| 1635 |
// in infinite recursion. In the later case, the analysis code will |
--- |
1635 |
// in infinite recursion. In the later case, the analysis code will |
--- |
| 1636 |
// cope with a conservative value, and it will take care to purge |
--- |
1636 |
// cope with a conservative value, and it will take care to purge |
--- |
| 1637 |
// that value once it has finished. |
--- |
1637 |
// that value once it has finished. |
--- |
| 1638 |
const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L); |
0 |
1638 |
const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L); |
0 |
| 1639 |
if (!isa(MaxBECount)) { |
0 |
1639 |
if (!isa(MaxBECount)) { |
0 |
| 1640 |
// Manually compute the final value for AR, checking for overflow. |
--- |
1640 |
// Manually compute the final value for AR, checking for overflow. |
--- |
| 1641 |
|
--- |
1641 |
|
--- |
| 1642 |
// Check whether the backedge-taken count can be losslessly casted to |
--- |
1642 |
// Check whether the backedge-taken count can be losslessly casted to |
--- |
| 1643 |
// the addrec's type. The count is always unsigned. |
--- |
1643 |
// the addrec's type. The count is always unsigned. |
--- |
| 1644 |
const SCEV *CastedMaxBECount = |
--- |
1644 |
const SCEV *CastedMaxBECount = |
--- |
| 1645 |
getTruncateOrZeroExtend(MaxBECount, Start->getType(), Depth); |
0 |
1645 |
getTruncateOrZeroExtend(MaxBECount, Start->getType(), Depth); |
0 |
| 1646 |
const SCEV *RecastedMaxBECount = getTruncateOrZeroExtend( |
0 |
1646 |
const SCEV *RecastedMaxBECount = getTruncateOrZeroExtend( |
0 |
| 1647 |
CastedMaxBECount, MaxBECount->getType(), Depth); |
--- |
1647 |
CastedMaxBECount, MaxBECount->getType(), Depth); |
--- |
| 1648 |
if (MaxBECount == RecastedMaxBECount) { |
0 |
1648 |
if (MaxBECount == RecastedMaxBECount) { |
0 |
| 1649 |
Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
0 |
1649 |
Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
0 |
| 1650 |
// Check whether Start+Step*MaxBECount has no unsigned overflow. |
--- |
1650 |
// Check whether Start+Step*MaxBECount has no unsigned overflow. |
--- |
| 1651 |
const SCEV *ZMul = getMulExpr(CastedMaxBECount, Step, |
0 |
1651 |
const SCEV *ZMul = getMulExpr(CastedMaxBECount, Step, |
0 |
| 1652 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
1652 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
| 1653 |
const SCEV *ZAdd = getZeroExtendExpr(getAddExpr(Start, ZMul, |
0 |
1653 |
const SCEV *ZAdd = getZeroExtendExpr(getAddExpr(Start, ZMul, |
0 |
| 1654 |
SCEV::FlagAnyWrap, |
--- |
1654 |
SCEV::FlagAnyWrap, |
--- |
| 1655 |
Depth + 1), |
--- |
1655 |
Depth + 1), |
--- |
| 1656 |
WideTy, Depth + 1); |
--- |
1656 |
WideTy, Depth + 1); |
--- |
| 1657 |
const SCEV *WideStart = getZeroExtendExpr(Start, WideTy, Depth + 1); |
0 |
1657 |
const SCEV *WideStart = getZeroExtendExpr(Start, WideTy, Depth + 1); |
0 |
| 1658 |
const SCEV *WideMaxBECount = |
--- |
1658 |
const SCEV *WideMaxBECount = |
--- |
| 1659 |
getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1); |
0 |
1659 |
getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1); |
0 |
| 1660 |
const SCEV *OperandExtendedAdd = |
--- |
1660 |
const SCEV *OperandExtendedAdd = |
--- |
| 1661 |
getAddExpr(WideStart, |
0 |
1661 |
getAddExpr(WideStart, |
0 |
| 1662 |
getMulExpr(WideMaxBECount, |
--- |
1662 |
getMulExpr(WideMaxBECount, |
--- |
| 1663 |
getZeroExtendExpr(Step, WideTy, Depth + 1), |
--- |
1663 |
getZeroExtendExpr(Step, WideTy, Depth + 1), |
--- |
| 1664 |
SCEV::FlagAnyWrap, Depth + 1), |
--- |
1664 |
SCEV::FlagAnyWrap, Depth + 1), |
--- |
| 1665 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
1665 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
| 1666 |
if (ZAdd == OperandExtendedAdd) { |
0 |
1666 |
if (ZAdd == OperandExtendedAdd) { |
0 |
| 1667 |
// Cache knowledge of AR NUW, which is propagated to this AddRec. |
--- |
1667 |
// Cache knowledge of AR NUW, which is propagated to this AddRec. |
--- |
| 1668 |
setNoWrapFlags(const_cast(AR), SCEV::FlagNUW); |
0 |
1668 |
setNoWrapFlags(const_cast(AR), SCEV::FlagNUW); |
0 |
| 1669 |
// Return the expression with the addrec on the outside. |
--- |
1669 |
// Return the expression with the addrec on the outside. |
--- |
| 1670 |
Start = getExtendAddRecStart(AR, Ty, this, |
0 |
1670 |
Start = getExtendAddRecStart(AR, Ty, this, |
0 |
| 1671 |
Depth + 1); |
--- |
1671 |
Depth + 1); |
--- |
| 1672 |
Step = getZeroExtendExpr(Step, Ty, Depth + 1); |
0 |
1672 |
Step = getZeroExtendExpr(Step, Ty, Depth + 1); |
0 |
| 1673 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
1673 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
| 1674 |
} |
--- |
1674 |
} |
--- |
| 1675 |
// Similar to above, only this time treat the step value as signed. |
--- |
1675 |
// Similar to above, only this time treat the step value as signed. |
--- |
| 1676 |
// This covers loops that count down. |
--- |
1676 |
// This covers loops that count down. |
--- |
| 1677 |
OperandExtendedAdd = |
--- |
1677 |
OperandExtendedAdd = |
--- |
| 1678 |
getAddExpr(WideStart, |
0 |
1678 |
getAddExpr(WideStart, |
0 |
| 1679 |
getMulExpr(WideMaxBECount, |
--- |
1679 |
getMulExpr(WideMaxBECount, |
--- |
| 1680 |
getSignExtendExpr(Step, WideTy, Depth + 1), |
--- |
1680 |
getSignExtendExpr(Step, WideTy, Depth + 1), |
--- |
| 1681 |
SCEV::FlagAnyWrap, Depth + 1), |
--- |
1681 |
SCEV::FlagAnyWrap, Depth + 1), |
--- |
| 1682 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
1682 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
| 1683 |
if (ZAdd == OperandExtendedAdd) { |
0 |
1683 |
if (ZAdd == OperandExtendedAdd) { |
0 |
| 1684 |
// Cache knowledge of AR NW, which is propagated to this AddRec. |
--- |
1684 |
// Cache knowledge of AR NW, which is propagated to this AddRec. |
--- |
| 1685 |
// Negative step causes unsigned wrap, but it still can't self-wrap. |
--- |
1685 |
// Negative step causes unsigned wrap, but it still can't self-wrap. |
--- |
| 1686 |
setNoWrapFlags(const_cast(AR), SCEV::FlagNW); |
0 |
1686 |
setNoWrapFlags(const_cast(AR), SCEV::FlagNW); |
0 |
| 1687 |
// Return the expression with the addrec on the outside. |
--- |
1687 |
// Return the expression with the addrec on the outside. |
--- |
| 1688 |
Start = getExtendAddRecStart(AR, Ty, this, |
0 |
1688 |
Start = getExtendAddRecStart(AR, Ty, this, |
0 |
| 1689 |
Depth + 1); |
--- |
1689 |
Depth + 1); |
--- |
| 1690 |
Step = getSignExtendExpr(Step, Ty, Depth + 1); |
0 |
1690 |
Step = getSignExtendExpr(Step, Ty, Depth + 1); |
0 |
| 1691 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
1691 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
| 1692 |
} |
--- |
1692 |
} |
--- |
| 1693 |
} |
--- |
1693 |
} |
--- |
| 1694 |
} |
--- |
1694 |
} |
--- |
| 1695 |
|
--- |
1695 |
|
--- |
| 1696 |
// Normally, in the cases we can prove no-overflow via a |
--- |
1696 |
// Normally, in the cases we can prove no-overflow via a |
--- |
| 1697 |
// backedge guarding condition, we can also compute a backedge |
--- |
1697 |
// backedge guarding condition, we can also compute a backedge |
--- |
| 1698 |
// taken count for the loop. The exceptions are assumptions and |
--- |
1698 |
// taken count for the loop. The exceptions are assumptions and |
--- |
| 1699 |
// guards present in the loop -- SCEV is not great at exploiting |
--- |
1699 |
// guards present in the loop -- SCEV is not great at exploiting |
--- |
| 1700 |
// these to compute max backedge taken counts, but can still use |
--- |
1700 |
// these to compute max backedge taken counts, but can still use |
--- |
| 1701 |
// these to prove lack of overflow. Use this fact to avoid |
--- |
1701 |
// these to prove lack of overflow. Use this fact to avoid |
--- |
| 1702 |
// doing extra work that may not pay off. |
--- |
1702 |
// doing extra work that may not pay off. |
--- |
| 1703 |
if (!isa(MaxBECount) || HasGuards || |
0 |
1703 |
if (!isa(MaxBECount) || HasGuards || |
0 |
| 1704 |
!AC.assumptions().empty()) { |
0 |
1704 |
!AC.assumptions().empty()) { |
0 |
| 1705 |
|
--- |
1705 |
|
--- |
| 1706 |
auto NewFlags = proveNoUnsignedWrapViaInduction(AR); |
0 |
1706 |
auto NewFlags = proveNoUnsignedWrapViaInduction(AR); |
0 |
| 1707 |
setNoWrapFlags(const_cast(AR), NewFlags); |
0 |
1707 |
setNoWrapFlags(const_cast(AR), NewFlags); |
0 |
| 1708 |
if (AR->hasNoUnsignedWrap()) { |
0 |
1708 |
if (AR->hasNoUnsignedWrap()) { |
0 |
| 1709 |
// Same as nuw case above - duplicated here to avoid a compile time |
--- |
1709 |
// Same as nuw case above - duplicated here to avoid a compile time |
--- |
| 1710 |
// issue. It's not clear that the order of checks does matter, but |
--- |
1710 |
// issue. It's not clear that the order of checks does matter, but |
--- |
| 1711 |
// it's one of two issue possible causes for a change which was |
--- |
1711 |
// it's one of two issue possible causes for a change which was |
--- |
| 1712 |
// reverted. Be conservative for the moment. |
--- |
1712 |
// reverted. Be conservative for the moment. |
--- |
| 1713 |
Start = |
--- |
1713 |
Start = |
--- |
| 1714 |
getExtendAddRecStart(AR, Ty, this, Depth + 1); |
0 |
1714 |
getExtendAddRecStart(AR, Ty, this, Depth + 1); |
0 |
| 1715 |
Step = getZeroExtendExpr(Step, Ty, Depth + 1); |
0 |
1715 |
Step = getZeroExtendExpr(Step, Ty, Depth + 1); |
0 |
| 1716 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
1716 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
| 1717 |
} |
--- |
1717 |
} |
--- |
| 1718 |
|
--- |
1718 |
|
--- |
| 1719 |
// For a negative step, we can extend the operands iff doing so only |
--- |
1719 |
// For a negative step, we can extend the operands iff doing so only |
--- |
| 1720 |
// traverses values in the range zext([0,UINT_MAX]). |
--- |
1720 |
// traverses values in the range zext([0,UINT_MAX]). |
--- |
| 1721 |
if (isKnownNegative(Step)) { |
0 |
1721 |
if (isKnownNegative(Step)) { |
0 |
| 1722 |
const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) - |
0 |
1722 |
const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) - |
0 |
| 1723 |
getSignedRangeMin(Step)); |
0 |
1723 |
getSignedRangeMin(Step)); |
0 |
| 1724 |
if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) || |
0 |
1724 |
if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) || |
0 |
| 1725 |
isKnownOnEveryIteration(ICmpInst::ICMP_UGT, AR, N)) { |
0 |
1725 |
isKnownOnEveryIteration(ICmpInst::ICMP_UGT, AR, N)) { |
0 |
| 1726 |
// Cache knowledge of AR NW, which is propagated to this |
--- |
1726 |
// Cache knowledge of AR NW, which is propagated to this |
--- |
| 1727 |
// AddRec. Negative step causes unsigned wrap, but it |
--- |
1727 |
// AddRec. Negative step causes unsigned wrap, but it |
--- |
| 1728 |
// still can't self-wrap. |
--- |
1728 |
// still can't self-wrap. |
--- |
| 1729 |
setNoWrapFlags(const_cast(AR), SCEV::FlagNW); |
0 |
1729 |
setNoWrapFlags(const_cast(AR), SCEV::FlagNW); |
0 |
| 1730 |
// Return the expression with the addrec on the outside. |
--- |
1730 |
// Return the expression with the addrec on the outside. |
--- |
| 1731 |
Start = getExtendAddRecStart(AR, Ty, this, |
0 |
1731 |
Start = getExtendAddRecStart(AR, Ty, this, |
0 |
| 1732 |
Depth + 1); |
--- |
1732 |
Depth + 1); |
--- |
| 1733 |
Step = getSignExtendExpr(Step, Ty, Depth + 1); |
0 |
1733 |
Step = getSignExtendExpr(Step, Ty, Depth + 1); |
0 |
| 1734 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
1734 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
| 1735 |
} |
--- |
1735 |
} |
--- |
| 1736 |
} |
--- |
1736 |
} |
--- |
| 1737 |
} |
--- |
1737 |
} |
--- |
| 1738 |
|
--- |
1738 |
|
--- |
| 1739 |
// zext({C,+,Step}) --> (zext(D) + zext({C-D,+,Step})) |
--- |
1739 |
// zext({C,+,Step}) --> (zext(D) + zext({C-D,+,Step})) |
--- |
| 1740 |
// if D + (C - D + Step * n) could be proven to not unsigned wrap |
--- |
1740 |
// if D + (C - D + Step * n) could be proven to not unsigned wrap |
--- |
| 1741 |
// where D maximizes the number of trailing zeros of (C - D + Step * n) |
--- |
1741 |
// where D maximizes the number of trailing zeros of (C - D + Step * n) |
--- |
| 1742 |
if (const auto *SC = dyn_cast(Start)) { |
0 |
1742 |
if (const auto *SC = dyn_cast(Start)) { |
0 |
| 1743 |
const APInt &C = SC->getAPInt(); |
0 |
1743 |
const APInt &C = SC->getAPInt(); |
0 |
| 1744 |
const APInt &D = extractConstantWithoutWrapping(*this, C, Step); |
0 |
1744 |
const APInt &D = extractConstantWithoutWrapping(*this, C, Step); |
0 |
| 1745 |
if (D != 0) { |
0 |
1745 |
if (D != 0) { |
0 |
| 1746 |
const SCEV *SZExtD = getZeroExtendExpr(getConstant(D), Ty, Depth); |
0 |
1746 |
const SCEV *SZExtD = getZeroExtendExpr(getConstant(D), Ty, Depth); |
0 |
| 1747 |
const SCEV *SResidual = |
--- |
1747 |
const SCEV *SResidual = |
--- |
| 1748 |
getAddRecExpr(getConstant(C - D), Step, L, AR->getNoWrapFlags()); |
0 |
1748 |
getAddRecExpr(getConstant(C - D), Step, L, AR->getNoWrapFlags()); |
0 |
| 1749 |
const SCEV *SZExtR = getZeroExtendExpr(SResidual, Ty, Depth + 1); |
0 |
1749 |
const SCEV *SZExtR = getZeroExtendExpr(SResidual, Ty, Depth + 1); |
0 |
| 1750 |
return getAddExpr(SZExtD, SZExtR, |
0 |
1750 |
return getAddExpr(SZExtD, SZExtR, |
0 |
| 1751 |
(SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW), |
--- |
1751 |
(SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW), |
--- |
| 1752 |
Depth + 1); |
0 |
1752 |
Depth + 1); |
0 |
| 1753 |
} |
--- |
1753 |
} |
--- |
| 1754 |
} |
0 |
1754 |
} |
0 |
| 1755 |
|
--- |
1755 |
|
--- |
| 1756 |
if (proveNoWrapByVaryingStart(Start, Step, L)) { |
0 |
1756 |
if (proveNoWrapByVaryingStart(Start, Step, L)) { |
0 |
| 1757 |
setNoWrapFlags(const_cast(AR), SCEV::FlagNUW); |
0 |
1757 |
setNoWrapFlags(const_cast(AR), SCEV::FlagNUW); |
0 |
| 1758 |
Start = |
--- |
1758 |
Start = |
--- |
| 1759 |
getExtendAddRecStart(AR, Ty, this, Depth + 1); |
0 |
1759 |
getExtendAddRecStart(AR, Ty, this, Depth + 1); |
0 |
| 1760 |
Step = getZeroExtendExpr(Step, Ty, Depth + 1); |
0 |
1760 |
Step = getZeroExtendExpr(Step, Ty, Depth + 1); |
0 |
| 1761 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
1761 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
| 1762 |
} |
--- |
1762 |
} |
--- |
| 1763 |
} |
--- |
1763 |
} |
--- |
| 1764 |
|
--- |
1764 |
|
--- |
| 1765 |
// zext(A % B) --> zext(A) % zext(B) |
--- |
1765 |
// zext(A % B) --> zext(A) % zext(B) |
--- |
| 1766 |
{ |
--- |
1766 |
{ |
--- |
| 1767 |
const SCEV *LHS; |
--- |
1767 |
const SCEV *LHS; |
--- |
| 1768 |
const SCEV *RHS; |
--- |
1768 |
const SCEV *RHS; |
--- |
| 1769 |
if (matchURem(Op, LHS, RHS)) |
0 |
1769 |
if (matchURem(Op, LHS, RHS)) |
0 |
| 1770 |
return getURemExpr(getZeroExtendExpr(LHS, Ty, Depth + 1), |
0 |
1770 |
return getURemExpr(getZeroExtendExpr(LHS, Ty, Depth + 1), |
0 |
| 1771 |
getZeroExtendExpr(RHS, Ty, Depth + 1)); |
0 |
1771 |
getZeroExtendExpr(RHS, Ty, Depth + 1)); |
0 |
| 1772 |
} |
--- |
1772 |
} |
--- |
| 1773 |
|
--- |
1773 |
|
--- |
| 1774 |
// zext(A / B) --> zext(A) / zext(B). |
--- |
1774 |
// zext(A / B) --> zext(A) / zext(B). |
--- |
| 1775 |
if (auto *Div = dyn_cast(Op)) |
0 |
1775 |
if (auto *Div = dyn_cast(Op)) |
0 |
| 1776 |
return getUDivExpr(getZeroExtendExpr(Div->getLHS(), Ty, Depth + 1), |
0 |
1776 |
return getUDivExpr(getZeroExtendExpr(Div->getLHS(), Ty, Depth + 1), |
0 |
| 1777 |
getZeroExtendExpr(Div->getRHS(), Ty, Depth + 1)); |
0 |
1777 |
getZeroExtendExpr(Div->getRHS(), Ty, Depth + 1)); |
0 |
| 1778 |
|
--- |
1778 |
|
--- |
| 1779 |
if (auto *SA = dyn_cast(Op)) { |
0 |
1779 |
if (auto *SA = dyn_cast(Op)) { |
0 |
| 1780 |
// zext((A + B + ...)) --> (zext(A) + zext(B) + ...) |
--- |
1780 |
// zext((A + B + ...)) --> (zext(A) + zext(B) + ...) |
--- |
| 1781 |
if (SA->hasNoUnsignedWrap()) { |
0 |
1781 |
if (SA->hasNoUnsignedWrap()) { |
0 |
| 1782 |
// If the addition does not unsign overflow then we can, by definition, |
--- |
1782 |
// If the addition does not unsign overflow then we can, by definition, |
--- |
| 1783 |
// commute the zero extension with the addition operation. |
--- |
1783 |
// commute the zero extension with the addition operation. |
--- |
| 1784 |
SmallVector Ops; |
0 |
1784 |
SmallVector Ops; |
0 |
| 1785 |
for (const auto *Op : SA->operands()) |
0 |
1785 |
for (const auto *Op : SA->operands()) |
0 |
| 1786 |
Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1)); |
0 |
1786 |
Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1)); |
0 |
| 1787 |
return getAddExpr(Ops, SCEV::FlagNUW, Depth + 1); |
0 |
1787 |
return getAddExpr(Ops, SCEV::FlagNUW, Depth + 1); |
0 |
| 1788 |
} |
0 |
1788 |
} |
0 |
| 1789 |
|
--- |
1789 |
|
--- |
| 1790 |
// zext(C + x + y + ...) --> (zext(D) + zext((C - D) + x + y + ...)) |
--- |
1790 |
// zext(C + x + y + ...) --> (zext(D) + zext((C - D) + x + y + ...)) |
--- |
| 1791 |
// if D + (C - D + x + y + ...) could be proven to not unsigned wrap |
--- |
1791 |
// if D + (C - D + x + y + ...) could be proven to not unsigned wrap |
--- |
| 1792 |
// where D maximizes the number of trailing zeros of (C - D + x + y + ...) |
--- |
1792 |
// where D maximizes the number of trailing zeros of (C - D + x + y + ...) |
--- |
| 1793 |
// |
--- |
1793 |
// |
--- |
| 1794 |
// Often address arithmetics contain expressions like |
--- |
1794 |
// Often address arithmetics contain expressions like |
--- |
| 1795 |
// (zext (add (shl X, C1), C2)), for instance, (zext (5 + (4 * X))). |
--- |
1795 |
// (zext (add (shl X, C1), C2)), for instance, (zext (5 + (4 * X))). |
--- |
| 1796 |
// This transformation is useful while proving that such expressions are |
--- |
1796 |
// This transformation is useful while proving that such expressions are |
--- |
| 1797 |
// equal or differ by a small constant amount, see LoadStoreVectorizer pass. |
--- |
1797 |
// equal or differ by a small constant amount, see LoadStoreVectorizer pass. |
--- |
| 1798 |
if (const auto *SC = dyn_cast(SA->getOperand(0))) { |
0 |
1798 |
if (const auto *SC = dyn_cast(SA->getOperand(0))) { |
0 |
| 1799 |
const APInt &D = extractConstantWithoutWrapping(*this, SC, SA); |
0 |
1799 |
const APInt &D = extractConstantWithoutWrapping(*this, SC, SA); |
0 |
| 1800 |
if (D != 0) { |
0 |
1800 |
if (D != 0) { |
0 |
| 1801 |
const SCEV *SZExtD = getZeroExtendExpr(getConstant(D), Ty, Depth); |
0 |
1801 |
const SCEV *SZExtD = getZeroExtendExpr(getConstant(D), Ty, Depth); |
0 |
| 1802 |
const SCEV *SResidual = |
--- |
1802 |
const SCEV *SResidual = |
--- |
| 1803 |
getAddExpr(getConstant(-D), SA, SCEV::FlagAnyWrap, Depth); |
0 |
1803 |
getAddExpr(getConstant(-D), SA, SCEV::FlagAnyWrap, Depth); |
0 |
| 1804 |
const SCEV *SZExtR = getZeroExtendExpr(SResidual, Ty, Depth + 1); |
0 |
1804 |
const SCEV *SZExtR = getZeroExtendExpr(SResidual, Ty, Depth + 1); |
0 |
| 1805 |
return getAddExpr(SZExtD, SZExtR, |
0 |
1805 |
return getAddExpr(SZExtD, SZExtR, |
0 |
| 1806 |
(SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW), |
--- |
1806 |
(SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW), |
--- |
| 1807 |
Depth + 1); |
0 |
1807 |
Depth + 1); |
0 |
| 1808 |
} |
--- |
1808 |
} |
--- |
| 1809 |
} |
0 |
1809 |
} |
0 |
| 1810 |
} |
--- |
1810 |
} |
--- |
| 1811 |
|
--- |
1811 |
|
--- |
| 1812 |
if (auto *SM = dyn_cast(Op)) { |
0 |
1812 |
if (auto *SM = dyn_cast(Op)) { |
0 |
| 1813 |
// zext((A * B * ...)) --> (zext(A) * zext(B) * ...) |
--- |
1813 |
// zext((A * B * ...)) --> (zext(A) * zext(B) * ...) |
--- |
| 1814 |
if (SM->hasNoUnsignedWrap()) { |
0 |
1814 |
if (SM->hasNoUnsignedWrap()) { |
0 |
| 1815 |
// If the multiply does not unsign overflow then we can, by definition, |
--- |
1815 |
// If the multiply does not unsign overflow then we can, by definition, |
--- |
| 1816 |
// commute the zero extension with the multiply operation. |
--- |
1816 |
// commute the zero extension with the multiply operation. |
--- |
| 1817 |
SmallVector Ops; |
0 |
1817 |
SmallVector Ops; |
0 |
| 1818 |
for (const auto *Op : SM->operands()) |
0 |
1818 |
for (const auto *Op : SM->operands()) |
0 |
| 1819 |
Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1)); |
0 |
1819 |
Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1)); |
0 |
| 1820 |
return getMulExpr(Ops, SCEV::FlagNUW, Depth + 1); |
0 |
1820 |
return getMulExpr(Ops, SCEV::FlagNUW, Depth + 1); |
0 |
| 1821 |
} |
0 |
1821 |
} |
0 |
| 1822 |
|
--- |
1822 |
|
--- |
| 1823 |
// zext(2^K * (trunc X to iN)) to iM -> |
--- |
1823 |
// zext(2^K * (trunc X to iN)) to iM -> |
--- |
| 1824 |
// 2^K * (zext(trunc X to i{N-K}) to iM) |
--- |
1824 |
// 2^K * (zext(trunc X to i{N-K}) to iM) |
--- |
| 1825 |
// |
--- |
1825 |
// |
--- |
| 1826 |
// Proof: |
--- |
1826 |
// Proof: |
--- |
| 1827 |
// |
--- |
1827 |
// |
--- |
| 1828 |
// zext(2^K * (trunc X to iN)) to iM |
--- |
1828 |
// zext(2^K * (trunc X to iN)) to iM |
--- |
| 1829 |
// = zext((trunc X to iN) << K) to iM |
--- |
1829 |
// = zext((trunc X to iN) << K) to iM |
--- |
| 1830 |
// = zext((trunc X to i{N-K}) << K) to iM |
--- |
1830 |
// = zext((trunc X to i{N-K}) << K) to iM |
--- |
| 1831 |
// (because shl removes the top K bits) |
--- |
1831 |
// (because shl removes the top K bits) |
--- |
| 1832 |
// = zext((2^K * (trunc X to i{N-K}))) to iM |
--- |
1832 |
// = zext((2^K * (trunc X to i{N-K}))) to iM |
--- |
| 1833 |
// = (2^K * (zext(trunc X to i{N-K}) to iM)). |
--- |
1833 |
// = (2^K * (zext(trunc X to i{N-K}) to iM)). |
--- |
| 1834 |
// |
--- |
1834 |
// |
--- |
| 1835 |
if (SM->getNumOperands() == 2) |
0 |
1835 |
if (SM->getNumOperands() == 2) |
0 |
| 1836 |
if (auto *MulLHS = dyn_cast(SM->getOperand(0))) |
0 |
1836 |
if (auto *MulLHS = dyn_cast(SM->getOperand(0))) |
0 |
| 1837 |
if (MulLHS->getAPInt().isPowerOf2()) |
0 |
1837 |
if (MulLHS->getAPInt().isPowerOf2()) |
0 |
| 1838 |
if (auto *TruncRHS = dyn_cast(SM->getOperand(1))) { |
0 |
1838 |
if (auto *TruncRHS = dyn_cast(SM->getOperand(1))) { |
0 |
| 1839 |
int NewTruncBits = getTypeSizeInBits(TruncRHS->getType()) - |
0 |
1839 |
int NewTruncBits = getTypeSizeInBits(TruncRHS->getType()) - |
0 |
| 1840 |
MulLHS->getAPInt().logBase2(); |
0 |
1840 |
MulLHS->getAPInt().logBase2(); |
0 |
| 1841 |
Type *NewTruncTy = IntegerType::get(getContext(), NewTruncBits); |
0 |
1841 |
Type *NewTruncTy = IntegerType::get(getContext(), NewTruncBits); |
0 |
| 1842 |
return getMulExpr( |
0 |
1842 |
return getMulExpr( |
0 |
| 1843 |
getZeroExtendExpr(MulLHS, Ty), |
--- |
1843 |
getZeroExtendExpr(MulLHS, Ty), |
--- |
| 1844 |
getZeroExtendExpr( |
--- |
1844 |
getZeroExtendExpr( |
--- |
| 1845 |
getTruncateExpr(TruncRHS->getOperand(), NewTruncTy), Ty), |
--- |
1845 |
getTruncateExpr(TruncRHS->getOperand(), NewTruncTy), Ty), |
--- |
| 1846 |
SCEV::FlagNUW, Depth + 1); |
0 |
1846 |
SCEV::FlagNUW, Depth + 1); |
0 |
| 1847 |
} |
--- |
1847 |
} |
--- |
| 1848 |
} |
--- |
1848 |
} |
--- |
| 1849 |
|
--- |
1849 |
|
--- |
| 1850 |
// zext(umin(x, y)) -> umin(zext(x), zext(y)) |
--- |
1850 |
// zext(umin(x, y)) -> umin(zext(x), zext(y)) |
--- |
| 1851 |
// zext(umax(x, y)) -> umax(zext(x), zext(y)) |
--- |
1851 |
// zext(umax(x, y)) -> umax(zext(x), zext(y)) |
--- |
| 1852 |
if (isa(Op) || isa(Op)) { |
0 |
1852 |
if (isa(Op) || isa(Op)) { |
0 |
| 1853 |
auto *MinMax = cast(Op); |
0 |
1853 |
auto *MinMax = cast(Op); |
0 |
| 1854 |
SmallVector Operands; |
0 |
1854 |
SmallVector Operands; |
0 |
| 1855 |
for (auto *Operand : MinMax->operands()) |
0 |
1855 |
for (auto *Operand : MinMax->operands()) |
0 |
| 1856 |
Operands.push_back(getZeroExtendExpr(Operand, Ty)); |
0 |
1856 |
Operands.push_back(getZeroExtendExpr(Operand, Ty)); |
0 |
| 1857 |
if (isa(MinMax)) |
0 |
1857 |
if (isa(MinMax)) |
0 |
| 1858 |
return getUMinExpr(Operands); |
0 |
1858 |
return getUMinExpr(Operands); |
0 |
| 1859 |
return getUMaxExpr(Operands); |
0 |
1859 |
return getUMaxExpr(Operands); |
0 |
| 1860 |
} |
0 |
1860 |
} |
0 |
| 1861 |
|
--- |
1861 |
|
--- |
| 1862 |
// zext(umin_seq(x, y)) -> umin_seq(zext(x), zext(y)) |
--- |
1862 |
// zext(umin_seq(x, y)) -> umin_seq(zext(x), zext(y)) |
--- |
| 1863 |
if (auto *MinMax = dyn_cast(Op)) { |
0 |
1863 |
if (auto *MinMax = dyn_cast(Op)) { |
0 |
| 1864 |
assert(isa(MinMax) && "Not supported!"); |
0 |
1864 |
assert(isa(MinMax) && "Not supported!"); |
0 |
| 1865 |
SmallVector Operands; |
0 |
1865 |
SmallVector Operands; |
0 |
| 1866 |
for (auto *Operand : MinMax->operands()) |
0 |
1866 |
for (auto *Operand : MinMax->operands()) |
0 |
| 1867 |
Operands.push_back(getZeroExtendExpr(Operand, Ty)); |
0 |
1867 |
Operands.push_back(getZeroExtendExpr(Operand, Ty)); |
0 |
| 1868 |
return getUMinExpr(Operands, /*Sequential*/ true); |
0 |
1868 |
return getUMinExpr(Operands, /*Sequential*/ true); |
0 |
| 1869 |
} |
0 |
1869 |
} |
0 |
| 1870 |
|
--- |
1870 |
|
--- |
| 1871 |
// The cast wasn't folded; create an explicit cast node. |
--- |
1871 |
// The cast wasn't folded; create an explicit cast node. |
--- |
| 1872 |
// Recompute the insert position, as it may have been invalidated. |
--- |
1872 |
// Recompute the insert position, as it may have been invalidated. |
--- |
| 1873 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
0 |
1873 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
0 |
| 1874 |
SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator), |
0 |
1874 |
SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator), |
0 |
| 1875 |
Op, Ty); |
0 |
1875 |
Op, Ty); |
0 |
| 1876 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
1876 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 1877 |
registerUser(S, Op); |
0 |
1877 |
registerUser(S, Op); |
0 |
| 1878 |
return S; |
0 |
1878 |
return S; |
0 |
| 1879 |
} |
0 |
1879 |
} |
0 |
| 1880 |
|
--- |
1880 |
|
--- |
| 1881 |
const SCEV * |
--- |
1881 |
const SCEV * |
--- |
| 1882 |
ScalarEvolution::getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) { |
0 |
1882 |
ScalarEvolution::getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) { |
0 |
| 1883 |
assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
0 |
1883 |
assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
0 |
| 1884 |
"This is not an extending conversion!"); |
--- |
1884 |
"This is not an extending conversion!"); |
--- |
| 1885 |
assert(isSCEVable(Ty) && |
0 |
1885 |
assert(isSCEVable(Ty) && |
0 |
| 1886 |
"This is not a conversion to a SCEVable type!"); |
--- |
1886 |
"This is not a conversion to a SCEVable type!"); |
--- |
| 1887 |
assert(!Op->getType()->isPointerTy() && "Can't extend pointer!"); |
0 |
1887 |
assert(!Op->getType()->isPointerTy() && "Can't extend pointer!"); |
0 |
| 1888 |
Ty = getEffectiveSCEVType(Ty); |
0 |
1888 |
Ty = getEffectiveSCEVType(Ty); |
0 |
| 1889 |
|
--- |
1889 |
|
--- |
| 1890 |
FoldID ID(scSignExtend, Op, Ty); |
0 |
1890 |
FoldID ID(scSignExtend, Op, Ty); |
0 |
| 1891 |
auto Iter = FoldCache.find(ID); |
0 |
1891 |
auto Iter = FoldCache.find(ID); |
0 |
| 1892 |
if (Iter != FoldCache.end()) |
0 |
1892 |
if (Iter != FoldCache.end()) |
0 |
| 1893 |
return Iter->second; |
0 |
1893 |
return Iter->second; |
0 |
| 1894 |
|
--- |
1894 |
|
--- |
| 1895 |
const SCEV *S = getSignExtendExprImpl(Op, Ty, Depth); |
0 |
1895 |
const SCEV *S = getSignExtendExprImpl(Op, Ty, Depth); |
0 |
| 1896 |
if (!isa(S)) |
0 |
1896 |
if (!isa(S)) |
0 |
| 1897 |
insertFoldCacheEntry(ID, S, FoldCache, FoldCacheUser); |
0 |
1897 |
insertFoldCacheEntry(ID, S, FoldCache, FoldCacheUser); |
0 |
| 1898 |
return S; |
0 |
1898 |
return S; |
0 |
| 1899 |
} |
--- |
1899 |
} |
--- |
| 1900 |
|
--- |
1900 |
|
--- |
| 1901 |
const SCEV *ScalarEvolution::getSignExtendExprImpl(const SCEV *Op, Type *Ty, |
0 |
1901 |
const SCEV *ScalarEvolution::getSignExtendExprImpl(const SCEV *Op, Type *Ty, |
0 |
| 1902 |
unsigned Depth) { |
--- |
1902 |
unsigned Depth) { |
--- |
| 1903 |
assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
0 |
1903 |
assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
0 |
| 1904 |
"This is not an extending conversion!"); |
--- |
1904 |
"This is not an extending conversion!"); |
--- |
| 1905 |
assert(isSCEVable(Ty) && "This is not a conversion to a SCEVable type!"); |
0 |
1905 |
assert(isSCEVable(Ty) && "This is not a conversion to a SCEVable type!"); |
0 |
| 1906 |
assert(!Op->getType()->isPointerTy() && "Can't extend pointer!"); |
0 |
1906 |
assert(!Op->getType()->isPointerTy() && "Can't extend pointer!"); |
0 |
| 1907 |
Ty = getEffectiveSCEVType(Ty); |
0 |
1907 |
Ty = getEffectiveSCEVType(Ty); |
0 |
| 1908 |
|
--- |
1908 |
|
--- |
| 1909 |
// Fold if the operand is constant. |
--- |
1909 |
// Fold if the operand is constant. |
--- |
| 1910 |
if (const SCEVConstant *SC = dyn_cast(Op)) |
0 |
1910 |
if (const SCEVConstant *SC = dyn_cast(Op)) |
0 |
| 1911 |
return getConstant( |
0 |
1911 |
return getConstant( |
0 |
| 1912 |
cast(ConstantExpr::getSExt(SC->getValue(), Ty))); |
0 |
1912 |
cast(ConstantExpr::getSExt(SC->getValue(), Ty))); |
0 |
| 1913 |
|
--- |
1913 |
|
--- |
| 1914 |
// sext(sext(x)) --> sext(x) |
--- |
1914 |
// sext(sext(x)) --> sext(x) |
--- |
| 1915 |
if (const SCEVSignExtendExpr *SS = dyn_cast(Op)) |
0 |
1915 |
if (const SCEVSignExtendExpr *SS = dyn_cast(Op)) |
0 |
| 1916 |
return getSignExtendExpr(SS->getOperand(), Ty, Depth + 1); |
0 |
1916 |
return getSignExtendExpr(SS->getOperand(), Ty, Depth + 1); |
0 |
| 1917 |
|
--- |
1917 |
|
--- |
| 1918 |
// sext(zext(x)) --> zext(x) |
--- |
1918 |
// sext(zext(x)) --> zext(x) |
--- |
| 1919 |
if (const SCEVZeroExtendExpr *SZ = dyn_cast(Op)) |
0 |
1919 |
if (const SCEVZeroExtendExpr *SZ = dyn_cast(Op)) |
0 |
| 1920 |
return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1); |
0 |
1920 |
return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1); |
0 |
| 1921 |
|
--- |
1921 |
|
--- |
| 1922 |
// Before doing any expensive analysis, check to see if we've already |
--- |
1922 |
// Before doing any expensive analysis, check to see if we've already |
--- |
| 1923 |
// computed a SCEV for this Op and Ty. |
--- |
1923 |
// computed a SCEV for this Op and Ty. |
--- |
| 1924 |
FoldingSetNodeID ID; |
0 |
1924 |
FoldingSetNodeID ID; |
0 |
| 1925 |
ID.AddInteger(scSignExtend); |
0 |
1925 |
ID.AddInteger(scSignExtend); |
0 |
| 1926 |
ID.AddPointer(Op); |
0 |
1926 |
ID.AddPointer(Op); |
0 |
| 1927 |
ID.AddPointer(Ty); |
0 |
1927 |
ID.AddPointer(Ty); |
0 |
| 1928 |
void *IP = nullptr; |
0 |
1928 |
void *IP = nullptr; |
0 |
| 1929 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
0 |
1929 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
0 |
| 1930 |
// Limit recursion depth. |
--- |
1930 |
// Limit recursion depth. |
--- |
| 1931 |
if (Depth > MaxCastDepth) { |
0 |
1931 |
if (Depth > MaxCastDepth) { |
0 |
| 1932 |
SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator), |
0 |
1932 |
SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator), |
0 |
| 1933 |
Op, Ty); |
0 |
1933 |
Op, Ty); |
0 |
| 1934 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
1934 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 1935 |
registerUser(S, Op); |
0 |
1935 |
registerUser(S, Op); |
0 |
| 1936 |
return S; |
0 |
1936 |
return S; |
0 |
| 1937 |
} |
--- |
1937 |
} |
--- |
| 1938 |
|
--- |
1938 |
|
--- |
| 1939 |
// sext(trunc(x)) --> sext(x) or x or trunc(x) |
--- |
1939 |
// sext(trunc(x)) --> sext(x) or x or trunc(x) |
--- |
| 1940 |
if (const SCEVTruncateExpr *ST = dyn_cast(Op)) { |
0 |
1940 |
if (const SCEVTruncateExpr *ST = dyn_cast(Op)) { |
0 |
| 1941 |
// It's possible the bits taken off by the truncate were all sign bits. If |
--- |
1941 |
// It's possible the bits taken off by the truncate were all sign bits. If |
--- |
| 1942 |
// so, we should be able to simplify this further. |
--- |
1942 |
// so, we should be able to simplify this further. |
--- |
| 1943 |
const SCEV *X = ST->getOperand(); |
0 |
1943 |
const SCEV *X = ST->getOperand(); |
0 |
| 1944 |
ConstantRange CR = getSignedRange(X); |
0 |
1944 |
ConstantRange CR = getSignedRange(X); |
0 |
| 1945 |
unsigned TruncBits = getTypeSizeInBits(ST->getType()); |
0 |
1945 |
unsigned TruncBits = getTypeSizeInBits(ST->getType()); |
0 |
| 1946 |
unsigned NewBits = getTypeSizeInBits(Ty); |
0 |
1946 |
unsigned NewBits = getTypeSizeInBits(Ty); |
0 |
| 1947 |
if (CR.truncate(TruncBits).signExtend(NewBits).contains( |
0 |
1947 |
if (CR.truncate(TruncBits).signExtend(NewBits).contains( |
0 |
| 1948 |
CR.sextOrTrunc(NewBits))) |
0 |
1948 |
CR.sextOrTrunc(NewBits))) |
0 |
| 1949 |
return getTruncateOrSignExtend(X, Ty, Depth); |
0 |
1949 |
return getTruncateOrSignExtend(X, Ty, Depth); |
0 |
| 1950 |
} |
0 |
1950 |
} |
0 |
| 1951 |
|
--- |
1951 |
|
--- |
| 1952 |
if (auto *SA = dyn_cast(Op)) { |
0 |
1952 |
if (auto *SA = dyn_cast(Op)) { |
0 |
| 1953 |
// sext((A + B + ...)) --> (sext(A) + sext(B) + ...) |
--- |
1953 |
// sext((A + B + ...)) --> (sext(A) + sext(B) + ...) |
--- |
| 1954 |
if (SA->hasNoSignedWrap()) { |
0 |
1954 |
if (SA->hasNoSignedWrap()) { |
0 |
| 1955 |
// If the addition does not sign overflow then we can, by definition, |
--- |
1955 |
// If the addition does not sign overflow then we can, by definition, |
--- |
| 1956 |
// commute the sign extension with the addition operation. |
--- |
1956 |
// commute the sign extension with the addition operation. |
--- |
| 1957 |
SmallVector Ops; |
0 |
1957 |
SmallVector Ops; |
0 |
| 1958 |
for (const auto *Op : SA->operands()) |
0 |
1958 |
for (const auto *Op : SA->operands()) |
0 |
| 1959 |
Ops.push_back(getSignExtendExpr(Op, Ty, Depth + 1)); |
0 |
1959 |
Ops.push_back(getSignExtendExpr(Op, Ty, Depth + 1)); |
0 |
| 1960 |
return getAddExpr(Ops, SCEV::FlagNSW, Depth + 1); |
0 |
1960 |
return getAddExpr(Ops, SCEV::FlagNSW, Depth + 1); |
0 |
| 1961 |
} |
0 |
1961 |
} |
0 |
| 1962 |
|
--- |
1962 |
|
--- |
| 1963 |
// sext(C + x + y + ...) --> (sext(D) + sext((C - D) + x + y + ...)) |
--- |
1963 |
// sext(C + x + y + ...) --> (sext(D) + sext((C - D) + x + y + ...)) |
--- |
| 1964 |
// if D + (C - D + x + y + ...) could be proven to not signed wrap |
--- |
1964 |
// if D + (C - D + x + y + ...) could be proven to not signed wrap |
--- |
| 1965 |
// where D maximizes the number of trailing zeros of (C - D + x + y + ...) |
--- |
1965 |
// where D maximizes the number of trailing zeros of (C - D + x + y + ...) |
--- |
| 1966 |
// |
--- |
1966 |
// |
--- |
| 1967 |
// For instance, this will bring two seemingly different expressions: |
--- |
1967 |
// For instance, this will bring two seemingly different expressions: |
--- |
| 1968 |
// 1 + sext(5 + 20 * %x + 24 * %y) and |
--- |
1968 |
// 1 + sext(5 + 20 * %x + 24 * %y) and |
--- |
| 1969 |
// sext(6 + 20 * %x + 24 * %y) |
--- |
1969 |
// sext(6 + 20 * %x + 24 * %y) |
--- |
| 1970 |
// to the same form: |
--- |
1970 |
// to the same form: |
--- |
| 1971 |
// 2 + sext(4 + 20 * %x + 24 * %y) |
--- |
1971 |
// 2 + sext(4 + 20 * %x + 24 * %y) |
--- |
| 1972 |
if (const auto *SC = dyn_cast(SA->getOperand(0))) { |
0 |
1972 |
if (const auto *SC = dyn_cast(SA->getOperand(0))) { |
0 |
| 1973 |
const APInt &D = extractConstantWithoutWrapping(*this, SC, SA); |
0 |
1973 |
const APInt &D = extractConstantWithoutWrapping(*this, SC, SA); |
0 |
| 1974 |
if (D != 0) { |
0 |
1974 |
if (D != 0) { |
0 |
| 1975 |
const SCEV *SSExtD = getSignExtendExpr(getConstant(D), Ty, Depth); |
0 |
1975 |
const SCEV *SSExtD = getSignExtendExpr(getConstant(D), Ty, Depth); |
0 |
| 1976 |
const SCEV *SResidual = |
--- |
1976 |
const SCEV *SResidual = |
--- |
| 1977 |
getAddExpr(getConstant(-D), SA, SCEV::FlagAnyWrap, Depth); |
0 |
1977 |
getAddExpr(getConstant(-D), SA, SCEV::FlagAnyWrap, Depth); |
0 |
| 1978 |
const SCEV *SSExtR = getSignExtendExpr(SResidual, Ty, Depth + 1); |
0 |
1978 |
const SCEV *SSExtR = getSignExtendExpr(SResidual, Ty, Depth + 1); |
0 |
| 1979 |
return getAddExpr(SSExtD, SSExtR, |
0 |
1979 |
return getAddExpr(SSExtD, SSExtR, |
0 |
| 1980 |
(SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW), |
--- |
1980 |
(SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW), |
--- |
| 1981 |
Depth + 1); |
0 |
1981 |
Depth + 1); |
0 |
| 1982 |
} |
--- |
1982 |
} |
--- |
| 1983 |
} |
0 |
1983 |
} |
0 |
| 1984 |
} |
--- |
1984 |
} |
--- |
| 1985 |
// If the input value is a chrec scev, and we can prove that the value |
--- |
1985 |
// If the input value is a chrec scev, and we can prove that the value |
--- |
| 1986 |
// did not overflow the old, smaller, value, we can sign extend all of the |
--- |
1986 |
// did not overflow the old, smaller, value, we can sign extend all of the |
--- |
| 1987 |
// operands (often constants). This allows analysis of something like |
--- |
1987 |
// operands (often constants). This allows analysis of something like |
--- |
| 1988 |
// this: for (signed char X = 0; X < 100; ++X) { int Y = X; } |
--- |
1988 |
// this: for (signed char X = 0; X < 100; ++X) { int Y = X; } |
--- |
| 1989 |
if (const SCEVAddRecExpr *AR = dyn_cast(Op)) |
0 |
1989 |
if (const SCEVAddRecExpr *AR = dyn_cast(Op)) |
0 |
| 1990 |
if (AR->isAffine()) { |
0 |
1990 |
if (AR->isAffine()) { |
0 |
| 1991 |
const SCEV *Start = AR->getStart(); |
0 |
1991 |
const SCEV *Start = AR->getStart(); |
0 |
| 1992 |
const SCEV *Step = AR->getStepRecurrence(*this); |
0 |
1992 |
const SCEV *Step = AR->getStepRecurrence(*this); |
0 |
| 1993 |
unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
0 |
1993 |
unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
0 |
| 1994 |
const Loop *L = AR->getLoop(); |
0 |
1994 |
const Loop *L = AR->getLoop(); |
0 |
| 1995 |
|
--- |
1995 |
|
--- |
| 1996 |
// If we have special knowledge that this addrec won't overflow, |
--- |
1996 |
// If we have special knowledge that this addrec won't overflow, |
--- |
| 1997 |
// we don't need to do any further analysis. |
--- |
1997 |
// we don't need to do any further analysis. |
--- |
| 1998 |
if (AR->hasNoSignedWrap()) { |
0 |
1998 |
if (AR->hasNoSignedWrap()) { |
0 |
| 1999 |
Start = |
--- |
1999 |
Start = |
--- |
| 2000 |
getExtendAddRecStart(AR, Ty, this, Depth + 1); |
0 |
2000 |
getExtendAddRecStart(AR, Ty, this, Depth + 1); |
0 |
| 2001 |
Step = getSignExtendExpr(Step, Ty, Depth + 1); |
0 |
2001 |
Step = getSignExtendExpr(Step, Ty, Depth + 1); |
0 |
| 2002 |
return getAddRecExpr(Start, Step, L, SCEV::FlagNSW); |
0 |
2002 |
return getAddRecExpr(Start, Step, L, SCEV::FlagNSW); |
0 |
| 2003 |
} |
--- |
2003 |
} |
--- |
| 2004 |
|
--- |
2004 |
|
--- |
| 2005 |
// Check whether the backedge-taken count is SCEVCouldNotCompute. |
--- |
2005 |
// Check whether the backedge-taken count is SCEVCouldNotCompute. |
--- |
| 2006 |
// Note that this serves two purposes: It filters out loops that are |
--- |
2006 |
// Note that this serves two purposes: It filters out loops that are |
--- |
| 2007 |
// simply not analyzable, and it covers the case where this code is |
--- |
2007 |
// simply not analyzable, and it covers the case where this code is |
--- |
| 2008 |
// being called from within backedge-taken count analysis, such that |
--- |
2008 |
// being called from within backedge-taken count analysis, such that |
--- |
| 2009 |
// attempting to ask for the backedge-taken count would likely result |
--- |
2009 |
// attempting to ask for the backedge-taken count would likely result |
--- |
| 2010 |
// in infinite recursion. In the later case, the analysis code will |
--- |
2010 |
// in infinite recursion. In the later case, the analysis code will |
--- |
| 2011 |
// cope with a conservative value, and it will take care to purge |
--- |
2011 |
// cope with a conservative value, and it will take care to purge |
--- |
| 2012 |
// that value once it has finished. |
--- |
2012 |
// that value once it has finished. |
--- |
| 2013 |
const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L); |
0 |
2013 |
const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L); |
0 |
| 2014 |
if (!isa(MaxBECount)) { |
0 |
2014 |
if (!isa(MaxBECount)) { |
0 |
| 2015 |
// Manually compute the final value for AR, checking for |
--- |
2015 |
// Manually compute the final value for AR, checking for |
--- |
| 2016 |
// overflow. |
--- |
2016 |
// overflow. |
--- |
| 2017 |
|
--- |
2017 |
|
--- |
| 2018 |
// Check whether the backedge-taken count can be losslessly casted to |
--- |
2018 |
// Check whether the backedge-taken count can be losslessly casted to |
--- |
| 2019 |
// the addrec's type. The count is always unsigned. |
--- |
2019 |
// the addrec's type. The count is always unsigned. |
--- |
| 2020 |
const SCEV *CastedMaxBECount = |
--- |
2020 |
const SCEV *CastedMaxBECount = |
--- |
| 2021 |
getTruncateOrZeroExtend(MaxBECount, Start->getType(), Depth); |
0 |
2021 |
getTruncateOrZeroExtend(MaxBECount, Start->getType(), Depth); |
0 |
| 2022 |
const SCEV *RecastedMaxBECount = getTruncateOrZeroExtend( |
0 |
2022 |
const SCEV *RecastedMaxBECount = getTruncateOrZeroExtend( |
0 |
| 2023 |
CastedMaxBECount, MaxBECount->getType(), Depth); |
--- |
2023 |
CastedMaxBECount, MaxBECount->getType(), Depth); |
--- |
| 2024 |
if (MaxBECount == RecastedMaxBECount) { |
0 |
2024 |
if (MaxBECount == RecastedMaxBECount) { |
0 |
| 2025 |
Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
0 |
2025 |
Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
0 |
| 2026 |
// Check whether Start+Step*MaxBECount has no signed overflow. |
--- |
2026 |
// Check whether Start+Step*MaxBECount has no signed overflow. |
--- |
| 2027 |
const SCEV *SMul = getMulExpr(CastedMaxBECount, Step, |
0 |
2027 |
const SCEV *SMul = getMulExpr(CastedMaxBECount, Step, |
0 |
| 2028 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
2028 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
| 2029 |
const SCEV *SAdd = getSignExtendExpr(getAddExpr(Start, SMul, |
0 |
2029 |
const SCEV *SAdd = getSignExtendExpr(getAddExpr(Start, SMul, |
0 |
| 2030 |
SCEV::FlagAnyWrap, |
--- |
2030 |
SCEV::FlagAnyWrap, |
--- |
| 2031 |
Depth + 1), |
--- |
2031 |
Depth + 1), |
--- |
| 2032 |
WideTy, Depth + 1); |
--- |
2032 |
WideTy, Depth + 1); |
--- |
| 2033 |
const SCEV *WideStart = getSignExtendExpr(Start, WideTy, Depth + 1); |
0 |
2033 |
const SCEV *WideStart = getSignExtendExpr(Start, WideTy, Depth + 1); |
0 |
| 2034 |
const SCEV *WideMaxBECount = |
--- |
2034 |
const SCEV *WideMaxBECount = |
--- |
| 2035 |
getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1); |
0 |
2035 |
getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1); |
0 |
| 2036 |
const SCEV *OperandExtendedAdd = |
--- |
2036 |
const SCEV *OperandExtendedAdd = |
--- |
| 2037 |
getAddExpr(WideStart, |
0 |
2037 |
getAddExpr(WideStart, |
0 |
| 2038 |
getMulExpr(WideMaxBECount, |
--- |
2038 |
getMulExpr(WideMaxBECount, |
--- |
| 2039 |
getSignExtendExpr(Step, WideTy, Depth + 1), |
--- |
2039 |
getSignExtendExpr(Step, WideTy, Depth + 1), |
--- |
| 2040 |
SCEV::FlagAnyWrap, Depth + 1), |
--- |
2040 |
SCEV::FlagAnyWrap, Depth + 1), |
--- |
| 2041 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
2041 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
| 2042 |
if (SAdd == OperandExtendedAdd) { |
0 |
2042 |
if (SAdd == OperandExtendedAdd) { |
0 |
| 2043 |
// Cache knowledge of AR NSW, which is propagated to this AddRec. |
--- |
2043 |
// Cache knowledge of AR NSW, which is propagated to this AddRec. |
--- |
| 2044 |
setNoWrapFlags(const_cast(AR), SCEV::FlagNSW); |
0 |
2044 |
setNoWrapFlags(const_cast(AR), SCEV::FlagNSW); |
0 |
| 2045 |
// Return the expression with the addrec on the outside. |
--- |
2045 |
// Return the expression with the addrec on the outside. |
--- |
| 2046 |
Start = getExtendAddRecStart(AR, Ty, this, |
0 |
2046 |
Start = getExtendAddRecStart(AR, Ty, this, |
0 |
| 2047 |
Depth + 1); |
--- |
2047 |
Depth + 1); |
--- |
| 2048 |
Step = getSignExtendExpr(Step, Ty, Depth + 1); |
0 |
2048 |
Step = getSignExtendExpr(Step, Ty, Depth + 1); |
0 |
| 2049 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
2049 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
| 2050 |
} |
--- |
2050 |
} |
--- |
| 2051 |
// Similar to above, only this time treat the step value as unsigned. |
--- |
2051 |
// Similar to above, only this time treat the step value as unsigned. |
--- |
| 2052 |
// This covers loops that count up with an unsigned step. |
--- |
2052 |
// This covers loops that count up with an unsigned step. |
--- |
| 2053 |
OperandExtendedAdd = |
--- |
2053 |
OperandExtendedAdd = |
--- |
| 2054 |
getAddExpr(WideStart, |
0 |
2054 |
getAddExpr(WideStart, |
0 |
| 2055 |
getMulExpr(WideMaxBECount, |
--- |
2055 |
getMulExpr(WideMaxBECount, |
--- |
| 2056 |
getZeroExtendExpr(Step, WideTy, Depth + 1), |
--- |
2056 |
getZeroExtendExpr(Step, WideTy, Depth + 1), |
--- |
| 2057 |
SCEV::FlagAnyWrap, Depth + 1), |
--- |
2057 |
SCEV::FlagAnyWrap, Depth + 1), |
--- |
| 2058 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
2058 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
| 2059 |
if (SAdd == OperandExtendedAdd) { |
0 |
2059 |
if (SAdd == OperandExtendedAdd) { |
0 |
| 2060 |
// If AR wraps around then |
--- |
2060 |
// If AR wraps around then |
--- |
| 2061 |
// |
--- |
2061 |
// |
--- |
| 2062 |
// abs(Step) * MaxBECount > unsigned-max(AR->getType()) |
--- |
2062 |
// abs(Step) * MaxBECount > unsigned-max(AR->getType()) |
--- |
| 2063 |
// => SAdd != OperandExtendedAdd |
--- |
2063 |
// => SAdd != OperandExtendedAdd |
--- |
| 2064 |
// |
--- |
2064 |
// |
--- |
| 2065 |
// Thus (AR is not NW => SAdd != OperandExtendedAdd) <=> |
--- |
2065 |
// Thus (AR is not NW => SAdd != OperandExtendedAdd) <=> |
--- |
| 2066 |
// (SAdd == OperandExtendedAdd => AR is NW) |
--- |
2066 |
// (SAdd == OperandExtendedAdd => AR is NW) |
--- |
| 2067 |
|
--- |
2067 |
|
--- |
| 2068 |
setNoWrapFlags(const_cast(AR), SCEV::FlagNW); |
0 |
2068 |
setNoWrapFlags(const_cast(AR), SCEV::FlagNW); |
0 |
| 2069 |
|
--- |
2069 |
|
--- |
| 2070 |
// Return the expression with the addrec on the outside. |
--- |
2070 |
// Return the expression with the addrec on the outside. |
--- |
| 2071 |
Start = getExtendAddRecStart(AR, Ty, this, |
0 |
2071 |
Start = getExtendAddRecStart(AR, Ty, this, |
0 |
| 2072 |
Depth + 1); |
--- |
2072 |
Depth + 1); |
--- |
| 2073 |
Step = getZeroExtendExpr(Step, Ty, Depth + 1); |
0 |
2073 |
Step = getZeroExtendExpr(Step, Ty, Depth + 1); |
0 |
| 2074 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
2074 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
| 2075 |
} |
--- |
2075 |
} |
--- |
| 2076 |
} |
--- |
2076 |
} |
--- |
| 2077 |
} |
--- |
2077 |
} |
--- |
| 2078 |
|
--- |
2078 |
|
--- |
| 2079 |
auto NewFlags = proveNoSignedWrapViaInduction(AR); |
0 |
2079 |
auto NewFlags = proveNoSignedWrapViaInduction(AR); |
0 |
| 2080 |
setNoWrapFlags(const_cast(AR), NewFlags); |
0 |
2080 |
setNoWrapFlags(const_cast(AR), NewFlags); |
0 |
| 2081 |
if (AR->hasNoSignedWrap()) { |
0 |
2081 |
if (AR->hasNoSignedWrap()) { |
0 |
| 2082 |
// Same as nsw case above - duplicated here to avoid a compile time |
--- |
2082 |
// Same as nsw case above - duplicated here to avoid a compile time |
--- |
| 2083 |
// issue. It's not clear that the order of checks does matter, but |
--- |
2083 |
// issue. It's not clear that the order of checks does matter, but |
--- |
| 2084 |
// it's one of two issue possible causes for a change which was |
--- |
2084 |
// it's one of two issue possible causes for a change which was |
--- |
| 2085 |
// reverted. Be conservative for the moment. |
--- |
2085 |
// reverted. Be conservative for the moment. |
--- |
| 2086 |
Start = |
--- |
2086 |
Start = |
--- |
| 2087 |
getExtendAddRecStart(AR, Ty, this, Depth + 1); |
0 |
2087 |
getExtendAddRecStart(AR, Ty, this, Depth + 1); |
0 |
| 2088 |
Step = getSignExtendExpr(Step, Ty, Depth + 1); |
0 |
2088 |
Step = getSignExtendExpr(Step, Ty, Depth + 1); |
0 |
| 2089 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
2089 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
| 2090 |
} |
--- |
2090 |
} |
--- |
| 2091 |
|
--- |
2091 |
|
--- |
| 2092 |
// sext({C,+,Step}) --> (sext(D) + sext({C-D,+,Step})) |
--- |
2092 |
// sext({C,+,Step}) --> (sext(D) + sext({C-D,+,Step})) |
--- |
| 2093 |
// if D + (C - D + Step * n) could be proven to not signed wrap |
--- |
2093 |
// if D + (C - D + Step * n) could be proven to not signed wrap |
--- |
| 2094 |
// where D maximizes the number of trailing zeros of (C - D + Step * n) |
--- |
2094 |
// where D maximizes the number of trailing zeros of (C - D + Step * n) |
--- |
| 2095 |
if (const auto *SC = dyn_cast(Start)) { |
0 |
2095 |
if (const auto *SC = dyn_cast(Start)) { |
0 |
| 2096 |
const APInt &C = SC->getAPInt(); |
0 |
2096 |
const APInt &C = SC->getAPInt(); |
0 |
| 2097 |
const APInt &D = extractConstantWithoutWrapping(*this, C, Step); |
0 |
2097 |
const APInt &D = extractConstantWithoutWrapping(*this, C, Step); |
0 |
| 2098 |
if (D != 0) { |
0 |
2098 |
if (D != 0) { |
0 |
| 2099 |
const SCEV *SSExtD = getSignExtendExpr(getConstant(D), Ty, Depth); |
0 |
2099 |
const SCEV *SSExtD = getSignExtendExpr(getConstant(D), Ty, Depth); |
0 |
| 2100 |
const SCEV *SResidual = |
--- |
2100 |
const SCEV *SResidual = |
--- |
| 2101 |
getAddRecExpr(getConstant(C - D), Step, L, AR->getNoWrapFlags()); |
0 |
2101 |
getAddRecExpr(getConstant(C - D), Step, L, AR->getNoWrapFlags()); |
0 |
| 2102 |
const SCEV *SSExtR = getSignExtendExpr(SResidual, Ty, Depth + 1); |
0 |
2102 |
const SCEV *SSExtR = getSignExtendExpr(SResidual, Ty, Depth + 1); |
0 |
| 2103 |
return getAddExpr(SSExtD, SSExtR, |
0 |
2103 |
return getAddExpr(SSExtD, SSExtR, |
0 |
| 2104 |
(SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW), |
--- |
2104 |
(SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW), |
--- |
| 2105 |
Depth + 1); |
0 |
2105 |
Depth + 1); |
0 |
| 2106 |
} |
--- |
2106 |
} |
--- |
| 2107 |
} |
0 |
2107 |
} |
0 |
| 2108 |
|
--- |
2108 |
|
--- |
| 2109 |
if (proveNoWrapByVaryingStart(Start, Step, L)) { |
0 |
2109 |
if (proveNoWrapByVaryingStart(Start, Step, L)) { |
0 |
| 2110 |
setNoWrapFlags(const_cast(AR), SCEV::FlagNSW); |
0 |
2110 |
setNoWrapFlags(const_cast(AR), SCEV::FlagNSW); |
0 |
| 2111 |
Start = |
--- |
2111 |
Start = |
--- |
| 2112 |
getExtendAddRecStart(AR, Ty, this, Depth + 1); |
0 |
2112 |
getExtendAddRecStart(AR, Ty, this, Depth + 1); |
0 |
| 2113 |
Step = getSignExtendExpr(Step, Ty, Depth + 1); |
0 |
2113 |
Step = getSignExtendExpr(Step, Ty, Depth + 1); |
0 |
| 2114 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
2114 |
return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); |
0 |
| 2115 |
} |
--- |
2115 |
} |
--- |
| 2116 |
} |
--- |
2116 |
} |
--- |
| 2117 |
|
--- |
2117 |
|
--- |
| 2118 |
// If the input value is provably positive and we could not simplify |
--- |
2118 |
// If the input value is provably positive and we could not simplify |
--- |
| 2119 |
// away the sext build a zext instead. |
--- |
2119 |
// away the sext build a zext instead. |
--- |
| 2120 |
if (isKnownNonNegative(Op)) |
0 |
2120 |
if (isKnownNonNegative(Op)) |
0 |
| 2121 |
return getZeroExtendExpr(Op, Ty, Depth + 1); |
0 |
2121 |
return getZeroExtendExpr(Op, Ty, Depth + 1); |
0 |
| 2122 |
|
--- |
2122 |
|
--- |
| 2123 |
// sext(smin(x, y)) -> smin(sext(x), sext(y)) |
--- |
2123 |
// sext(smin(x, y)) -> smin(sext(x), sext(y)) |
--- |
| 2124 |
// sext(smax(x, y)) -> smax(sext(x), sext(y)) |
--- |
2124 |
// sext(smax(x, y)) -> smax(sext(x), sext(y)) |
--- |
| 2125 |
if (isa(Op) || isa(Op)) { |
0 |
2125 |
if (isa(Op) || isa(Op)) { |
0 |
| 2126 |
auto *MinMax = cast(Op); |
0 |
2126 |
auto *MinMax = cast(Op); |
0 |
| 2127 |
SmallVector Operands; |
0 |
2127 |
SmallVector Operands; |
0 |
| 2128 |
for (auto *Operand : MinMax->operands()) |
0 |
2128 |
for (auto *Operand : MinMax->operands()) |
0 |
| 2129 |
Operands.push_back(getSignExtendExpr(Operand, Ty)); |
0 |
2129 |
Operands.push_back(getSignExtendExpr(Operand, Ty)); |
0 |
| 2130 |
if (isa(MinMax)) |
0 |
2130 |
if (isa(MinMax)) |
0 |
| 2131 |
return getSMinExpr(Operands); |
0 |
2131 |
return getSMinExpr(Operands); |
0 |
| 2132 |
return getSMaxExpr(Operands); |
0 |
2132 |
return getSMaxExpr(Operands); |
0 |
| 2133 |
} |
0 |
2133 |
} |
0 |
| 2134 |
|
--- |
2134 |
|
--- |
| 2135 |
// The cast wasn't folded; create an explicit cast node. |
--- |
2135 |
// The cast wasn't folded; create an explicit cast node. |
--- |
| 2136 |
// Recompute the insert position, as it may have been invalidated. |
--- |
2136 |
// Recompute the insert position, as it may have been invalidated. |
--- |
| 2137 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
0 |
2137 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
0 |
| 2138 |
SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator), |
0 |
2138 |
SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator), |
0 |
| 2139 |
Op, Ty); |
0 |
2139 |
Op, Ty); |
0 |
| 2140 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
2140 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 2141 |
registerUser(S, { Op }); |
0 |
2141 |
registerUser(S, { Op }); |
0 |
| 2142 |
return S; |
0 |
2142 |
return S; |
0 |
| 2143 |
} |
0 |
2143 |
} |
0 |
| 2144 |
|
--- |
2144 |
|
--- |
| 2145 |
const SCEV *ScalarEvolution::getCastExpr(SCEVTypes Kind, const SCEV *Op, |
0 |
2145 |
const SCEV *ScalarEvolution::getCastExpr(SCEVTypes Kind, const SCEV *Op, |
0 |
| 2146 |
Type *Ty) { |
--- |
2146 |
Type *Ty) { |
--- |
| 2147 |
switch (Kind) { |
0 |
2147 |
switch (Kind) { |
0 |
| 2148 |
case scTruncate: |
0 |
2148 |
case scTruncate: |
0 |
| 2149 |
return getTruncateExpr(Op, Ty); |
0 |
2149 |
return getTruncateExpr(Op, Ty); |
0 |
| 2150 |
case scZeroExtend: |
0 |
2150 |
case scZeroExtend: |
0 |
| 2151 |
return getZeroExtendExpr(Op, Ty); |
0 |
2151 |
return getZeroExtendExpr(Op, Ty); |
0 |
| 2152 |
case scSignExtend: |
0 |
2152 |
case scSignExtend: |
0 |
| 2153 |
return getSignExtendExpr(Op, Ty); |
0 |
2153 |
return getSignExtendExpr(Op, Ty); |
0 |
| 2154 |
case scPtrToInt: |
0 |
2154 |
case scPtrToInt: |
0 |
| 2155 |
return getPtrToIntExpr(Op, Ty); |
0 |
2155 |
return getPtrToIntExpr(Op, Ty); |
0 |
| 2156 |
default: |
0 |
2156 |
default: |
0 |
| 2157 |
llvm_unreachable("Not a SCEV cast expression!"); |
0 |
2157 |
llvm_unreachable("Not a SCEV cast expression!"); |
0 |
| 2158 |
} |
--- |
2158 |
} |
--- |
| 2159 |
} |
--- |
2159 |
} |
--- |
| 2160 |
|
--- |
2160 |
|
--- |
| 2161 |
/// getAnyExtendExpr - Return a SCEV for the given operand extended with |
--- |
2161 |
/// getAnyExtendExpr - Return a SCEV for the given operand extended with |
--- |
| 2162 |
/// unspecified bits out to the given type. |
--- |
2162 |
/// unspecified bits out to the given type. |
--- |
| 2163 |
const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op, |
0 |
2163 |
const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op, |
0 |
| 2164 |
Type *Ty) { |
--- |
2164 |
Type *Ty) { |
--- |
| 2165 |
assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
0 |
2165 |
assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
0 |
| 2166 |
"This is not an extending conversion!"); |
--- |
2166 |
"This is not an extending conversion!"); |
--- |
| 2167 |
assert(isSCEVable(Ty) && |
0 |
2167 |
assert(isSCEVable(Ty) && |
0 |
| 2168 |
"This is not a conversion to a SCEVable type!"); |
--- |
2168 |
"This is not a conversion to a SCEVable type!"); |
--- |
| 2169 |
Ty = getEffectiveSCEVType(Ty); |
0 |
2169 |
Ty = getEffectiveSCEVType(Ty); |
0 |
| 2170 |
|
--- |
2170 |
|
--- |
| 2171 |
// Sign-extend negative constants. |
--- |
2171 |
// Sign-extend negative constants. |
--- |
| 2172 |
if (const SCEVConstant *SC = dyn_cast(Op)) |
0 |
2172 |
if (const SCEVConstant *SC = dyn_cast(Op)) |
0 |
| 2173 |
if (SC->getAPInt().isNegative()) |
0 |
2173 |
if (SC->getAPInt().isNegative()) |
0 |
| 2174 |
return getSignExtendExpr(Op, Ty); |
0 |
2174 |
return getSignExtendExpr(Op, Ty); |
0 |
| 2175 |
|
--- |
2175 |
|
--- |
| 2176 |
// Peel off a truncate cast. |
--- |
2176 |
// Peel off a truncate cast. |
--- |
| 2177 |
if (const SCEVTruncateExpr *T = dyn_cast(Op)) { |
0 |
2177 |
if (const SCEVTruncateExpr *T = dyn_cast(Op)) { |
0 |
| 2178 |
const SCEV *NewOp = T->getOperand(); |
0 |
2178 |
const SCEV *NewOp = T->getOperand(); |
0 |
| 2179 |
if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) |
0 |
2179 |
if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) |
0 |
| 2180 |
return getAnyExtendExpr(NewOp, Ty); |
0 |
2180 |
return getAnyExtendExpr(NewOp, Ty); |
0 |
| 2181 |
return getTruncateOrNoop(NewOp, Ty); |
0 |
2181 |
return getTruncateOrNoop(NewOp, Ty); |
0 |
| 2182 |
} |
--- |
2182 |
} |
--- |
| 2183 |
|
--- |
2183 |
|
--- |
| 2184 |
// Next try a zext cast. If the cast is folded, use it. |
--- |
2184 |
// Next try a zext cast. If the cast is folded, use it. |
--- |
| 2185 |
const SCEV *ZExt = getZeroExtendExpr(Op, Ty); |
0 |
2185 |
const SCEV *ZExt = getZeroExtendExpr(Op, Ty); |
0 |
| 2186 |
if (!isa(ZExt)) |
0 |
2186 |
if (!isa(ZExt)) |
0 |
| 2187 |
return ZExt; |
0 |
2187 |
return ZExt; |
0 |
| 2188 |
|
--- |
2188 |
|
--- |
| 2189 |
// Next try a sext cast. If the cast is folded, use it. |
--- |
2189 |
// Next try a sext cast. If the cast is folded, use it. |
--- |
| 2190 |
const SCEV *SExt = getSignExtendExpr(Op, Ty); |
0 |
2190 |
const SCEV *SExt = getSignExtendExpr(Op, Ty); |
0 |
| 2191 |
if (!isa(SExt)) |
0 |
2191 |
if (!isa(SExt)) |
0 |
| 2192 |
return SExt; |
0 |
2192 |
return SExt; |
0 |
| 2193 |
|
--- |
2193 |
|
--- |
| 2194 |
// Force the cast to be folded into the operands of an addrec. |
--- |
2194 |
// Force the cast to be folded into the operands of an addrec. |
--- |
| 2195 |
if (const SCEVAddRecExpr *AR = dyn_cast(Op)) { |
0 |
2195 |
if (const SCEVAddRecExpr *AR = dyn_cast(Op)) { |
0 |
| 2196 |
SmallVector Ops; |
0 |
2196 |
SmallVector Ops; |
0 |
| 2197 |
for (const SCEV *Op : AR->operands()) |
0 |
2197 |
for (const SCEV *Op : AR->operands()) |
0 |
| 2198 |
Ops.push_back(getAnyExtendExpr(Op, Ty)); |
0 |
2198 |
Ops.push_back(getAnyExtendExpr(Op, Ty)); |
0 |
| 2199 |
return getAddRecExpr(Ops, AR->getLoop(), SCEV::FlagNW); |
0 |
2199 |
return getAddRecExpr(Ops, AR->getLoop(), SCEV::FlagNW); |
0 |
| 2200 |
} |
0 |
2200 |
} |
0 |
| 2201 |
|
--- |
2201 |
|
--- |
| 2202 |
// If the expression is obviously signed, use the sext cast value. |
--- |
2202 |
// If the expression is obviously signed, use the sext cast value. |
--- |
| 2203 |
if (isa(Op)) |
0 |
2203 |
if (isa(Op)) |
0 |
| 2204 |
return SExt; |
0 |
2204 |
return SExt; |
0 |
| 2205 |
|
--- |
2205 |
|
--- |
| 2206 |
// Absent any other information, use the zext cast value. |
--- |
2206 |
// Absent any other information, use the zext cast value. |
--- |
| 2207 |
return ZExt; |
0 |
2207 |
return ZExt; |
0 |
| 2208 |
} |
--- |
2208 |
} |
--- |
| 2209 |
|
--- |
2209 |
|
--- |
| 2210 |
/// Process the given Ops list, which is a list of operands to be added under |
--- |
2210 |
/// Process the given Ops list, which is a list of operands to be added under |
--- |
| 2211 |
/// the given scale, update the given map. This is a helper function for |
--- |
2211 |
/// the given scale, update the given map. This is a helper function for |
--- |
| 2212 |
/// getAddRecExpr. As an example of what it does, given a sequence of operands |
--- |
2212 |
/// getAddRecExpr. As an example of what it does, given a sequence of operands |
--- |
| 2213 |
/// that would form an add expression like this: |
--- |
2213 |
/// that would form an add expression like this: |
--- |
| 2214 |
/// |
--- |
2214 |
/// |
--- |
| 2215 |
/// m + n + 13 + (A * (o + p + (B * (q + m + 29)))) + r + (-1 * r) |
--- |
2215 |
/// m + n + 13 + (A * (o + p + (B * (q + m + 29)))) + r + (-1 * r) |
--- |
| 2216 |
/// |
--- |
2216 |
/// |
--- |
| 2217 |
/// where A and B are constants, update the map with these values: |
--- |
2217 |
/// where A and B are constants, update the map with these values: |
--- |
| 2218 |
/// |
--- |
2218 |
/// |
--- |
| 2219 |
/// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) |
--- |
2219 |
/// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) |
--- |
| 2220 |
/// |
--- |
2220 |
/// |
--- |
| 2221 |
/// and add 13 + A*B*29 to AccumulatedConstant. |
--- |
2221 |
/// and add 13 + A*B*29 to AccumulatedConstant. |
--- |
| 2222 |
/// This will allow getAddRecExpr to produce this: |
--- |
2222 |
/// This will allow getAddRecExpr to produce this: |
--- |
| 2223 |
/// |
--- |
2223 |
/// |
--- |
| 2224 |
/// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) |
--- |
2224 |
/// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) |
--- |
| 2225 |
/// |
--- |
2225 |
/// |
--- |
| 2226 |
/// This form often exposes folding opportunities that are hidden in |
--- |
2226 |
/// This form often exposes folding opportunities that are hidden in |
--- |
| 2227 |
/// the original operand list. |
--- |
2227 |
/// the original operand list. |
--- |
| 2228 |
/// |
--- |
2228 |
/// |
--- |
| 2229 |
/// Return true iff it appears that any interesting folding opportunities |
--- |
2229 |
/// Return true iff it appears that any interesting folding opportunities |
--- |
| 2230 |
/// may be exposed. This helps getAddRecExpr short-circuit extra work in |
--- |
2230 |
/// may be exposed. This helps getAddRecExpr short-circuit extra work in |
--- |
| 2231 |
/// the common case where no interesting opportunities are present, and |
--- |
2231 |
/// the common case where no interesting opportunities are present, and |
--- |
| 2232 |
/// is also used as a check to avoid infinite recursion. |
--- |
2232 |
/// is also used as a check to avoid infinite recursion. |
--- |
| 2233 |
static bool |
--- |
2233 |
static bool |
--- |
| 2234 |
CollectAddOperandsWithScales(DenseMap &M, |
0 |
2234 |
CollectAddOperandsWithScales(DenseMap &M, |
0 |
| 2235 |
SmallVectorImpl &NewOps, |
--- |
2235 |
SmallVectorImpl &NewOps, |
--- |
| 2236 |
APInt &AccumulatedConstant, |
--- |
2236 |
APInt &AccumulatedConstant, |
--- |
| 2237 |
ArrayRef Ops, const APInt &Scale, |
--- |
2237 |
ArrayRef Ops, const APInt &Scale, |
--- |
| 2238 |
ScalarEvolution &SE) { |
--- |
2238 |
ScalarEvolution &SE) { |
--- |
| 2239 |
bool Interesting = false; |
0 |
2239 |
bool Interesting = false; |
0 |
| 2240 |
|
--- |
2240 |
|
--- |
| 2241 |
// Iterate over the add operands. They are sorted, with constants first. |
--- |
2241 |
// Iterate over the add operands. They are sorted, with constants first. |
--- |
| 2242 |
unsigned i = 0; |
0 |
2242 |
unsigned i = 0; |
0 |
| 2243 |
while (const SCEVConstant *C = dyn_cast(Ops[i])) { |
0 |
2243 |
while (const SCEVConstant *C = dyn_cast(Ops[i])) { |
0 |
| 2244 |
++i; |
0 |
2244 |
++i; |
0 |
| 2245 |
// Pull a buried constant out to the outside. |
--- |
2245 |
// Pull a buried constant out to the outside. |
--- |
| 2246 |
if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero()) |
0 |
2246 |
if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero()) |
0 |
| 2247 |
Interesting = true; |
0 |
2247 |
Interesting = true; |
0 |
| 2248 |
AccumulatedConstant += Scale * C->getAPInt(); |
0 |
2248 |
AccumulatedConstant += Scale * C->getAPInt(); |
0 |
| 2249 |
} |
0 |
2249 |
} |
0 |
| 2250 |
|
--- |
2250 |
|
--- |
| 2251 |
// Next comes everything else. We're especially interested in multiplies |
--- |
2251 |
// Next comes everything else. We're especially interested in multiplies |
--- |
| 2252 |
// here, but they're in the middle, so just visit the rest with one loop. |
--- |
2252 |
// here, but they're in the middle, so just visit the rest with one loop. |
--- |
| 2253 |
for (; i != Ops.size(); ++i) { |
0 |
2253 |
for (; i != Ops.size(); ++i) { |
0 |
| 2254 |
const SCEVMulExpr *Mul = dyn_cast(Ops[i]); |
0 |
2254 |
const SCEVMulExpr *Mul = dyn_cast(Ops[i]); |
0 |
| 2255 |
if (Mul && isa(Mul->getOperand(0))) { |
0 |
2255 |
if (Mul && isa(Mul->getOperand(0))) { |
0 |
| 2256 |
APInt NewScale = |
--- |
2256 |
APInt NewScale = |
--- |
| 2257 |
Scale * cast(Mul->getOperand(0))->getAPInt(); |
0 |
2257 |
Scale * cast(Mul->getOperand(0))->getAPInt(); |
0 |
| 2258 |
if (Mul->getNumOperands() == 2 && isa(Mul->getOperand(1))) { |
0 |
2258 |
if (Mul->getNumOperands() == 2 && isa(Mul->getOperand(1))) { |
0 |
| 2259 |
// A multiplication of a constant with another add; recurse. |
--- |
2259 |
// A multiplication of a constant with another add; recurse. |
--- |
| 2260 |
const SCEVAddExpr *Add = cast(Mul->getOperand(1)); |
0 |
2260 |
const SCEVAddExpr *Add = cast(Mul->getOperand(1)); |
0 |
| 2261 |
Interesting |= |
0 |
2261 |
Interesting |= |
0 |
| 2262 |
CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
0 |
2262 |
CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
0 |
| 2263 |
Add->operands(), NewScale, SE); |
--- |
2263 |
Add->operands(), NewScale, SE); |
--- |
| 2264 |
} else { |
--- |
2264 |
} else { |
--- |
| 2265 |
// A multiplication of a constant with some other value. Update |
--- |
2265 |
// A multiplication of a constant with some other value. Update |
--- |
| 2266 |
// the map. |
--- |
2266 |
// the map. |
--- |
| 2267 |
SmallVector MulOps(drop_begin(Mul->operands())); |
0 |
2267 |
SmallVector MulOps(drop_begin(Mul->operands())); |
0 |
| 2268 |
const SCEV *Key = SE.getMulExpr(MulOps); |
0 |
2268 |
const SCEV *Key = SE.getMulExpr(MulOps); |
0 |
| 2269 |
auto Pair = M.insert({Key, NewScale}); |
0 |
2269 |
auto Pair = M.insert({Key, NewScale}); |
0 |
| 2270 |
if (Pair.second) { |
0 |
2270 |
if (Pair.second) { |
0 |
| 2271 |
NewOps.push_back(Pair.first->first); |
0 |
2271 |
NewOps.push_back(Pair.first->first); |
0 |
| 2272 |
} else { |
--- |
2272 |
} else { |
--- |
| 2273 |
Pair.first->second += NewScale; |
0 |
2273 |
Pair.first->second += NewScale; |
0 |
| 2274 |
// The map already had an entry for this value, which may indicate |
--- |
2274 |
// The map already had an entry for this value, which may indicate |
--- |
| 2275 |
// a folding opportunity. |
--- |
2275 |
// a folding opportunity. |
--- |
| 2276 |
Interesting = true; |
0 |
2276 |
Interesting = true; |
0 |
| 2277 |
} |
--- |
2277 |
} |
--- |
| 2278 |
} |
0 |
2278 |
} |
0 |
| 2279 |
} else { |
0 |
2279 |
} else { |
0 |
| 2280 |
// An ordinary operand. Update the map. |
--- |
2280 |
// An ordinary operand. Update the map. |
--- |
| 2281 |
std::pair::iterator, bool> Pair = |
--- |
2281 |
std::pair::iterator, bool> Pair = |
--- |
| 2282 |
M.insert({Ops[i], Scale}); |
0 |
2282 |
M.insert({Ops[i], Scale}); |
0 |
| 2283 |
if (Pair.second) { |
0 |
2283 |
if (Pair.second) { |
0 |
| 2284 |
NewOps.push_back(Pair.first->first); |
0 |
2284 |
NewOps.push_back(Pair.first->first); |
0 |
| 2285 |
} else { |
--- |
2285 |
} else { |
--- |
| 2286 |
Pair.first->second += Scale; |
0 |
2286 |
Pair.first->second += Scale; |
0 |
| 2287 |
// The map already had an entry for this value, which may indicate |
--- |
2287 |
// The map already had an entry for this value, which may indicate |
--- |
| 2288 |
// a folding opportunity. |
--- |
2288 |
// a folding opportunity. |
--- |
| 2289 |
Interesting = true; |
0 |
2289 |
Interesting = true; |
0 |
| 2290 |
} |
--- |
2290 |
} |
--- |
| 2291 |
} |
--- |
2291 |
} |
--- |
| 2292 |
} |
--- |
2292 |
} |
--- |
| 2293 |
|
--- |
2293 |
|
--- |
| 2294 |
return Interesting; |
0 |
2294 |
return Interesting; |
0 |
| 2295 |
} |
--- |
2295 |
} |
--- |
| 2296 |
|
--- |
2296 |
|
--- |
| 2297 |
bool ScalarEvolution::willNotOverflow(Instruction::BinaryOps BinOp, bool Signed, |
0 |
2297 |
bool ScalarEvolution::willNotOverflow(Instruction::BinaryOps BinOp, bool Signed, |
0 |
| 2298 |
const SCEV *LHS, const SCEV *RHS, |
--- |
2298 |
const SCEV *LHS, const SCEV *RHS, |
--- |
| 2299 |
const Instruction *CtxI) { |
--- |
2299 |
const Instruction *CtxI) { |
--- |
| 2300 |
const SCEV *(ScalarEvolution::*Operation)(const SCEV *, const SCEV *, |
--- |
2300 |
const SCEV *(ScalarEvolution::*Operation)(const SCEV *, const SCEV *, |
--- |
| 2301 |
SCEV::NoWrapFlags, unsigned); |
--- |
2301 |
SCEV::NoWrapFlags, unsigned); |
--- |
| 2302 |
switch (BinOp) { |
0 |
2302 |
switch (BinOp) { |
0 |
| 2303 |
default: |
0 |
2303 |
default: |
0 |
| 2304 |
llvm_unreachable("Unsupported binary op"); |
0 |
2304 |
llvm_unreachable("Unsupported binary op"); |
0 |
| 2305 |
case Instruction::Add: |
0 |
2305 |
case Instruction::Add: |
0 |
| 2306 |
Operation = &ScalarEvolution::getAddExpr; |
0 |
2306 |
Operation = &ScalarEvolution::getAddExpr; |
0 |
| 2307 |
break; |
0 |
2307 |
break; |
0 |
| 2308 |
case Instruction::Sub: |
0 |
2308 |
case Instruction::Sub: |
0 |
| 2309 |
Operation = &ScalarEvolution::getMinusSCEV; |
0 |
2309 |
Operation = &ScalarEvolution::getMinusSCEV; |
0 |
| 2310 |
break; |
0 |
2310 |
break; |
0 |
| 2311 |
case Instruction::Mul: |
0 |
2311 |
case Instruction::Mul: |
0 |
| 2312 |
Operation = &ScalarEvolution::getMulExpr; |
0 |
2312 |
Operation = &ScalarEvolution::getMulExpr; |
0 |
| 2313 |
break; |
0 |
2313 |
break; |
0 |
| 2314 |
} |
--- |
2314 |
} |
--- |
| 2315 |
|
--- |
2315 |
|
--- |
| 2316 |
const SCEV *(ScalarEvolution::*Extension)(const SCEV *, Type *, unsigned) = |
0 |
2316 |
const SCEV *(ScalarEvolution::*Extension)(const SCEV *, Type *, unsigned) = |
0 |
| 2317 |
Signed ? &ScalarEvolution::getSignExtendExpr |
--- |
2317 |
Signed ? &ScalarEvolution::getSignExtendExpr |
--- |
| 2318 |
: &ScalarEvolution::getZeroExtendExpr; |
--- |
2318 |
: &ScalarEvolution::getZeroExtendExpr; |
--- |
| 2319 |
|
--- |
2319 |
|
--- |
| 2320 |
// Check ext(LHS op RHS) == ext(LHS) op ext(RHS) |
--- |
2320 |
// Check ext(LHS op RHS) == ext(LHS) op ext(RHS) |
--- |
| 2321 |
auto *NarrowTy = cast(LHS->getType()); |
0 |
2321 |
auto *NarrowTy = cast(LHS->getType()); |
0 |
| 2322 |
auto *WideTy = |
--- |
2322 |
auto *WideTy = |
--- |
| 2323 |
IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2); |
0 |
2323 |
IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2); |
0 |
| 2324 |
|
--- |
2324 |
|
--- |
| 2325 |
const SCEV *A = (this->*Extension)( |
0 |
2325 |
const SCEV *A = (this->*Extension)( |
0 |
| 2326 |
(this->*Operation)(LHS, RHS, SCEV::FlagAnyWrap, 0), WideTy, 0); |
0 |
2326 |
(this->*Operation)(LHS, RHS, SCEV::FlagAnyWrap, 0), WideTy, 0); |
0 |
| 2327 |
const SCEV *LHSB = (this->*Extension)(LHS, WideTy, 0); |
0 |
2327 |
const SCEV *LHSB = (this->*Extension)(LHS, WideTy, 0); |
0 |
| 2328 |
const SCEV *RHSB = (this->*Extension)(RHS, WideTy, 0); |
0 |
2328 |
const SCEV *RHSB = (this->*Extension)(RHS, WideTy, 0); |
0 |
| 2329 |
const SCEV *B = (this->*Operation)(LHSB, RHSB, SCEV::FlagAnyWrap, 0); |
0 |
2329 |
const SCEV *B = (this->*Operation)(LHSB, RHSB, SCEV::FlagAnyWrap, 0); |
0 |
| 2330 |
if (A == B) |
0 |
2330 |
if (A == B) |
0 |
| 2331 |
return true; |
0 |
2331 |
return true; |
0 |
| 2332 |
// Can we use context to prove the fact we need? |
--- |
2332 |
// Can we use context to prove the fact we need? |
--- |
| 2333 |
if (!CtxI) |
0 |
2333 |
if (!CtxI) |
0 |
| 2334 |
return false; |
0 |
2334 |
return false; |
0 |
| 2335 |
// TODO: Support mul. |
--- |
2335 |
// TODO: Support mul. |
--- |
| 2336 |
if (BinOp == Instruction::Mul) |
0 |
2336 |
if (BinOp == Instruction::Mul) |
0 |
| 2337 |
return false; |
0 |
2337 |
return false; |
0 |
| 2338 |
auto *RHSC = dyn_cast(RHS); |
0 |
2338 |
auto *RHSC = dyn_cast(RHS); |
0 |
| 2339 |
// TODO: Lift this limitation. |
--- |
2339 |
// TODO: Lift this limitation. |
--- |
| 2340 |
if (!RHSC) |
0 |
2340 |
if (!RHSC) |
0 |
| 2341 |
return false; |
0 |
2341 |
return false; |
0 |
| 2342 |
APInt C = RHSC->getAPInt(); |
0 |
2342 |
APInt C = RHSC->getAPInt(); |
0 |
| 2343 |
unsigned NumBits = C.getBitWidth(); |
0 |
2343 |
unsigned NumBits = C.getBitWidth(); |
0 |
| 2344 |
bool IsSub = (BinOp == Instruction::Sub); |
0 |
2344 |
bool IsSub = (BinOp == Instruction::Sub); |
0 |
| 2345 |
bool IsNegativeConst = (Signed && C.isNegative()); |
0 |
2345 |
bool IsNegativeConst = (Signed && C.isNegative()); |
0 |
| 2346 |
// Compute the direction and magnitude by which we need to check overflow. |
--- |
2346 |
// Compute the direction and magnitude by which we need to check overflow. |
--- |
| 2347 |
bool OverflowDown = IsSub ^ IsNegativeConst; |
0 |
2347 |
bool OverflowDown = IsSub ^ IsNegativeConst; |
0 |
| 2348 |
APInt Magnitude = C; |
0 |
2348 |
APInt Magnitude = C; |
0 |
| 2349 |
if (IsNegativeConst) { |
0 |
2349 |
if (IsNegativeConst) { |
0 |
| 2350 |
if (C == APInt::getSignedMinValue(NumBits)) |
0 |
2350 |
if (C == APInt::getSignedMinValue(NumBits)) |
0 |
| 2351 |
// TODO: SINT_MIN on inversion gives the same negative value, we don't |
--- |
2351 |
// TODO: SINT_MIN on inversion gives the same negative value, we don't |
--- |
| 2352 |
// want to deal with that. |
--- |
2352 |
// want to deal with that. |
--- |
| 2353 |
return false; |
0 |
2353 |
return false; |
0 |
| 2354 |
Magnitude = -C; |
0 |
2354 |
Magnitude = -C; |
0 |
| 2355 |
} |
--- |
2355 |
} |
--- |
| 2356 |
|
--- |
2356 |
|
--- |
| 2357 |
ICmpInst::Predicate Pred = Signed ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; |
0 |
2357 |
ICmpInst::Predicate Pred = Signed ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; |
0 |
| 2358 |
if (OverflowDown) { |
0 |
2358 |
if (OverflowDown) { |
0 |
| 2359 |
// To avoid overflow down, we need to make sure that MIN + Magnitude <= LHS. |
--- |
2359 |
// To avoid overflow down, we need to make sure that MIN + Magnitude <= LHS. |
--- |
| 2360 |
APInt Min = Signed ? APInt::getSignedMinValue(NumBits) |
--- |
2360 |
APInt Min = Signed ? APInt::getSignedMinValue(NumBits) |
--- |
| 2361 |
: APInt::getMinValue(NumBits); |
0 |
2361 |
: APInt::getMinValue(NumBits); |
0 |
| 2362 |
APInt Limit = Min + Magnitude; |
0 |
2362 |
APInt Limit = Min + Magnitude; |
0 |
| 2363 |
return isKnownPredicateAt(Pred, getConstant(Limit), LHS, CtxI); |
0 |
2363 |
return isKnownPredicateAt(Pred, getConstant(Limit), LHS, CtxI); |
0 |
| 2364 |
} else { |
0 |
2364 |
} else { |
0 |
| 2365 |
// To avoid overflow up, we need to make sure that LHS <= MAX - Magnitude. |
--- |
2365 |
// To avoid overflow up, we need to make sure that LHS <= MAX - Magnitude. |
--- |
| 2366 |
APInt Max = Signed ? APInt::getSignedMaxValue(NumBits) |
--- |
2366 |
APInt Max = Signed ? APInt::getSignedMaxValue(NumBits) |
--- |
| 2367 |
: APInt::getMaxValue(NumBits); |
0 |
2367 |
: APInt::getMaxValue(NumBits); |
0 |
| 2368 |
APInt Limit = Max - Magnitude; |
0 |
2368 |
APInt Limit = Max - Magnitude; |
0 |
| 2369 |
return isKnownPredicateAt(Pred, LHS, getConstant(Limit), CtxI); |
0 |
2369 |
return isKnownPredicateAt(Pred, LHS, getConstant(Limit), CtxI); |
0 |
| 2370 |
} |
0 |
2370 |
} |
0 |
| 2371 |
} |
--- |
2371 |
} |
--- |
| 2372 |
|
--- |
2372 |
|
--- |
| 2373 |
std::optional |
--- |
2373 |
std::optional |
--- |
| 2374 |
ScalarEvolution::getStrengthenedNoWrapFlagsFromBinOp( |
0 |
2374 |
ScalarEvolution::getStrengthenedNoWrapFlagsFromBinOp( |
0 |
| 2375 |
const OverflowingBinaryOperator *OBO) { |
--- |
2375 |
const OverflowingBinaryOperator *OBO) { |
--- |
| 2376 |
// It cannot be done any better. |
--- |
2376 |
// It cannot be done any better. |
--- |
| 2377 |
if (OBO->hasNoUnsignedWrap() && OBO->hasNoSignedWrap()) |
0 |
2377 |
if (OBO->hasNoUnsignedWrap() && OBO->hasNoSignedWrap()) |
0 |
| 2378 |
return std::nullopt; |
0 |
2378 |
return std::nullopt; |
0 |
| 2379 |
|
--- |
2379 |
|
--- |
| 2380 |
SCEV::NoWrapFlags Flags = SCEV::NoWrapFlags::FlagAnyWrap; |
0 |
2380 |
SCEV::NoWrapFlags Flags = SCEV::NoWrapFlags::FlagAnyWrap; |
0 |
| 2381 |
|
--- |
2381 |
|
--- |
| 2382 |
if (OBO->hasNoUnsignedWrap()) |
0 |
2382 |
if (OBO->hasNoUnsignedWrap()) |
0 |
| 2383 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); |
0 |
2383 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); |
0 |
| 2384 |
if (OBO->hasNoSignedWrap()) |
0 |
2384 |
if (OBO->hasNoSignedWrap()) |
0 |
| 2385 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); |
0 |
2385 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); |
0 |
| 2386 |
|
--- |
2386 |
|
--- |
| 2387 |
bool Deduced = false; |
0 |
2387 |
bool Deduced = false; |
0 |
| 2388 |
|
--- |
2388 |
|
--- |
| 2389 |
if (OBO->getOpcode() != Instruction::Add && |
0 |
2389 |
if (OBO->getOpcode() != Instruction::Add && |
0 |
| 2390 |
OBO->getOpcode() != Instruction::Sub && |
0 |
2390 |
OBO->getOpcode() != Instruction::Sub && |
0 |
| 2391 |
OBO->getOpcode() != Instruction::Mul) |
0 |
2391 |
OBO->getOpcode() != Instruction::Mul) |
0 |
| 2392 |
return std::nullopt; |
0 |
2392 |
return std::nullopt; |
0 |
| 2393 |
|
--- |
2393 |
|
--- |
| 2394 |
const SCEV *LHS = getSCEV(OBO->getOperand(0)); |
0 |
2394 |
const SCEV *LHS = getSCEV(OBO->getOperand(0)); |
0 |
| 2395 |
const SCEV *RHS = getSCEV(OBO->getOperand(1)); |
0 |
2395 |
const SCEV *RHS = getSCEV(OBO->getOperand(1)); |
0 |
| 2396 |
|
--- |
2396 |
|
--- |
| 2397 |
const Instruction *CtxI = |
--- |
2397 |
const Instruction *CtxI = |
--- |
| 2398 |
UseContextForNoWrapFlagInference ? dyn_cast(OBO) : nullptr; |
0 |
2398 |
UseContextForNoWrapFlagInference ? dyn_cast(OBO) : nullptr; |
0 |
| 2399 |
if (!OBO->hasNoUnsignedWrap() && |
0 |
2399 |
if (!OBO->hasNoUnsignedWrap() && |
0 |
| 2400 |
willNotOverflow((Instruction::BinaryOps)OBO->getOpcode(), |
0 |
2400 |
willNotOverflow((Instruction::BinaryOps)OBO->getOpcode(), |
0 |
| 2401 |
/* Signed */ false, LHS, RHS, CtxI)) { |
--- |
2401 |
/* Signed */ false, LHS, RHS, CtxI)) { |
--- |
| 2402 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); |
0 |
2402 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); |
0 |
| 2403 |
Deduced = true; |
0 |
2403 |
Deduced = true; |
0 |
| 2404 |
} |
--- |
2404 |
} |
--- |
| 2405 |
|
--- |
2405 |
|
--- |
| 2406 |
if (!OBO->hasNoSignedWrap() && |
0 |
2406 |
if (!OBO->hasNoSignedWrap() && |
0 |
| 2407 |
willNotOverflow((Instruction::BinaryOps)OBO->getOpcode(), |
0 |
2407 |
willNotOverflow((Instruction::BinaryOps)OBO->getOpcode(), |
0 |
| 2408 |
/* Signed */ true, LHS, RHS, CtxI)) { |
--- |
2408 |
/* Signed */ true, LHS, RHS, CtxI)) { |
--- |
| 2409 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); |
0 |
2409 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); |
0 |
| 2410 |
Deduced = true; |
0 |
2410 |
Deduced = true; |
0 |
| 2411 |
} |
--- |
2411 |
} |
--- |
| 2412 |
|
--- |
2412 |
|
--- |
| 2413 |
if (Deduced) |
0 |
2413 |
if (Deduced) |
0 |
| 2414 |
return Flags; |
0 |
2414 |
return Flags; |
0 |
| 2415 |
return std::nullopt; |
0 |
2415 |
return std::nullopt; |
0 |
| 2416 |
} |
--- |
2416 |
} |
--- |
| 2417 |
|
--- |
2417 |
|
--- |
| 2418 |
// We're trying to construct a SCEV of type `Type' with `Ops' as operands and |
--- |
2418 |
// We're trying to construct a SCEV of type `Type' with `Ops' as operands and |
--- |
| 2419 |
// `OldFlags' as can't-wrap behavior. Infer a more aggressive set of |
--- |
2419 |
// `OldFlags' as can't-wrap behavior. Infer a more aggressive set of |
--- |
| 2420 |
// can't-overflow flags for the operation if possible. |
--- |
2420 |
// can't-overflow flags for the operation if possible. |
--- |
| 2421 |
static SCEV::NoWrapFlags |
--- |
2421 |
static SCEV::NoWrapFlags |
--- |
| 2422 |
StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type, |
0 |
2422 |
StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type, |
0 |
| 2423 |
const ArrayRef Ops, |
--- |
2423 |
const ArrayRef Ops, |
--- |
| 2424 |
SCEV::NoWrapFlags Flags) { |
--- |
2424 |
SCEV::NoWrapFlags Flags) { |
--- |
| 2425 |
using namespace std::placeholders; |
--- |
2425 |
using namespace std::placeholders; |
--- |
| 2426 |
|
--- |
2426 |
|
--- |
| 2427 |
using OBO = OverflowingBinaryOperator; |
--- |
2427 |
using OBO = OverflowingBinaryOperator; |
--- |
| 2428 |
|
--- |
2428 |
|
--- |
| 2429 |
bool CanAnalyze = |
0 |
2429 |
bool CanAnalyze = |
0 |
| 2430 |
Type == scAddExpr || Type == scAddRecExpr || Type == scMulExpr; |
0 |
2430 |
Type == scAddExpr || Type == scAddRecExpr || Type == scMulExpr; |
0 |
| 2431 |
(void)CanAnalyze; |
--- |
2431 |
(void)CanAnalyze; |
--- |
| 2432 |
assert(CanAnalyze && "don't call from other places!"); |
0 |
2432 |
assert(CanAnalyze && "don't call from other places!"); |
0 |
| 2433 |
|
--- |
2433 |
|
--- |
| 2434 |
int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW; |
0 |
2434 |
int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW; |
0 |
| 2435 |
SCEV::NoWrapFlags SignOrUnsignWrap = |
--- |
2435 |
SCEV::NoWrapFlags SignOrUnsignWrap = |
--- |
| 2436 |
ScalarEvolution::maskFlags(Flags, SignOrUnsignMask); |
0 |
2436 |
ScalarEvolution::maskFlags(Flags, SignOrUnsignMask); |
0 |
| 2437 |
|
--- |
2437 |
|
--- |
| 2438 |
// If FlagNSW is true and all the operands are non-negative, infer FlagNUW. |
--- |
2438 |
// If FlagNSW is true and all the operands are non-negative, infer FlagNUW. |
--- |
| 2439 |
auto IsKnownNonNegative = [&](const SCEV *S) { |
0 |
2439 |
auto IsKnownNonNegative = [&](const SCEV *S) { |
0 |
| 2440 |
return SE->isKnownNonNegative(S); |
0 |
2440 |
return SE->isKnownNonNegative(S); |
0 |
| 2441 |
}; |
0 |
2441 |
}; |
0 |
| 2442 |
|
--- |
2442 |
|
--- |
| 2443 |
if (SignOrUnsignWrap == SCEV::FlagNSW && all_of(Ops, IsKnownNonNegative)) |
0 |
2443 |
if (SignOrUnsignWrap == SCEV::FlagNSW && all_of(Ops, IsKnownNonNegative)) |
0 |
| 2444 |
Flags = |
--- |
2444 |
Flags = |
--- |
| 2445 |
ScalarEvolution::setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask); |
0 |
2445 |
ScalarEvolution::setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask); |
0 |
| 2446 |
|
--- |
2446 |
|
--- |
| 2447 |
SignOrUnsignWrap = ScalarEvolution::maskFlags(Flags, SignOrUnsignMask); |
0 |
2447 |
SignOrUnsignWrap = ScalarEvolution::maskFlags(Flags, SignOrUnsignMask); |
0 |
| 2448 |
|
--- |
2448 |
|
--- |
| 2449 |
if (SignOrUnsignWrap != SignOrUnsignMask && |
0 |
2449 |
if (SignOrUnsignWrap != SignOrUnsignMask && |
0 |
| 2450 |
(Type == scAddExpr || Type == scMulExpr) && Ops.size() == 2 && |
0 |
2450 |
(Type == scAddExpr || Type == scMulExpr) && Ops.size() == 2 && |
0 |
| 2451 |
isa(Ops[0])) { |
0 |
2451 |
isa(Ops[0])) { |
0 |
| 2452 |
|
--- |
2452 |
|
--- |
| 2453 |
auto Opcode = [&] { |
0 |
2453 |
auto Opcode = [&] { |
0 |
| 2454 |
switch (Type) { |
0 |
2454 |
switch (Type) { |
0 |
| 2455 |
case scAddExpr: |
0 |
2455 |
case scAddExpr: |
0 |
| 2456 |
return Instruction::Add; |
0 |
2456 |
return Instruction::Add; |
0 |
| 2457 |
case scMulExpr: |
0 |
2457 |
case scMulExpr: |
0 |
| 2458 |
return Instruction::Mul; |
0 |
2458 |
return Instruction::Mul; |
0 |
| 2459 |
default: |
0 |
2459 |
default: |
0 |
| 2460 |
llvm_unreachable("Unexpected SCEV op."); |
0 |
2460 |
llvm_unreachable("Unexpected SCEV op."); |
0 |
| 2461 |
} |
--- |
2461 |
} |
--- |
| 2462 |
}(); |
0 |
2462 |
}(); |
0 |
| 2463 |
|
--- |
2463 |
|
--- |
| 2464 |
const APInt &C = cast(Ops[0])->getAPInt(); |
0 |
2464 |
const APInt &C = cast(Ops[0])->getAPInt(); |
0 |
| 2465 |
|
--- |
2465 |
|
--- |
| 2466 |
// (A C) --> (A C) if the op doesn't sign overflow. |
--- |
2466 |
// (A C) --> (A C) if the op doesn't sign overflow. |
--- |
| 2467 |
if (!(SignOrUnsignWrap & SCEV::FlagNSW)) { |
0 |
2467 |
if (!(SignOrUnsignWrap & SCEV::FlagNSW)) { |
0 |
| 2468 |
auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion( |
--- |
2468 |
auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion( |
--- |
| 2469 |
Opcode, C, OBO::NoSignedWrap); |
0 |
2469 |
Opcode, C, OBO::NoSignedWrap); |
0 |
| 2470 |
if (NSWRegion.contains(SE->getSignedRange(Ops[1]))) |
0 |
2470 |
if (NSWRegion.contains(SE->getSignedRange(Ops[1]))) |
0 |
| 2471 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); |
0 |
2471 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); |
0 |
| 2472 |
} |
0 |
2472 |
} |
0 |
| 2473 |
|
--- |
2473 |
|
--- |
| 2474 |
// (A C) --> (A C) if the op doesn't unsign overflow. |
--- |
2474 |
// (A C) --> (A C) if the op doesn't unsign overflow. |
--- |
| 2475 |
if (!(SignOrUnsignWrap & SCEV::FlagNUW)) { |
0 |
2475 |
if (!(SignOrUnsignWrap & SCEV::FlagNUW)) { |
0 |
| 2476 |
auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion( |
--- |
2476 |
auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion( |
--- |
| 2477 |
Opcode, C, OBO::NoUnsignedWrap); |
0 |
2477 |
Opcode, C, OBO::NoUnsignedWrap); |
0 |
| 2478 |
if (NUWRegion.contains(SE->getUnsignedRange(Ops[1]))) |
0 |
2478 |
if (NUWRegion.contains(SE->getUnsignedRange(Ops[1]))) |
0 |
| 2479 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); |
0 |
2479 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); |
0 |
| 2480 |
} |
0 |
2480 |
} |
0 |
| 2481 |
} |
--- |
2481 |
} |
--- |
| 2482 |
|
--- |
2482 |
|
--- |
| 2483 |
// <0,+,nonnegative> is also nuw |
--- |
2483 |
// <0,+,nonnegative> is also nuw |
--- |
| 2484 |
// TODO: Add corresponding nsw case |
--- |
2484 |
// TODO: Add corresponding nsw case |
--- |
| 2485 |
if (Type == scAddRecExpr && ScalarEvolution::hasFlags(Flags, SCEV::FlagNW) && |
0 |
2485 |
if (Type == scAddRecExpr && ScalarEvolution::hasFlags(Flags, SCEV::FlagNW) && |
0 |
| 2486 |
!ScalarEvolution::hasFlags(Flags, SCEV::FlagNUW) && Ops.size() == 2 && |
0 |
2486 |
!ScalarEvolution::hasFlags(Flags, SCEV::FlagNUW) && Ops.size() == 2 && |
0 |
| 2487 |
Ops[0]->isZero() && IsKnownNonNegative(Ops[1])) |
0 |
2487 |
Ops[0]->isZero() && IsKnownNonNegative(Ops[1])) |
0 |
| 2488 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); |
0 |
2488 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); |
0 |
| 2489 |
|
--- |
2489 |
|
--- |
| 2490 |
// both (udiv X, Y) * Y and Y * (udiv X, Y) are always NUW |
--- |
2490 |
// both (udiv X, Y) * Y and Y * (udiv X, Y) are always NUW |
--- |
| 2491 |
if (Type == scMulExpr && !ScalarEvolution::hasFlags(Flags, SCEV::FlagNUW) && |
0 |
2491 |
if (Type == scMulExpr && !ScalarEvolution::hasFlags(Flags, SCEV::FlagNUW) && |
0 |
| 2492 |
Ops.size() == 2) { |
0 |
2492 |
Ops.size() == 2) { |
0 |
| 2493 |
if (auto *UDiv = dyn_cast(Ops[0])) |
0 |
2493 |
if (auto *UDiv = dyn_cast(Ops[0])) |
0 |
| 2494 |
if (UDiv->getOperand(1) == Ops[1]) |
0 |
2494 |
if (UDiv->getOperand(1) == Ops[1]) |
0 |
| 2495 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); |
0 |
2495 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); |
0 |
| 2496 |
if (auto *UDiv = dyn_cast(Ops[1])) |
0 |
2496 |
if (auto *UDiv = dyn_cast(Ops[1])) |
0 |
| 2497 |
if (UDiv->getOperand(1) == Ops[0]) |
0 |
2497 |
if (UDiv->getOperand(1) == Ops[0]) |
0 |
| 2498 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); |
0 |
2498 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); |
0 |
| 2499 |
} |
--- |
2499 |
} |
--- |
| 2500 |
|
--- |
2500 |
|
--- |
| 2501 |
return Flags; |
0 |
2501 |
return Flags; |
0 |
| 2502 |
} |
--- |
2502 |
} |
--- |
| 2503 |
|
--- |
2503 |
|
--- |
| 2504 |
bool ScalarEvolution::isAvailableAtLoopEntry(const SCEV *S, const Loop *L) { |
0 |
2504 |
bool ScalarEvolution::isAvailableAtLoopEntry(const SCEV *S, const Loop *L) { |
0 |
| 2505 |
return isLoopInvariant(S, L) && properlyDominates(S, L->getHeader()); |
0 |
2505 |
return isLoopInvariant(S, L) && properlyDominates(S, L->getHeader()); |
0 |
| 2506 |
} |
--- |
2506 |
} |
--- |
| 2507 |
|
--- |
2507 |
|
--- |
| 2508 |
/// Get a canonical add expression, or something simpler if possible. |
--- |
2508 |
/// Get a canonical add expression, or something simpler if possible. |
--- |
| 2509 |
const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl &Ops, |
0 |
2509 |
const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl &Ops, |
0 |
| 2510 |
SCEV::NoWrapFlags OrigFlags, |
--- |
2510 |
SCEV::NoWrapFlags OrigFlags, |
--- |
| 2511 |
unsigned Depth) { |
--- |
2511 |
unsigned Depth) { |
--- |
| 2512 |
assert(!(OrigFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) && |
0 |
2512 |
assert(!(OrigFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) && |
0 |
| 2513 |
"only nuw or nsw allowed"); |
--- |
2513 |
"only nuw or nsw allowed"); |
--- |
| 2514 |
assert(!Ops.empty() && "Cannot get empty add!"); |
0 |
2514 |
assert(!Ops.empty() && "Cannot get empty add!"); |
0 |
| 2515 |
if (Ops.size() == 1) return Ops[0]; |
0 |
2515 |
if (Ops.size() == 1) return Ops[0]; |
0 |
| 2516 |
#ifndef NDEBUG |
--- |
2516 |
#ifndef NDEBUG |
--- |
| 2517 |
Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
0 |
2517 |
Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
0 |
| 2518 |
for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
0 |
2518 |
for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
0 |
| 2519 |
assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
0 |
2519 |
assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
0 |
| 2520 |
"SCEVAddExpr operand types don't match!"); |
--- |
2520 |
"SCEVAddExpr operand types don't match!"); |
--- |
| 2521 |
unsigned NumPtrs = count_if( |
0 |
2521 |
unsigned NumPtrs = count_if( |
0 |
| 2522 |
Ops, [](const SCEV *Op) { return Op->getType()->isPointerTy(); }); |
0 |
2522 |
Ops, [](const SCEV *Op) { return Op->getType()->isPointerTy(); }); |
0 |
| 2523 |
assert(NumPtrs <= 1 && "add has at most one pointer operand"); |
0 |
2523 |
assert(NumPtrs <= 1 && "add has at most one pointer operand"); |
0 |
| 2524 |
#endif |
--- |
2524 |
#endif |
--- |
| 2525 |
|
--- |
2525 |
|
--- |
| 2526 |
// Sort by complexity, this groups all similar expression types together. |
--- |
2526 |
// Sort by complexity, this groups all similar expression types together. |
--- |
| 2527 |
GroupByComplexity(Ops, &LI, DT); |
0 |
2527 |
GroupByComplexity(Ops, &LI, DT); |
0 |
| 2528 |
|
--- |
2528 |
|
--- |
| 2529 |
// If there are any constants, fold them together. |
--- |
2529 |
// If there are any constants, fold them together. |
--- |
| 2530 |
unsigned Idx = 0; |
0 |
2530 |
unsigned Idx = 0; |
0 |
| 2531 |
if (const SCEVConstant *LHSC = dyn_cast(Ops[0])) { |
0 |
2531 |
if (const SCEVConstant *LHSC = dyn_cast(Ops[0])) { |
0 |
| 2532 |
++Idx; |
0 |
2532 |
++Idx; |
0 |
| 2533 |
assert(Idx < Ops.size()); |
0 |
2533 |
assert(Idx < Ops.size()); |
0 |
| 2534 |
while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) { |
0 |
2534 |
while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) { |
0 |
| 2535 |
// We found two constants, fold them together! |
--- |
2535 |
// We found two constants, fold them together! |
--- |
| 2536 |
Ops[0] = getConstant(LHSC->getAPInt() + RHSC->getAPInt()); |
0 |
2536 |
Ops[0] = getConstant(LHSC->getAPInt() + RHSC->getAPInt()); |
0 |
| 2537 |
if (Ops.size() == 2) return Ops[0]; |
0 |
2537 |
if (Ops.size() == 2) return Ops[0]; |
0 |
| 2538 |
Ops.erase(Ops.begin()+1); // Erase the folded element |
0 |
2538 |
Ops.erase(Ops.begin()+1); // Erase the folded element |
0 |
| 2539 |
LHSC = cast(Ops[0]); |
0 |
2539 |
LHSC = cast(Ops[0]); |
0 |
| 2540 |
} |
0 |
2540 |
} |
0 |
| 2541 |
|
--- |
2541 |
|
--- |
| 2542 |
// If we are left with a constant zero being added, strip it off. |
--- |
2542 |
// If we are left with a constant zero being added, strip it off. |
--- |
| 2543 |
if (LHSC->getValue()->isZero()) { |
0 |
2543 |
if (LHSC->getValue()->isZero()) { |
0 |
| 2544 |
Ops.erase(Ops.begin()); |
0 |
2544 |
Ops.erase(Ops.begin()); |
0 |
| 2545 |
--Idx; |
0 |
2545 |
--Idx; |
0 |
| 2546 |
} |
--- |
2546 |
} |
--- |
| 2547 |
|
--- |
2547 |
|
--- |
| 2548 |
if (Ops.size() == 1) return Ops[0]; |
0 |
2548 |
if (Ops.size() == 1) return Ops[0]; |
0 |
| 2549 |
} |
--- |
2549 |
} |
--- |
| 2550 |
|
--- |
2550 |
|
--- |
| 2551 |
// Delay expensive flag strengthening until necessary. |
--- |
2551 |
// Delay expensive flag strengthening until necessary. |
--- |
| 2552 |
auto ComputeFlags = [this, OrigFlags](const ArrayRef Ops) { |
0 |
2552 |
auto ComputeFlags = [this, OrigFlags](const ArrayRef Ops) { |
0 |
| 2553 |
return StrengthenNoWrapFlags(this, scAddExpr, Ops, OrigFlags); |
0 |
2553 |
return StrengthenNoWrapFlags(this, scAddExpr, Ops, OrigFlags); |
0 |
| 2554 |
}; |
0 |
2554 |
}; |
0 |
| 2555 |
|
--- |
2555 |
|
--- |
| 2556 |
// Limit recursion calls depth. |
--- |
2556 |
// Limit recursion calls depth. |
--- |
| 2557 |
if (Depth > MaxArithDepth || hasHugeExpression(Ops)) |
0 |
2557 |
if (Depth > MaxArithDepth || hasHugeExpression(Ops)) |
0 |
| 2558 |
return getOrCreateAddExpr(Ops, ComputeFlags(Ops)); |
0 |
2558 |
return getOrCreateAddExpr(Ops, ComputeFlags(Ops)); |
0 |
| 2559 |
|
--- |
2559 |
|
--- |
| 2560 |
if (SCEV *S = findExistingSCEVInCache(scAddExpr, Ops)) { |
0 |
2560 |
if (SCEV *S = findExistingSCEVInCache(scAddExpr, Ops)) { |
0 |
| 2561 |
// Don't strengthen flags if we have no new information. |
--- |
2561 |
// Don't strengthen flags if we have no new information. |
--- |
| 2562 |
SCEVAddExpr *Add = static_cast(S); |
0 |
2562 |
SCEVAddExpr *Add = static_cast(S); |
0 |
| 2563 |
if (Add->getNoWrapFlags(OrigFlags) != OrigFlags) |
0 |
2563 |
if (Add->getNoWrapFlags(OrigFlags) != OrigFlags) |
0 |
| 2564 |
Add->setNoWrapFlags(ComputeFlags(Ops)); |
0 |
2564 |
Add->setNoWrapFlags(ComputeFlags(Ops)); |
0 |
| 2565 |
return S; |
0 |
2565 |
return S; |
0 |
| 2566 |
} |
--- |
2566 |
} |
--- |
| 2567 |
|
--- |
2567 |
|
--- |
| 2568 |
// Okay, check to see if the same value occurs in the operand list more than |
--- |
2568 |
// Okay, check to see if the same value occurs in the operand list more than |
--- |
| 2569 |
// once. If so, merge them together into an multiply expression. Since we |
--- |
2569 |
// once. If so, merge them together into an multiply expression. Since we |
--- |
| 2570 |
// sorted the list, these values are required to be adjacent. |
--- |
2570 |
// sorted the list, these values are required to be adjacent. |
--- |
| 2571 |
Type *Ty = Ops[0]->getType(); |
0 |
2571 |
Type *Ty = Ops[0]->getType(); |
0 |
| 2572 |
bool FoundMatch = false; |
0 |
2572 |
bool FoundMatch = false; |
0 |
| 2573 |
for (unsigned i = 0, e = Ops.size(); i != e-1; ++i) |
0 |
2573 |
for (unsigned i = 0, e = Ops.size(); i != e-1; ++i) |
0 |
| 2574 |
if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 |
0 |
2574 |
if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 |
0 |
| 2575 |
// Scan ahead to count how many equal operands there are. |
--- |
2575 |
// Scan ahead to count how many equal operands there are. |
--- |
| 2576 |
unsigned Count = 2; |
0 |
2576 |
unsigned Count = 2; |
0 |
| 2577 |
while (i+Count != e && Ops[i+Count] == Ops[i]) |
0 |
2577 |
while (i+Count != e && Ops[i+Count] == Ops[i]) |
0 |
| 2578 |
++Count; |
0 |
2578 |
++Count; |
0 |
| 2579 |
// Merge the values into a multiply. |
--- |
2579 |
// Merge the values into a multiply. |
--- |
| 2580 |
const SCEV *Scale = getConstant(Ty, Count); |
0 |
2580 |
const SCEV *Scale = getConstant(Ty, Count); |
0 |
| 2581 |
const SCEV *Mul = getMulExpr(Scale, Ops[i], SCEV::FlagAnyWrap, Depth + 1); |
0 |
2581 |
const SCEV *Mul = getMulExpr(Scale, Ops[i], SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 2582 |
if (Ops.size() == Count) |
0 |
2582 |
if (Ops.size() == Count) |
0 |
| 2583 |
return Mul; |
0 |
2583 |
return Mul; |
0 |
| 2584 |
Ops[i] = Mul; |
0 |
2584 |
Ops[i] = Mul; |
0 |
| 2585 |
Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count); |
0 |
2585 |
Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count); |
0 |
| 2586 |
--i; e -= Count - 1; |
0 |
2586 |
--i; e -= Count - 1; |
0 |
| 2587 |
FoundMatch = true; |
0 |
2587 |
FoundMatch = true; |
0 |
| 2588 |
} |
--- |
2588 |
} |
--- |
| 2589 |
if (FoundMatch) |
0 |
2589 |
if (FoundMatch) |
0 |
| 2590 |
return getAddExpr(Ops, OrigFlags, Depth + 1); |
0 |
2590 |
return getAddExpr(Ops, OrigFlags, Depth + 1); |
0 |
| 2591 |
|
--- |
2591 |
|
--- |
| 2592 |
// Check for truncates. If all the operands are truncated from the same |
--- |
2592 |
// Check for truncates. If all the operands are truncated from the same |
--- |
| 2593 |
// type, see if factoring out the truncate would permit the result to be |
--- |
2593 |
// type, see if factoring out the truncate would permit the result to be |
--- |
| 2594 |
// folded. eg., n*trunc(x) + m*trunc(y) --> trunc(trunc(m)*x + trunc(n)*y) |
--- |
2594 |
// folded. eg., n*trunc(x) + m*trunc(y) --> trunc(trunc(m)*x + trunc(n)*y) |
--- |
| 2595 |
// if the contents of the resulting outer trunc fold to something simple. |
--- |
2595 |
// if the contents of the resulting outer trunc fold to something simple. |
--- |
| 2596 |
auto FindTruncSrcType = [&]() -> Type * { |
0 |
2596 |
auto FindTruncSrcType = [&]() -> Type * { |
0 |
| 2597 |
// We're ultimately looking to fold an addrec of truncs and muls of only |
--- |
2597 |
// We're ultimately looking to fold an addrec of truncs and muls of only |
--- |
| 2598 |
// constants and truncs, so if we find any other types of SCEV |
--- |
2598 |
// constants and truncs, so if we find any other types of SCEV |
--- |
| 2599 |
// as operands of the addrec then we bail and return nullptr here. |
--- |
2599 |
// as operands of the addrec then we bail and return nullptr here. |
--- |
| 2600 |
// Otherwise, we return the type of the operand of a trunc that we find. |
--- |
2600 |
// Otherwise, we return the type of the operand of a trunc that we find. |
--- |
| 2601 |
if (auto *T = dyn_cast(Ops[Idx])) |
0 |
2601 |
if (auto *T = dyn_cast(Ops[Idx])) |
0 |
| 2602 |
return T->getOperand()->getType(); |
0 |
2602 |
return T->getOperand()->getType(); |
0 |
| 2603 |
if (const auto *Mul = dyn_cast(Ops[Idx])) { |
0 |
2603 |
if (const auto *Mul = dyn_cast(Ops[Idx])) { |
0 |
| 2604 |
const auto *LastOp = Mul->getOperand(Mul->getNumOperands() - 1); |
0 |
2604 |
const auto *LastOp = Mul->getOperand(Mul->getNumOperands() - 1); |
0 |
| 2605 |
if (const auto *T = dyn_cast(LastOp)) |
0 |
2605 |
if (const auto *T = dyn_cast(LastOp)) |
0 |
| 2606 |
return T->getOperand()->getType(); |
0 |
2606 |
return T->getOperand()->getType(); |
0 |
| 2607 |
} |
--- |
2607 |
} |
--- |
| 2608 |
return nullptr; |
0 |
2608 |
return nullptr; |
0 |
| 2609 |
}; |
0 |
2609 |
}; |
0 |
| 2610 |
if (auto *SrcType = FindTruncSrcType()) { |
0 |
2610 |
if (auto *SrcType = FindTruncSrcType()) { |
0 |
| 2611 |
SmallVector LargeOps; |
0 |
2611 |
SmallVector LargeOps; |
0 |
| 2612 |
bool Ok = true; |
0 |
2612 |
bool Ok = true; |
0 |
| 2613 |
// Check all the operands to see if they can be represented in the |
--- |
2613 |
// Check all the operands to see if they can be represented in the |
--- |
| 2614 |
// source type of the truncate. |
--- |
2614 |
// source type of the truncate. |
--- |
| 2615 |
for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
0 |
2615 |
for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
0 |
| 2616 |
if (const SCEVTruncateExpr *T = dyn_cast(Ops[i])) { |
0 |
2616 |
if (const SCEVTruncateExpr *T = dyn_cast(Ops[i])) { |
0 |
| 2617 |
if (T->getOperand()->getType() != SrcType) { |
0 |
2617 |
if (T->getOperand()->getType() != SrcType) { |
0 |
| 2618 |
Ok = false; |
0 |
2618 |
Ok = false; |
0 |
| 2619 |
break; |
0 |
2619 |
break; |
0 |
| 2620 |
} |
--- |
2620 |
} |
--- |
| 2621 |
LargeOps.push_back(T->getOperand()); |
0 |
2621 |
LargeOps.push_back(T->getOperand()); |
0 |
| 2622 |
} else if (const SCEVConstant *C = dyn_cast(Ops[i])) { |
0 |
2622 |
} else if (const SCEVConstant *C = dyn_cast(Ops[i])) { |
0 |
| 2623 |
LargeOps.push_back(getAnyExtendExpr(C, SrcType)); |
0 |
2623 |
LargeOps.push_back(getAnyExtendExpr(C, SrcType)); |
0 |
| 2624 |
} else if (const SCEVMulExpr *M = dyn_cast(Ops[i])) { |
0 |
2624 |
} else if (const SCEVMulExpr *M = dyn_cast(Ops[i])) { |
0 |
| 2625 |
SmallVector LargeMulOps; |
0 |
2625 |
SmallVector LargeMulOps; |
0 |
| 2626 |
for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { |
0 |
2626 |
for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { |
0 |
| 2627 |
if (const SCEVTruncateExpr *T = |
0 |
2627 |
if (const SCEVTruncateExpr *T = |
0 |
| 2628 |
dyn_cast(M->getOperand(j))) { |
0 |
2628 |
dyn_cast(M->getOperand(j))) { |
0 |
| 2629 |
if (T->getOperand()->getType() != SrcType) { |
0 |
2629 |
if (T->getOperand()->getType() != SrcType) { |
0 |
| 2630 |
Ok = false; |
0 |
2630 |
Ok = false; |
0 |
| 2631 |
break; |
0 |
2631 |
break; |
0 |
| 2632 |
} |
--- |
2632 |
} |
--- |
| 2633 |
LargeMulOps.push_back(T->getOperand()); |
0 |
2633 |
LargeMulOps.push_back(T->getOperand()); |
0 |
| 2634 |
} else if (const auto *C = dyn_cast(M->getOperand(j))) { |
0 |
2634 |
} else if (const auto *C = dyn_cast(M->getOperand(j))) { |
0 |
| 2635 |
LargeMulOps.push_back(getAnyExtendExpr(C, SrcType)); |
0 |
2635 |
LargeMulOps.push_back(getAnyExtendExpr(C, SrcType)); |
0 |
| 2636 |
} else { |
--- |
2636 |
} else { |
--- |
| 2637 |
Ok = false; |
0 |
2637 |
Ok = false; |
0 |
| 2638 |
break; |
0 |
2638 |
break; |
0 |
| 2639 |
} |
--- |
2639 |
} |
--- |
| 2640 |
} |
--- |
2640 |
} |
--- |
| 2641 |
if (Ok) |
0 |
2641 |
if (Ok) |
0 |
| 2642 |
LargeOps.push_back(getMulExpr(LargeMulOps, SCEV::FlagAnyWrap, Depth + 1)); |
0 |
2642 |
LargeOps.push_back(getMulExpr(LargeMulOps, SCEV::FlagAnyWrap, Depth + 1)); |
0 |
| 2643 |
} else { |
0 |
2643 |
} else { |
0 |
| 2644 |
Ok = false; |
0 |
2644 |
Ok = false; |
0 |
| 2645 |
break; |
0 |
2645 |
break; |
0 |
| 2646 |
} |
--- |
2646 |
} |
--- |
| 2647 |
} |
--- |
2647 |
} |
--- |
| 2648 |
if (Ok) { |
0 |
2648 |
if (Ok) { |
0 |
| 2649 |
// Evaluate the expression in the larger type. |
--- |
2649 |
// Evaluate the expression in the larger type. |
--- |
| 2650 |
const SCEV *Fold = getAddExpr(LargeOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
2650 |
const SCEV *Fold = getAddExpr(LargeOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 2651 |
// If it folds to something simple, use it. Otherwise, don't. |
--- |
2651 |
// If it folds to something simple, use it. Otherwise, don't. |
--- |
| 2652 |
if (isa(Fold) || isa(Fold)) |
0 |
2652 |
if (isa(Fold) || isa(Fold)) |
0 |
| 2653 |
return getTruncateExpr(Fold, Ty); |
0 |
2653 |
return getTruncateExpr(Fold, Ty); |
0 |
| 2654 |
} |
--- |
2654 |
} |
--- |
| 2655 |
} |
0 |
2655 |
} |
0 |
| 2656 |
|
--- |
2656 |
|
--- |
| 2657 |
if (Ops.size() == 2) { |
0 |
2657 |
if (Ops.size() == 2) { |
0 |
| 2658 |
// Check if we have an expression of the form ((X + C1) - C2), where C1 and |
--- |
2658 |
// Check if we have an expression of the form ((X + C1) - C2), where C1 and |
--- |
| 2659 |
// C2 can be folded in a way that allows retaining wrapping flags of (X + |
--- |
2659 |
// C2 can be folded in a way that allows retaining wrapping flags of (X + |
--- |
| 2660 |
// C1). |
--- |
2660 |
// C1). |
--- |
| 2661 |
const SCEV *A = Ops[0]; |
0 |
2661 |
const SCEV *A = Ops[0]; |
0 |
| 2662 |
const SCEV *B = Ops[1]; |
0 |
2662 |
const SCEV *B = Ops[1]; |
0 |
| 2663 |
auto *AddExpr = dyn_cast(B); |
0 |
2663 |
auto *AddExpr = dyn_cast(B); |
0 |
| 2664 |
auto *C = dyn_cast(A); |
0 |
2664 |
auto *C = dyn_cast(A); |
0 |
| 2665 |
if (AddExpr && C && isa(AddExpr->getOperand(0))) { |
0 |
2665 |
if (AddExpr && C && isa(AddExpr->getOperand(0))) { |
0 |
| 2666 |
auto C1 = cast(AddExpr->getOperand(0))->getAPInt(); |
0 |
2666 |
auto C1 = cast(AddExpr->getOperand(0))->getAPInt(); |
0 |
| 2667 |
auto C2 = C->getAPInt(); |
0 |
2667 |
auto C2 = C->getAPInt(); |
0 |
| 2668 |
SCEV::NoWrapFlags PreservedFlags = SCEV::FlagAnyWrap; |
0 |
2668 |
SCEV::NoWrapFlags PreservedFlags = SCEV::FlagAnyWrap; |
0 |
| 2669 |
|
--- |
2669 |
|
--- |
| 2670 |
APInt ConstAdd = C1 + C2; |
0 |
2670 |
APInt ConstAdd = C1 + C2; |
0 |
| 2671 |
auto AddFlags = AddExpr->getNoWrapFlags(); |
0 |
2671 |
auto AddFlags = AddExpr->getNoWrapFlags(); |
0 |
| 2672 |
// Adding a smaller constant is NUW if the original AddExpr was NUW. |
--- |
2672 |
// Adding a smaller constant is NUW if the original AddExpr was NUW. |
--- |
| 2673 |
if (ScalarEvolution::hasFlags(AddFlags, SCEV::FlagNUW) && |
0 |
2673 |
if (ScalarEvolution::hasFlags(AddFlags, SCEV::FlagNUW) && |
0 |
| 2674 |
ConstAdd.ule(C1)) { |
0 |
2674 |
ConstAdd.ule(C1)) { |
0 |
| 2675 |
PreservedFlags = |
--- |
2675 |
PreservedFlags = |
--- |
| 2676 |
ScalarEvolution::setFlags(PreservedFlags, SCEV::FlagNUW); |
0 |
2676 |
ScalarEvolution::setFlags(PreservedFlags, SCEV::FlagNUW); |
0 |
| 2677 |
} |
--- |
2677 |
} |
--- |
| 2678 |
|
--- |
2678 |
|
--- |
| 2679 |
// Adding a constant with the same sign and small magnitude is NSW, if the |
--- |
2679 |
// Adding a constant with the same sign and small magnitude is NSW, if the |
--- |
| 2680 |
// original AddExpr was NSW. |
--- |
2680 |
// original AddExpr was NSW. |
--- |
| 2681 |
if (ScalarEvolution::hasFlags(AddFlags, SCEV::FlagNSW) && |
0 |
2681 |
if (ScalarEvolution::hasFlags(AddFlags, SCEV::FlagNSW) && |
0 |
| 2682 |
C1.isSignBitSet() == ConstAdd.isSignBitSet() && |
0 |
2682 |
C1.isSignBitSet() == ConstAdd.isSignBitSet() && |
0 |
| 2683 |
ConstAdd.abs().ule(C1.abs())) { |
0 |
2683 |
ConstAdd.abs().ule(C1.abs())) { |
0 |
| 2684 |
PreservedFlags = |
--- |
2684 |
PreservedFlags = |
--- |
| 2685 |
ScalarEvolution::setFlags(PreservedFlags, SCEV::FlagNSW); |
0 |
2685 |
ScalarEvolution::setFlags(PreservedFlags, SCEV::FlagNSW); |
0 |
| 2686 |
} |
--- |
2686 |
} |
--- |
| 2687 |
|
--- |
2687 |
|
--- |
| 2688 |
if (PreservedFlags != SCEV::FlagAnyWrap) { |
0 |
2688 |
if (PreservedFlags != SCEV::FlagAnyWrap) { |
0 |
| 2689 |
SmallVector NewOps(AddExpr->operands()); |
0 |
2689 |
SmallVector NewOps(AddExpr->operands()); |
0 |
| 2690 |
NewOps[0] = getConstant(ConstAdd); |
0 |
2690 |
NewOps[0] = getConstant(ConstAdd); |
0 |
| 2691 |
return getAddExpr(NewOps, PreservedFlags); |
0 |
2691 |
return getAddExpr(NewOps, PreservedFlags); |
0 |
| 2692 |
} |
0 |
2692 |
} |
0 |
| 2693 |
} |
0 |
2693 |
} |
0 |
| 2694 |
} |
--- |
2694 |
} |
--- |
| 2695 |
|
--- |
2695 |
|
--- |
| 2696 |
// Canonicalize (-1 * urem X, Y) + X --> (Y * X/Y) |
--- |
2696 |
// Canonicalize (-1 * urem X, Y) + X --> (Y * X/Y) |
--- |
| 2697 |
if (Ops.size() == 2) { |
0 |
2697 |
if (Ops.size() == 2) { |
0 |
| 2698 |
const SCEVMulExpr *Mul = dyn_cast(Ops[0]); |
0 |
2698 |
const SCEVMulExpr *Mul = dyn_cast(Ops[0]); |
0 |
| 2699 |
if (Mul && Mul->getNumOperands() == 2 && |
0 |
2699 |
if (Mul && Mul->getNumOperands() == 2 && |
0 |
| 2700 |
Mul->getOperand(0)->isAllOnesValue()) { |
0 |
2700 |
Mul->getOperand(0)->isAllOnesValue()) { |
0 |
| 2701 |
const SCEV *X; |
--- |
2701 |
const SCEV *X; |
--- |
| 2702 |
const SCEV *Y; |
--- |
2702 |
const SCEV *Y; |
--- |
| 2703 |
if (matchURem(Mul->getOperand(1), X, Y) && X == Ops[1]) { |
0 |
2703 |
if (matchURem(Mul->getOperand(1), X, Y) && X == Ops[1]) { |
0 |
| 2704 |
return getMulExpr(Y, getUDivExpr(X, Y)); |
0 |
2704 |
return getMulExpr(Y, getUDivExpr(X, Y)); |
0 |
| 2705 |
} |
--- |
2705 |
} |
--- |
| 2706 |
} |
--- |
2706 |
} |
--- |
| 2707 |
} |
--- |
2707 |
} |
--- |
| 2708 |
|
--- |
2708 |
|
--- |
| 2709 |
// Skip past any other cast SCEVs. |
--- |
2709 |
// Skip past any other cast SCEVs. |
--- |
| 2710 |
while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) |
0 |
2710 |
while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) |
0 |
| 2711 |
++Idx; |
0 |
2711 |
++Idx; |
0 |
| 2712 |
|
--- |
2712 |
|
--- |
| 2713 |
// If there are add operands they would be next. |
--- |
2713 |
// If there are add operands they would be next. |
--- |
| 2714 |
if (Idx < Ops.size()) { |
0 |
2714 |
if (Idx < Ops.size()) { |
0 |
| 2715 |
bool DeletedAdd = false; |
0 |
2715 |
bool DeletedAdd = false; |
0 |
| 2716 |
// If the original flags and all inlined SCEVAddExprs are NUW, use the |
--- |
2716 |
// If the original flags and all inlined SCEVAddExprs are NUW, use the |
--- |
| 2717 |
// common NUW flag for expression after inlining. Other flags cannot be |
--- |
2717 |
// common NUW flag for expression after inlining. Other flags cannot be |
--- |
| 2718 |
// preserved, because they may depend on the original order of operations. |
--- |
2718 |
// preserved, because they may depend on the original order of operations. |
--- |
| 2719 |
SCEV::NoWrapFlags CommonFlags = maskFlags(OrigFlags, SCEV::FlagNUW); |
0 |
2719 |
SCEV::NoWrapFlags CommonFlags = maskFlags(OrigFlags, SCEV::FlagNUW); |
0 |
| 2720 |
while (const SCEVAddExpr *Add = dyn_cast(Ops[Idx])) { |
0 |
2720 |
while (const SCEVAddExpr *Add = dyn_cast(Ops[Idx])) { |
0 |
| 2721 |
if (Ops.size() > AddOpsInlineThreshold || |
0 |
2721 |
if (Ops.size() > AddOpsInlineThreshold || |
0 |
| 2722 |
Add->getNumOperands() > AddOpsInlineThreshold) |
0 |
2722 |
Add->getNumOperands() > AddOpsInlineThreshold) |
0 |
| 2723 |
break; |
0 |
2723 |
break; |
0 |
| 2724 |
// If we have an add, expand the add operands onto the end of the operands |
--- |
2724 |
// If we have an add, expand the add operands onto the end of the operands |
--- |
| 2725 |
// list. |
--- |
2725 |
// list. |
--- |
| 2726 |
Ops.erase(Ops.begin()+Idx); |
0 |
2726 |
Ops.erase(Ops.begin()+Idx); |
0 |
| 2727 |
append_range(Ops, Add->operands()); |
0 |
2727 |
append_range(Ops, Add->operands()); |
0 |
| 2728 |
DeletedAdd = true; |
0 |
2728 |
DeletedAdd = true; |
0 |
| 2729 |
CommonFlags = maskFlags(CommonFlags, Add->getNoWrapFlags()); |
0 |
2729 |
CommonFlags = maskFlags(CommonFlags, Add->getNoWrapFlags()); |
0 |
| 2730 |
} |
0 |
2730 |
} |
0 |
| 2731 |
|
--- |
2731 |
|
--- |
| 2732 |
// If we deleted at least one add, we added operands to the end of the list, |
--- |
2732 |
// If we deleted at least one add, we added operands to the end of the list, |
--- |
| 2733 |
// and they are not necessarily sorted. Recurse to resort and resimplify |
--- |
2733 |
// and they are not necessarily sorted. Recurse to resort and resimplify |
--- |
| 2734 |
// any operands we just acquired. |
--- |
2734 |
// any operands we just acquired. |
--- |
| 2735 |
if (DeletedAdd) |
0 |
2735 |
if (DeletedAdd) |
0 |
| 2736 |
return getAddExpr(Ops, CommonFlags, Depth + 1); |
0 |
2736 |
return getAddExpr(Ops, CommonFlags, Depth + 1); |
0 |
| 2737 |
} |
--- |
2737 |
} |
--- |
| 2738 |
|
--- |
2738 |
|
--- |
| 2739 |
// Skip over the add expression until we get to a multiply. |
--- |
2739 |
// Skip over the add expression until we get to a multiply. |
--- |
| 2740 |
while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
0 |
2740 |
while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
0 |
| 2741 |
++Idx; |
0 |
2741 |
++Idx; |
0 |
| 2742 |
|
--- |
2742 |
|
--- |
| 2743 |
// Check to see if there are any folding opportunities present with |
--- |
2743 |
// Check to see if there are any folding opportunities present with |
--- |
| 2744 |
// operands multiplied by constant values. |
--- |
2744 |
// operands multiplied by constant values. |
--- |
| 2745 |
if (Idx < Ops.size() && isa(Ops[Idx])) { |
0 |
2745 |
if (Idx < Ops.size() && isa(Ops[Idx])) { |
0 |
| 2746 |
uint64_t BitWidth = getTypeSizeInBits(Ty); |
0 |
2746 |
uint64_t BitWidth = getTypeSizeInBits(Ty); |
0 |
| 2747 |
DenseMap M; |
0 |
2747 |
DenseMap M; |
0 |
| 2748 |
SmallVector NewOps; |
0 |
2748 |
SmallVector NewOps; |
0 |
| 2749 |
APInt AccumulatedConstant(BitWidth, 0); |
0 |
2749 |
APInt AccumulatedConstant(BitWidth, 0); |
0 |
| 2750 |
if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
0 |
2750 |
if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
0 |
| 2751 |
Ops, APInt(BitWidth, 1), *this)) { |
0 |
2751 |
Ops, APInt(BitWidth, 1), *this)) { |
0 |
| 2752 |
struct APIntCompare { |
--- |
2752 |
struct APIntCompare { |
--- |
| 2753 |
bool operator()(const APInt &LHS, const APInt &RHS) const { |
0 |
2753 |
bool operator()(const APInt &LHS, const APInt &RHS) const { |
0 |
| 2754 |
return LHS.ult(RHS); |
0 |
2754 |
return LHS.ult(RHS); |
0 |
| 2755 |
} |
--- |
2755 |
} |
--- |
| 2756 |
}; |
--- |
2756 |
}; |
--- |
| 2757 |
|
--- |
2757 |
|
--- |
| 2758 |
// Some interesting folding opportunity is present, so its worthwhile to |
--- |
2758 |
// Some interesting folding opportunity is present, so its worthwhile to |
--- |
| 2759 |
// re-generate the operands list. Group the operands by constant scale, |
--- |
2759 |
// re-generate the operands list. Group the operands by constant scale, |
--- |
| 2760 |
// to avoid multiplying by the same constant scale multiple times. |
--- |
2760 |
// to avoid multiplying by the same constant scale multiple times. |
--- |
| 2761 |
std::map, APIntCompare> MulOpLists; |
0 |
2761 |
std::map, APIntCompare> MulOpLists; |
0 |
| 2762 |
for (const SCEV *NewOp : NewOps) |
0 |
2762 |
for (const SCEV *NewOp : NewOps) |
0 |
| 2763 |
MulOpLists[M.find(NewOp)->second].push_back(NewOp); |
0 |
2763 |
MulOpLists[M.find(NewOp)->second].push_back(NewOp); |
0 |
| 2764 |
// Re-generate the operands list. |
--- |
2764 |
// Re-generate the operands list. |
--- |
| 2765 |
Ops.clear(); |
0 |
2765 |
Ops.clear(); |
0 |
| 2766 |
if (AccumulatedConstant != 0) |
0 |
2766 |
if (AccumulatedConstant != 0) |
0 |
| 2767 |
Ops.push_back(getConstant(AccumulatedConstant)); |
0 |
2767 |
Ops.push_back(getConstant(AccumulatedConstant)); |
0 |
| 2768 |
for (auto &MulOp : MulOpLists) { |
0 |
2768 |
for (auto &MulOp : MulOpLists) { |
0 |
| 2769 |
if (MulOp.first == 1) { |
0 |
2769 |
if (MulOp.first == 1) { |
0 |
| 2770 |
Ops.push_back(getAddExpr(MulOp.second, SCEV::FlagAnyWrap, Depth + 1)); |
0 |
2770 |
Ops.push_back(getAddExpr(MulOp.second, SCEV::FlagAnyWrap, Depth + 1)); |
0 |
| 2771 |
} else if (MulOp.first != 0) { |
0 |
2771 |
} else if (MulOp.first != 0) { |
0 |
| 2772 |
Ops.push_back(getMulExpr( |
0 |
2772 |
Ops.push_back(getMulExpr( |
0 |
| 2773 |
getConstant(MulOp.first), |
0 |
2773 |
getConstant(MulOp.first), |
0 |
| 2774 |
getAddExpr(MulOp.second, SCEV::FlagAnyWrap, Depth + 1), |
--- |
2774 |
getAddExpr(MulOp.second, SCEV::FlagAnyWrap, Depth + 1), |
--- |
| 2775 |
SCEV::FlagAnyWrap, Depth + 1)); |
--- |
2775 |
SCEV::FlagAnyWrap, Depth + 1)); |
--- |
| 2776 |
} |
--- |
2776 |
} |
--- |
| 2777 |
} |
--- |
2777 |
} |
--- |
| 2778 |
if (Ops.empty()) |
0 |
2778 |
if (Ops.empty()) |
0 |
| 2779 |
return getZero(Ty); |
0 |
2779 |
return getZero(Ty); |
0 |
| 2780 |
if (Ops.size() == 1) |
0 |
2780 |
if (Ops.size() == 1) |
0 |
| 2781 |
return Ops[0]; |
0 |
2781 |
return Ops[0]; |
0 |
| 2782 |
return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
2782 |
return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 2783 |
} |
0 |
2783 |
} |
0 |
| 2784 |
} |
0 |
2784 |
} |
0 |
| 2785 |
|
--- |
2785 |
|
--- |
| 2786 |
// If we are adding something to a multiply expression, make sure the |
--- |
2786 |
// If we are adding something to a multiply expression, make sure the |
--- |
| 2787 |
// something is not already an operand of the multiply. If so, merge it into |
--- |
2787 |
// something is not already an operand of the multiply. If so, merge it into |
--- |
| 2788 |
// the multiply. |
--- |
2788 |
// the multiply. |
--- |
| 2789 |
for (; Idx < Ops.size() && isa(Ops[Idx]); ++Idx) { |
0 |
2789 |
for (; Idx < Ops.size() && isa(Ops[Idx]); ++Idx) { |
0 |
| 2790 |
const SCEVMulExpr *Mul = cast(Ops[Idx]); |
0 |
2790 |
const SCEVMulExpr *Mul = cast(Ops[Idx]); |
0 |
| 2791 |
for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { |
0 |
2791 |
for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { |
0 |
| 2792 |
const SCEV *MulOpSCEV = Mul->getOperand(MulOp); |
0 |
2792 |
const SCEV *MulOpSCEV = Mul->getOperand(MulOp); |
0 |
| 2793 |
if (isa(MulOpSCEV)) |
0 |
2793 |
if (isa(MulOpSCEV)) |
0 |
| 2794 |
continue; |
0 |
2794 |
continue; |
0 |
| 2795 |
for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) |
0 |
2795 |
for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) |
0 |
| 2796 |
if (MulOpSCEV == Ops[AddOp]) { |
0 |
2796 |
if (MulOpSCEV == Ops[AddOp]) { |
0 |
| 2797 |
// Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) |
--- |
2797 |
// Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) |
--- |
| 2798 |
const SCEV *InnerMul = Mul->getOperand(MulOp == 0); |
0 |
2798 |
const SCEV *InnerMul = Mul->getOperand(MulOp == 0); |
0 |
| 2799 |
if (Mul->getNumOperands() != 2) { |
0 |
2799 |
if (Mul->getNumOperands() != 2) { |
0 |
| 2800 |
// If the multiply has more than two operands, we must get the |
--- |
2800 |
// If the multiply has more than two operands, we must get the |
--- |
| 2801 |
// Y*Z term. |
--- |
2801 |
// Y*Z term. |
--- |
| 2802 |
SmallVector MulOps( |
--- |
2802 |
SmallVector MulOps( |
--- |
| 2803 |
Mul->operands().take_front(MulOp)); |
0 |
2803 |
Mul->operands().take_front(MulOp)); |
0 |
| 2804 |
append_range(MulOps, Mul->operands().drop_front(MulOp + 1)); |
0 |
2804 |
append_range(MulOps, Mul->operands().drop_front(MulOp + 1)); |
0 |
| 2805 |
InnerMul = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
2805 |
InnerMul = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 2806 |
} |
0 |
2806 |
} |
0 |
| 2807 |
SmallVector TwoOps = {getOne(Ty), InnerMul}; |
0 |
2807 |
SmallVector TwoOps = {getOne(Ty), InnerMul}; |
0 |
| 2808 |
const SCEV *AddOne = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
2808 |
const SCEV *AddOne = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 2809 |
const SCEV *OuterMul = getMulExpr(AddOne, MulOpSCEV, |
0 |
2809 |
const SCEV *OuterMul = getMulExpr(AddOne, MulOpSCEV, |
0 |
| 2810 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
2810 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
| 2811 |
if (Ops.size() == 2) return OuterMul; |
0 |
2811 |
if (Ops.size() == 2) return OuterMul; |
0 |
| 2812 |
if (AddOp < Idx) { |
0 |
2812 |
if (AddOp < Idx) { |
0 |
| 2813 |
Ops.erase(Ops.begin()+AddOp); |
0 |
2813 |
Ops.erase(Ops.begin()+AddOp); |
0 |
| 2814 |
Ops.erase(Ops.begin()+Idx-1); |
0 |
2814 |
Ops.erase(Ops.begin()+Idx-1); |
0 |
| 2815 |
} else { |
--- |
2815 |
} else { |
--- |
| 2816 |
Ops.erase(Ops.begin()+Idx); |
0 |
2816 |
Ops.erase(Ops.begin()+Idx); |
0 |
| 2817 |
Ops.erase(Ops.begin()+AddOp-1); |
0 |
2817 |
Ops.erase(Ops.begin()+AddOp-1); |
0 |
| 2818 |
} |
--- |
2818 |
} |
--- |
| 2819 |
Ops.push_back(OuterMul); |
0 |
2819 |
Ops.push_back(OuterMul); |
0 |
| 2820 |
return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
2820 |
return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 2821 |
} |
0 |
2821 |
} |
0 |
| 2822 |
|
--- |
2822 |
|
--- |
| 2823 |
// Check this multiply against other multiplies being added together. |
--- |
2823 |
// Check this multiply against other multiplies being added together. |
--- |
| 2824 |
for (unsigned OtherMulIdx = Idx+1; |
0 |
2824 |
for (unsigned OtherMulIdx = Idx+1; |
0 |
| 2825 |
OtherMulIdx < Ops.size() && isa(Ops[OtherMulIdx]); |
0 |
2825 |
OtherMulIdx < Ops.size() && isa(Ops[OtherMulIdx]); |
0 |
| 2826 |
++OtherMulIdx) { |
--- |
2826 |
++OtherMulIdx) { |
--- |
| 2827 |
const SCEVMulExpr *OtherMul = cast(Ops[OtherMulIdx]); |
0 |
2827 |
const SCEVMulExpr *OtherMul = cast(Ops[OtherMulIdx]); |
0 |
| 2828 |
// If MulOp occurs in OtherMul, we can fold the two multiplies |
--- |
2828 |
// If MulOp occurs in OtherMul, we can fold the two multiplies |
--- |
| 2829 |
// together. |
--- |
2829 |
// together. |
--- |
| 2830 |
for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); |
0 |
2830 |
for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); |
0 |
| 2831 |
OMulOp != e; ++OMulOp) |
0 |
2831 |
OMulOp != e; ++OMulOp) |
0 |
| 2832 |
if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { |
0 |
2832 |
if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { |
0 |
| 2833 |
// Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E)) |
--- |
2833 |
// Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E)) |
--- |
| 2834 |
const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0); |
0 |
2834 |
const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0); |
0 |
| 2835 |
if (Mul->getNumOperands() != 2) { |
0 |
2835 |
if (Mul->getNumOperands() != 2) { |
0 |
| 2836 |
SmallVector MulOps( |
--- |
2836 |
SmallVector MulOps( |
--- |
| 2837 |
Mul->operands().take_front(MulOp)); |
0 |
2837 |
Mul->operands().take_front(MulOp)); |
0 |
| 2838 |
append_range(MulOps, Mul->operands().drop_front(MulOp+1)); |
0 |
2838 |
append_range(MulOps, Mul->operands().drop_front(MulOp+1)); |
0 |
| 2839 |
InnerMul1 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
2839 |
InnerMul1 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 2840 |
} |
0 |
2840 |
} |
0 |
| 2841 |
const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0); |
0 |
2841 |
const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0); |
0 |
| 2842 |
if (OtherMul->getNumOperands() != 2) { |
0 |
2842 |
if (OtherMul->getNumOperands() != 2) { |
0 |
| 2843 |
SmallVector MulOps( |
--- |
2843 |
SmallVector MulOps( |
--- |
| 2844 |
OtherMul->operands().take_front(OMulOp)); |
0 |
2844 |
OtherMul->operands().take_front(OMulOp)); |
0 |
| 2845 |
append_range(MulOps, OtherMul->operands().drop_front(OMulOp+1)); |
0 |
2845 |
append_range(MulOps, OtherMul->operands().drop_front(OMulOp+1)); |
0 |
| 2846 |
InnerMul2 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
2846 |
InnerMul2 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 2847 |
} |
0 |
2847 |
} |
0 |
| 2848 |
SmallVector TwoOps = {InnerMul1, InnerMul2}; |
0 |
2848 |
SmallVector TwoOps = {InnerMul1, InnerMul2}; |
0 |
| 2849 |
const SCEV *InnerMulSum = |
--- |
2849 |
const SCEV *InnerMulSum = |
--- |
| 2850 |
getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
2850 |
getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 2851 |
const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum, |
0 |
2851 |
const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum, |
0 |
| 2852 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
2852 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
| 2853 |
if (Ops.size() == 2) return OuterMul; |
0 |
2853 |
if (Ops.size() == 2) return OuterMul; |
0 |
| 2854 |
Ops.erase(Ops.begin()+Idx); |
0 |
2854 |
Ops.erase(Ops.begin()+Idx); |
0 |
| 2855 |
Ops.erase(Ops.begin()+OtherMulIdx-1); |
0 |
2855 |
Ops.erase(Ops.begin()+OtherMulIdx-1); |
0 |
| 2856 |
Ops.push_back(OuterMul); |
0 |
2856 |
Ops.push_back(OuterMul); |
0 |
| 2857 |
return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
2857 |
return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 2858 |
} |
0 |
2858 |
} |
0 |
| 2859 |
} |
--- |
2859 |
} |
--- |
| 2860 |
} |
--- |
2860 |
} |
--- |
| 2861 |
} |
--- |
2861 |
} |
--- |
| 2862 |
|
--- |
2862 |
|
--- |
| 2863 |
// If there are any add recurrences in the operands list, see if any other |
--- |
2863 |
// If there are any add recurrences in the operands list, see if any other |
--- |
| 2864 |
// added values are loop invariant. If so, we can fold them into the |
--- |
2864 |
// added values are loop invariant. If so, we can fold them into the |
--- |
| 2865 |
// recurrence. |
--- |
2865 |
// recurrence. |
--- |
| 2866 |
while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
0 |
2866 |
while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
0 |
| 2867 |
++Idx; |
0 |
2867 |
++Idx; |
0 |
| 2868 |
|
--- |
2868 |
|
--- |
| 2869 |
// Scan over all recurrences, trying to fold loop invariants into them. |
--- |
2869 |
// Scan over all recurrences, trying to fold loop invariants into them. |
--- |
| 2870 |
for (; Idx < Ops.size() && isa(Ops[Idx]); ++Idx) { |
0 |
2870 |
for (; Idx < Ops.size() && isa(Ops[Idx]); ++Idx) { |
0 |
| 2871 |
// Scan all of the other operands to this add and add them to the vector if |
--- |
2871 |
// Scan all of the other operands to this add and add them to the vector if |
--- |
| 2872 |
// they are loop invariant w.r.t. the recurrence. |
--- |
2872 |
// they are loop invariant w.r.t. the recurrence. |
--- |
| 2873 |
SmallVector LIOps; |
0 |
2873 |
SmallVector LIOps; |
0 |
| 2874 |
const SCEVAddRecExpr *AddRec = cast(Ops[Idx]); |
0 |
2874 |
const SCEVAddRecExpr *AddRec = cast(Ops[Idx]); |
0 |
| 2875 |
const Loop *AddRecLoop = AddRec->getLoop(); |
0 |
2875 |
const Loop *AddRecLoop = AddRec->getLoop(); |
0 |
| 2876 |
for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
0 |
2876 |
for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
0 |
| 2877 |
if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) { |
0 |
2877 |
if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) { |
0 |
| 2878 |
LIOps.push_back(Ops[i]); |
0 |
2878 |
LIOps.push_back(Ops[i]); |
0 |
| 2879 |
Ops.erase(Ops.begin()+i); |
0 |
2879 |
Ops.erase(Ops.begin()+i); |
0 |
| 2880 |
--i; --e; |
0 |
2880 |
--i; --e; |
0 |
| 2881 |
} |
--- |
2881 |
} |
--- |
| 2882 |
|
--- |
2882 |
|
--- |
| 2883 |
// If we found some loop invariants, fold them into the recurrence. |
--- |
2883 |
// If we found some loop invariants, fold them into the recurrence. |
--- |
| 2884 |
if (!LIOps.empty()) { |
0 |
2884 |
if (!LIOps.empty()) { |
0 |
| 2885 |
// Compute nowrap flags for the addition of the loop-invariant ops and |
--- |
2885 |
// Compute nowrap flags for the addition of the loop-invariant ops and |
--- |
| 2886 |
// the addrec. Temporarily push it as an operand for that purpose. These |
--- |
2886 |
// the addrec. Temporarily push it as an operand for that purpose. These |
--- |
| 2887 |
// flags are valid in the scope of the addrec only. |
--- |
2887 |
// flags are valid in the scope of the addrec only. |
--- |
| 2888 |
LIOps.push_back(AddRec); |
0 |
2888 |
LIOps.push_back(AddRec); |
0 |
| 2889 |
SCEV::NoWrapFlags Flags = ComputeFlags(LIOps); |
0 |
2889 |
SCEV::NoWrapFlags Flags = ComputeFlags(LIOps); |
0 |
| 2890 |
LIOps.pop_back(); |
0 |
2890 |
LIOps.pop_back(); |
0 |
| 2891 |
|
--- |
2891 |
|
--- |
| 2892 |
// NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} |
--- |
2892 |
// NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} |
--- |
| 2893 |
LIOps.push_back(AddRec->getStart()); |
0 |
2893 |
LIOps.push_back(AddRec->getStart()); |
0 |
| 2894 |
|
--- |
2894 |
|
--- |
| 2895 |
SmallVector AddRecOps(AddRec->operands()); |
0 |
2895 |
SmallVector AddRecOps(AddRec->operands()); |
0 |
| 2896 |
|
--- |
2896 |
|
--- |
| 2897 |
// It is not in general safe to propagate flags valid on an add within |
--- |
2897 |
// It is not in general safe to propagate flags valid on an add within |
--- |
| 2898 |
// the addrec scope to one outside it. We must prove that the inner |
--- |
2898 |
// the addrec scope to one outside it. We must prove that the inner |
--- |
| 2899 |
// scope is guaranteed to execute if the outer one does to be able to |
--- |
2899 |
// scope is guaranteed to execute if the outer one does to be able to |
--- |
| 2900 |
// safely propagate. We know the program is undefined if poison is |
--- |
2900 |
// safely propagate. We know the program is undefined if poison is |
--- |
| 2901 |
// produced on the inner scoped addrec. We also know that *for this use* |
--- |
2901 |
// produced on the inner scoped addrec. We also know that *for this use* |
--- |
| 2902 |
// the outer scoped add can't overflow (because of the flags we just |
--- |
2902 |
// the outer scoped add can't overflow (because of the flags we just |
--- |
| 2903 |
// computed for the inner scoped add) without the program being undefined. |
--- |
2903 |
// computed for the inner scoped add) without the program being undefined. |
--- |
| 2904 |
// Proving that entry to the outer scope neccesitates entry to the inner |
--- |
2904 |
// Proving that entry to the outer scope neccesitates entry to the inner |
--- |
| 2905 |
// scope, thus proves the program undefined if the flags would be violated |
--- |
2905 |
// scope, thus proves the program undefined if the flags would be violated |
--- |
| 2906 |
// in the outer scope. |
--- |
2906 |
// in the outer scope. |
--- |
| 2907 |
SCEV::NoWrapFlags AddFlags = Flags; |
0 |
2907 |
SCEV::NoWrapFlags AddFlags = Flags; |
0 |
| 2908 |
if (AddFlags != SCEV::FlagAnyWrap) { |
0 |
2908 |
if (AddFlags != SCEV::FlagAnyWrap) { |
0 |
| 2909 |
auto *DefI = getDefiningScopeBound(LIOps); |
0 |
2909 |
auto *DefI = getDefiningScopeBound(LIOps); |
0 |
| 2910 |
auto *ReachI = &*AddRecLoop->getHeader()->begin(); |
0 |
2910 |
auto *ReachI = &*AddRecLoop->getHeader()->begin(); |
0 |
| 2911 |
if (!isGuaranteedToTransferExecutionTo(DefI, ReachI)) |
0 |
2911 |
if (!isGuaranteedToTransferExecutionTo(DefI, ReachI)) |
0 |
| 2912 |
AddFlags = SCEV::FlagAnyWrap; |
0 |
2912 |
AddFlags = SCEV::FlagAnyWrap; |
0 |
| 2913 |
} |
--- |
2913 |
} |
--- |
| 2914 |
AddRecOps[0] = getAddExpr(LIOps, AddFlags, Depth + 1); |
0 |
2914 |
AddRecOps[0] = getAddExpr(LIOps, AddFlags, Depth + 1); |
0 |
| 2915 |
|
--- |
2915 |
|
--- |
| 2916 |
// Build the new addrec. Propagate the NUW and NSW flags if both the |
--- |
2916 |
// Build the new addrec. Propagate the NUW and NSW flags if both the |
--- |
| 2917 |
// outer add and the inner addrec are guaranteed to have no overflow. |
--- |
2917 |
// outer add and the inner addrec are guaranteed to have no overflow. |
--- |
| 2918 |
// Always propagate NW. |
--- |
2918 |
// Always propagate NW. |
--- |
| 2919 |
Flags = AddRec->getNoWrapFlags(setFlags(Flags, SCEV::FlagNW)); |
0 |
2919 |
Flags = AddRec->getNoWrapFlags(setFlags(Flags, SCEV::FlagNW)); |
0 |
| 2920 |
const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, Flags); |
0 |
2920 |
const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, Flags); |
0 |
| 2921 |
|
--- |
2921 |
|
--- |
| 2922 |
// If all of the other operands were loop invariant, we are done. |
--- |
2922 |
// If all of the other operands were loop invariant, we are done. |
--- |
| 2923 |
if (Ops.size() == 1) return NewRec; |
0 |
2923 |
if (Ops.size() == 1) return NewRec; |
0 |
| 2924 |
|
--- |
2924 |
|
--- |
| 2925 |
// Otherwise, add the folded AddRec by the non-invariant parts. |
--- |
2925 |
// Otherwise, add the folded AddRec by the non-invariant parts. |
--- |
| 2926 |
for (unsigned i = 0;; ++i) |
0 |
2926 |
for (unsigned i = 0;; ++i) |
0 |
| 2927 |
if (Ops[i] == AddRec) { |
0 |
2927 |
if (Ops[i] == AddRec) { |
0 |
| 2928 |
Ops[i] = NewRec; |
0 |
2928 |
Ops[i] = NewRec; |
0 |
| 2929 |
break; |
0 |
2929 |
break; |
0 |
| 2930 |
} |
--- |
2930 |
} |
--- |
| 2931 |
return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
2931 |
return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 2932 |
} |
0 |
2932 |
} |
0 |
| 2933 |
|
--- |
2933 |
|
--- |
| 2934 |
// Okay, if there weren't any loop invariants to be folded, check to see if |
--- |
2934 |
// Okay, if there weren't any loop invariants to be folded, check to see if |
--- |
| 2935 |
// there are multiple AddRec's with the same loop induction variable being |
--- |
2935 |
// there are multiple AddRec's with the same loop induction variable being |
--- |
| 2936 |
// added together. If so, we can fold them. |
--- |
2936 |
// added together. If so, we can fold them. |
--- |
| 2937 |
for (unsigned OtherIdx = Idx+1; |
0 |
2937 |
for (unsigned OtherIdx = Idx+1; |
0 |
| 2938 |
OtherIdx < Ops.size() && isa(Ops[OtherIdx]); |
0 |
2938 |
OtherIdx < Ops.size() && isa(Ops[OtherIdx]); |
0 |
| 2939 |
++OtherIdx) { |
--- |
2939 |
++OtherIdx) { |
--- |
| 2940 |
// We expect the AddRecExpr's to be sorted in reverse dominance order, |
--- |
2940 |
// We expect the AddRecExpr's to be sorted in reverse dominance order, |
--- |
| 2941 |
// so that the 1st found AddRecExpr is dominated by all others. |
--- |
2941 |
// so that the 1st found AddRecExpr is dominated by all others. |
--- |
| 2942 |
assert(DT.dominates( |
0 |
2942 |
assert(DT.dominates( |
0 |
| 2943 |
cast(Ops[OtherIdx])->getLoop()->getHeader(), |
--- |
2943 |
cast(Ops[OtherIdx])->getLoop()->getHeader(), |
--- |
| 2944 |
AddRec->getLoop()->getHeader()) && |
--- |
2944 |
AddRec->getLoop()->getHeader()) && |
--- |
| 2945 |
"AddRecExprs are not sorted in reverse dominance order?"); |
--- |
2945 |
"AddRecExprs are not sorted in reverse dominance order?"); |
--- |
| 2946 |
if (AddRecLoop == cast(Ops[OtherIdx])->getLoop()) { |
0 |
2946 |
if (AddRecLoop == cast(Ops[OtherIdx])->getLoop()) { |
0 |
| 2947 |
// Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D} |
--- |
2947 |
// Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D} |
--- |
| 2948 |
SmallVector AddRecOps(AddRec->operands()); |
0 |
2948 |
SmallVector AddRecOps(AddRec->operands()); |
0 |
| 2949 |
for (; OtherIdx != Ops.size() && isa(Ops[OtherIdx]); |
0 |
2949 |
for (; OtherIdx != Ops.size() && isa(Ops[OtherIdx]); |
0 |
| 2950 |
++OtherIdx) { |
--- |
2950 |
++OtherIdx) { |
--- |
| 2951 |
const auto *OtherAddRec = cast(Ops[OtherIdx]); |
0 |
2951 |
const auto *OtherAddRec = cast(Ops[OtherIdx]); |
0 |
| 2952 |
if (OtherAddRec->getLoop() == AddRecLoop) { |
0 |
2952 |
if (OtherAddRec->getLoop() == AddRecLoop) { |
0 |
| 2953 |
for (unsigned i = 0, e = OtherAddRec->getNumOperands(); |
0 |
2953 |
for (unsigned i = 0, e = OtherAddRec->getNumOperands(); |
0 |
| 2954 |
i != e; ++i) { |
0 |
2954 |
i != e; ++i) { |
0 |
| 2955 |
if (i >= AddRecOps.size()) { |
0 |
2955 |
if (i >= AddRecOps.size()) { |
0 |
| 2956 |
append_range(AddRecOps, OtherAddRec->operands().drop_front(i)); |
0 |
2956 |
append_range(AddRecOps, OtherAddRec->operands().drop_front(i)); |
0 |
| 2957 |
break; |
0 |
2957 |
break; |
0 |
| 2958 |
} |
--- |
2958 |
} |
--- |
| 2959 |
SmallVector TwoOps = { |
--- |
2959 |
SmallVector TwoOps = { |
--- |
| 2960 |
AddRecOps[i], OtherAddRec->getOperand(i)}; |
0 |
2960 |
AddRecOps[i], OtherAddRec->getOperand(i)}; |
0 |
| 2961 |
AddRecOps[i] = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
2961 |
AddRecOps[i] = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 2962 |
} |
0 |
2962 |
} |
0 |
| 2963 |
Ops.erase(Ops.begin() + OtherIdx); --OtherIdx; |
0 |
2963 |
Ops.erase(Ops.begin() + OtherIdx); --OtherIdx; |
0 |
| 2964 |
} |
--- |
2964 |
} |
--- |
| 2965 |
} |
--- |
2965 |
} |
--- |
| 2966 |
// Step size has changed, so we cannot guarantee no self-wraparound. |
--- |
2966 |
// Step size has changed, so we cannot guarantee no self-wraparound. |
--- |
| 2967 |
Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap); |
0 |
2967 |
Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap); |
0 |
| 2968 |
return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
2968 |
return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 2969 |
} |
0 |
2969 |
} |
0 |
| 2970 |
} |
--- |
2970 |
} |
--- |
| 2971 |
|
--- |
2971 |
|
--- |
| 2972 |
// Otherwise couldn't fold anything into this recurrence. Move onto the |
--- |
2972 |
// Otherwise couldn't fold anything into this recurrence. Move onto the |
--- |
| 2973 |
// next one. |
--- |
2973 |
// next one. |
--- |
| 2974 |
} |
0 |
2974 |
} |
0 |
| 2975 |
|
--- |
2975 |
|
--- |
| 2976 |
// Okay, it looks like we really DO need an add expr. Check to see if we |
--- |
2976 |
// Okay, it looks like we really DO need an add expr. Check to see if we |
--- |
| 2977 |
// already have one, otherwise create a new one. |
--- |
2977 |
// already have one, otherwise create a new one. |
--- |
| 2978 |
return getOrCreateAddExpr(Ops, ComputeFlags(Ops)); |
0 |
2978 |
return getOrCreateAddExpr(Ops, ComputeFlags(Ops)); |
0 |
| 2979 |
} |
--- |
2979 |
} |
--- |
| 2980 |
|
--- |
2980 |
|
--- |
| 2981 |
const SCEV * |
--- |
2981 |
const SCEV * |
--- |
| 2982 |
ScalarEvolution::getOrCreateAddExpr(ArrayRef Ops, |
0 |
2982 |
ScalarEvolution::getOrCreateAddExpr(ArrayRef Ops, |
0 |
| 2983 |
SCEV::NoWrapFlags Flags) { |
--- |
2983 |
SCEV::NoWrapFlags Flags) { |
--- |
| 2984 |
FoldingSetNodeID ID; |
0 |
2984 |
FoldingSetNodeID ID; |
0 |
| 2985 |
ID.AddInteger(scAddExpr); |
0 |
2985 |
ID.AddInteger(scAddExpr); |
0 |
| 2986 |
for (const SCEV *Op : Ops) |
0 |
2986 |
for (const SCEV *Op : Ops) |
0 |
| 2987 |
ID.AddPointer(Op); |
0 |
2987 |
ID.AddPointer(Op); |
0 |
| 2988 |
void *IP = nullptr; |
0 |
2988 |
void *IP = nullptr; |
0 |
| 2989 |
SCEVAddExpr *S = |
--- |
2989 |
SCEVAddExpr *S = |
--- |
| 2990 |
static_cast(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
0 |
2990 |
static_cast(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
0 |
| 2991 |
if (!S) { |
0 |
2991 |
if (!S) { |
0 |
| 2992 |
const SCEV **O = SCEVAllocator.Allocate(Ops.size()); |
0 |
2992 |
const SCEV **O = SCEVAllocator.Allocate(Ops.size()); |
0 |
| 2993 |
std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
0 |
2993 |
std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
0 |
| 2994 |
S = new (SCEVAllocator) |
0 |
2994 |
S = new (SCEVAllocator) |
0 |
| 2995 |
SCEVAddExpr(ID.Intern(SCEVAllocator), O, Ops.size()); |
0 |
2995 |
SCEVAddExpr(ID.Intern(SCEVAllocator), O, Ops.size()); |
0 |
| 2996 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
2996 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 2997 |
registerUser(S, Ops); |
0 |
2997 |
registerUser(S, Ops); |
0 |
| 2998 |
} |
--- |
2998 |
} |
--- |
| 2999 |
S->setNoWrapFlags(Flags); |
0 |
2999 |
S->setNoWrapFlags(Flags); |
0 |
| 3000 |
return S; |
0 |
3000 |
return S; |
0 |
| 3001 |
} |
0 |
3001 |
} |
0 |
| 3002 |
|
--- |
3002 |
|
--- |
| 3003 |
const SCEV * |
--- |
3003 |
const SCEV * |
--- |
| 3004 |
ScalarEvolution::getOrCreateAddRecExpr(ArrayRef Ops, |
0 |
3004 |
ScalarEvolution::getOrCreateAddRecExpr(ArrayRef Ops, |
0 |
| 3005 |
const Loop *L, SCEV::NoWrapFlags Flags) { |
--- |
3005 |
const Loop *L, SCEV::NoWrapFlags Flags) { |
--- |
| 3006 |
FoldingSetNodeID ID; |
0 |
3006 |
FoldingSetNodeID ID; |
0 |
| 3007 |
ID.AddInteger(scAddRecExpr); |
0 |
3007 |
ID.AddInteger(scAddRecExpr); |
0 |
| 3008 |
for (const SCEV *Op : Ops) |
0 |
3008 |
for (const SCEV *Op : Ops) |
0 |
| 3009 |
ID.AddPointer(Op); |
0 |
3009 |
ID.AddPointer(Op); |
0 |
| 3010 |
ID.AddPointer(L); |
0 |
3010 |
ID.AddPointer(L); |
0 |
| 3011 |
void *IP = nullptr; |
0 |
3011 |
void *IP = nullptr; |
0 |
| 3012 |
SCEVAddRecExpr *S = |
--- |
3012 |
SCEVAddRecExpr *S = |
--- |
| 3013 |
static_cast(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
0 |
3013 |
static_cast(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
0 |
| 3014 |
if (!S) { |
0 |
3014 |
if (!S) { |
0 |
| 3015 |
const SCEV **O = SCEVAllocator.Allocate(Ops.size()); |
0 |
3015 |
const SCEV **O = SCEVAllocator.Allocate(Ops.size()); |
0 |
| 3016 |
std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
0 |
3016 |
std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
0 |
| 3017 |
S = new (SCEVAllocator) |
0 |
3017 |
S = new (SCEVAllocator) |
0 |
| 3018 |
SCEVAddRecExpr(ID.Intern(SCEVAllocator), O, Ops.size(), L); |
0 |
3018 |
SCEVAddRecExpr(ID.Intern(SCEVAllocator), O, Ops.size(), L); |
0 |
| 3019 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
3019 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 3020 |
LoopUsers[L].push_back(S); |
0 |
3020 |
LoopUsers[L].push_back(S); |
0 |
| 3021 |
registerUser(S, Ops); |
0 |
3021 |
registerUser(S, Ops); |
0 |
| 3022 |
} |
--- |
3022 |
} |
--- |
| 3023 |
setNoWrapFlags(S, Flags); |
0 |
3023 |
setNoWrapFlags(S, Flags); |
0 |
| 3024 |
return S; |
0 |
3024 |
return S; |
0 |
| 3025 |
} |
0 |
3025 |
} |
0 |
| 3026 |
|
--- |
3026 |
|
--- |
| 3027 |
const SCEV * |
--- |
3027 |
const SCEV * |
--- |
| 3028 |
ScalarEvolution::getOrCreateMulExpr(ArrayRef Ops, |
0 |
3028 |
ScalarEvolution::getOrCreateMulExpr(ArrayRef Ops, |
0 |
| 3029 |
SCEV::NoWrapFlags Flags) { |
--- |
3029 |
SCEV::NoWrapFlags Flags) { |
--- |
| 3030 |
FoldingSetNodeID ID; |
0 |
3030 |
FoldingSetNodeID ID; |
0 |
| 3031 |
ID.AddInteger(scMulExpr); |
0 |
3031 |
ID.AddInteger(scMulExpr); |
0 |
| 3032 |
for (const SCEV *Op : Ops) |
0 |
3032 |
for (const SCEV *Op : Ops) |
0 |
| 3033 |
ID.AddPointer(Op); |
0 |
3033 |
ID.AddPointer(Op); |
0 |
| 3034 |
void *IP = nullptr; |
0 |
3034 |
void *IP = nullptr; |
0 |
| 3035 |
SCEVMulExpr *S = |
--- |
3035 |
SCEVMulExpr *S = |
--- |
| 3036 |
static_cast(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
0 |
3036 |
static_cast(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
0 |
| 3037 |
if (!S) { |
0 |
3037 |
if (!S) { |
0 |
| 3038 |
const SCEV **O = SCEVAllocator.Allocate(Ops.size()); |
0 |
3038 |
const SCEV **O = SCEVAllocator.Allocate(Ops.size()); |
0 |
| 3039 |
std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
0 |
3039 |
std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
0 |
| 3040 |
S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator), |
0 |
3040 |
S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator), |
0 |
| 3041 |
O, Ops.size()); |
0 |
3041 |
O, Ops.size()); |
0 |
| 3042 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
3042 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 3043 |
registerUser(S, Ops); |
0 |
3043 |
registerUser(S, Ops); |
0 |
| 3044 |
} |
--- |
3044 |
} |
--- |
| 3045 |
S->setNoWrapFlags(Flags); |
0 |
3045 |
S->setNoWrapFlags(Flags); |
0 |
| 3046 |
return S; |
0 |
3046 |
return S; |
0 |
| 3047 |
} |
0 |
3047 |
} |
0 |
| 3048 |
|
--- |
3048 |
|
--- |
| 3049 |
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow) { |
0 |
3049 |
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow) { |
0 |
| 3050 |
uint64_t k = i*j; |
0 |
3050 |
uint64_t k = i*j; |
0 |
| 3051 |
if (j > 1 && k / j != i) Overflow = true; |
0 |
3051 |
if (j > 1 && k / j != i) Overflow = true; |
0 |
| 3052 |
return k; |
0 |
3052 |
return k; |
0 |
| 3053 |
} |
--- |
3053 |
} |
--- |
| 3054 |
|
--- |
3054 |
|
--- |
| 3055 |
/// Compute the result of "n choose k", the binomial coefficient. If an |
--- |
3055 |
/// Compute the result of "n choose k", the binomial coefficient. If an |
--- |
| 3056 |
/// intermediate computation overflows, Overflow will be set and the return will |
--- |
3056 |
/// intermediate computation overflows, Overflow will be set and the return will |
--- |
| 3057 |
/// be garbage. Overflow is not cleared on absence of overflow. |
--- |
3057 |
/// be garbage. Overflow is not cleared on absence of overflow. |
--- |
| 3058 |
static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow) { |
0 |
3058 |
static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow) { |
0 |
| 3059 |
// We use the multiplicative formula: |
--- |
3059 |
// We use the multiplicative formula: |
--- |
| 3060 |
// n(n-1)(n-2)...(n-(k-1)) / k(k-1)(k-2)...1 . |
--- |
3060 |
// n(n-1)(n-2)...(n-(k-1)) / k(k-1)(k-2)...1 . |
--- |
| 3061 |
// At each iteration, we take the n-th term of the numeral and divide by the |
--- |
3061 |
// At each iteration, we take the n-th term of the numeral and divide by the |
--- |
| 3062 |
// (k-n)th term of the denominator. This division will always produce an |
--- |
3062 |
// (k-n)th term of the denominator. This division will always produce an |
--- |
| 3063 |
// integral result, and helps reduce the chance of overflow in the |
--- |
3063 |
// integral result, and helps reduce the chance of overflow in the |
--- |
| 3064 |
// intermediate computations. However, we can still overflow even when the |
--- |
3064 |
// intermediate computations. However, we can still overflow even when the |
--- |
| 3065 |
// final result would fit. |
--- |
3065 |
// final result would fit. |
--- |
| 3066 |
|
--- |
3066 |
|
--- |
| 3067 |
if (n == 0 || n == k) return 1; |
0 |
3067 |
if (n == 0 || n == k) return 1; |
0 |
| 3068 |
if (k > n) return 0; |
0 |
3068 |
if (k > n) return 0; |
0 |
| 3069 |
|
--- |
3069 |
|
--- |
| 3070 |
if (k > n/2) |
0 |
3070 |
if (k > n/2) |
0 |
| 3071 |
k = n-k; |
0 |
3071 |
k = n-k; |
0 |
| 3072 |
|
--- |
3072 |
|
--- |
| 3073 |
uint64_t r = 1; |
0 |
3073 |
uint64_t r = 1; |
0 |
| 3074 |
for (uint64_t i = 1; i <= k; ++i) { |
0 |
3074 |
for (uint64_t i = 1; i <= k; ++i) { |
0 |
| 3075 |
r = umul_ov(r, n-(i-1), Overflow); |
0 |
3075 |
r = umul_ov(r, n-(i-1), Overflow); |
0 |
| 3076 |
r /= i; |
0 |
3076 |
r /= i; |
0 |
| 3077 |
} |
--- |
3077 |
} |
--- |
| 3078 |
return r; |
0 |
3078 |
return r; |
0 |
| 3079 |
} |
--- |
3079 |
} |
--- |
| 3080 |
|
--- |
3080 |
|
--- |
| 3081 |
/// Determine if any of the operands in this SCEV are a constant or if |
--- |
3081 |
/// Determine if any of the operands in this SCEV are a constant or if |
--- |
| 3082 |
/// any of the add or multiply expressions in this SCEV contain a constant. |
--- |
3082 |
/// any of the add or multiply expressions in this SCEV contain a constant. |
--- |
| 3083 |
static bool containsConstantInAddMulChain(const SCEV *StartExpr) { |
0 |
3083 |
static bool containsConstantInAddMulChain(const SCEV *StartExpr) { |
0 |
| 3084 |
struct FindConstantInAddMulChain { |
--- |
3084 |
struct FindConstantInAddMulChain { |
--- |
| 3085 |
bool FoundConstant = false; |
--- |
3085 |
bool FoundConstant = false; |
--- |
| 3086 |
|
--- |
3086 |
|
--- |
| 3087 |
bool follow(const SCEV *S) { |
0 |
3087 |
bool follow(const SCEV *S) { |
0 |
| 3088 |
FoundConstant |= isa(S); |
0 |
3088 |
FoundConstant |= isa(S); |
0 |
| 3089 |
return isa(S) || isa(S); |
0 |
3089 |
return isa(S) || isa(S); |
0 |
| 3090 |
} |
--- |
3090 |
} |
--- |
| 3091 |
|
--- |
3091 |
|
--- |
| 3092 |
bool isDone() const { |
0 |
3092 |
bool isDone() const { |
0 |
| 3093 |
return FoundConstant; |
0 |
3093 |
return FoundConstant; |
0 |
| 3094 |
} |
--- |
3094 |
} |
--- |
| 3095 |
}; |
--- |
3095 |
}; |
--- |
| 3096 |
|
--- |
3096 |
|
--- |
| 3097 |
FindConstantInAddMulChain F; |
0 |
3097 |
FindConstantInAddMulChain F; |
0 |
| 3098 |
SCEVTraversal ST(F); |
0 |
3098 |
SCEVTraversal ST(F); |
0 |
| 3099 |
ST.visitAll(StartExpr); |
0 |
3099 |
ST.visitAll(StartExpr); |
0 |
| 3100 |
return F.FoundConstant; |
0 |
3100 |
return F.FoundConstant; |
0 |
| 3101 |
} |
0 |
3101 |
} |
0 |
| 3102 |
|
--- |
3102 |
|
--- |
| 3103 |
/// Get a canonical multiply expression, or something simpler if possible. |
--- |
3103 |
/// Get a canonical multiply expression, or something simpler if possible. |
--- |
| 3104 |
const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl &Ops, |
0 |
3104 |
const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl &Ops, |
0 |
| 3105 |
SCEV::NoWrapFlags OrigFlags, |
--- |
3105 |
SCEV::NoWrapFlags OrigFlags, |
--- |
| 3106 |
unsigned Depth) { |
--- |
3106 |
unsigned Depth) { |
--- |
| 3107 |
assert(OrigFlags == maskFlags(OrigFlags, SCEV::FlagNUW | SCEV::FlagNSW) && |
0 |
3107 |
assert(OrigFlags == maskFlags(OrigFlags, SCEV::FlagNUW | SCEV::FlagNSW) && |
0 |
| 3108 |
"only nuw or nsw allowed"); |
--- |
3108 |
"only nuw or nsw allowed"); |
--- |
| 3109 |
assert(!Ops.empty() && "Cannot get empty mul!"); |
0 |
3109 |
assert(!Ops.empty() && "Cannot get empty mul!"); |
0 |
| 3110 |
if (Ops.size() == 1) return Ops[0]; |
0 |
3110 |
if (Ops.size() == 1) return Ops[0]; |
0 |
| 3111 |
#ifndef NDEBUG |
--- |
3111 |
#ifndef NDEBUG |
--- |
| 3112 |
Type *ETy = Ops[0]->getType(); |
0 |
3112 |
Type *ETy = Ops[0]->getType(); |
0 |
| 3113 |
assert(!ETy->isPointerTy()); |
0 |
3113 |
assert(!ETy->isPointerTy()); |
0 |
| 3114 |
for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
0 |
3114 |
for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
0 |
| 3115 |
assert(Ops[i]->getType() == ETy && |
0 |
3115 |
assert(Ops[i]->getType() == ETy && |
0 |
| 3116 |
"SCEVMulExpr operand types don't match!"); |
--- |
3116 |
"SCEVMulExpr operand types don't match!"); |
--- |
| 3117 |
#endif |
--- |
3117 |
#endif |
--- |
| 3118 |
|
--- |
3118 |
|
--- |
| 3119 |
// Sort by complexity, this groups all similar expression types together. |
--- |
3119 |
// Sort by complexity, this groups all similar expression types together. |
--- |
| 3120 |
GroupByComplexity(Ops, &LI, DT); |
0 |
3120 |
GroupByComplexity(Ops, &LI, DT); |
0 |
| 3121 |
|
--- |
3121 |
|
--- |
| 3122 |
// If there are any constants, fold them together. |
--- |
3122 |
// If there are any constants, fold them together. |
--- |
| 3123 |
unsigned Idx = 0; |
0 |
3123 |
unsigned Idx = 0; |
0 |
| 3124 |
if (const SCEVConstant *LHSC = dyn_cast(Ops[0])) { |
0 |
3124 |
if (const SCEVConstant *LHSC = dyn_cast(Ops[0])) { |
0 |
| 3125 |
++Idx; |
0 |
3125 |
++Idx; |
0 |
| 3126 |
assert(Idx < Ops.size()); |
0 |
3126 |
assert(Idx < Ops.size()); |
0 |
| 3127 |
while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) { |
0 |
3127 |
while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) { |
0 |
| 3128 |
// We found two constants, fold them together! |
--- |
3128 |
// We found two constants, fold them together! |
--- |
| 3129 |
Ops[0] = getConstant(LHSC->getAPInt() * RHSC->getAPInt()); |
0 |
3129 |
Ops[0] = getConstant(LHSC->getAPInt() * RHSC->getAPInt()); |
0 |
| 3130 |
if (Ops.size() == 2) return Ops[0]; |
0 |
3130 |
if (Ops.size() == 2) return Ops[0]; |
0 |
| 3131 |
Ops.erase(Ops.begin()+1); // Erase the folded element |
0 |
3131 |
Ops.erase(Ops.begin()+1); // Erase the folded element |
0 |
| 3132 |
LHSC = cast(Ops[0]); |
0 |
3132 |
LHSC = cast(Ops[0]); |
0 |
| 3133 |
} |
0 |
3133 |
} |
0 |
| 3134 |
|
--- |
3134 |
|
--- |
| 3135 |
// If we have a multiply of zero, it will always be zero. |
--- |
3135 |
// If we have a multiply of zero, it will always be zero. |
--- |
| 3136 |
if (LHSC->getValue()->isZero()) |
0 |
3136 |
if (LHSC->getValue()->isZero()) |
0 |
| 3137 |
return LHSC; |
0 |
3137 |
return LHSC; |
0 |
| 3138 |
|
--- |
3138 |
|
--- |
| 3139 |
// If we are left with a constant one being multiplied, strip it off. |
--- |
3139 |
// If we are left with a constant one being multiplied, strip it off. |
--- |
| 3140 |
if (LHSC->getValue()->isOne()) { |
0 |
3140 |
if (LHSC->getValue()->isOne()) { |
0 |
| 3141 |
Ops.erase(Ops.begin()); |
0 |
3141 |
Ops.erase(Ops.begin()); |
0 |
| 3142 |
--Idx; |
0 |
3142 |
--Idx; |
0 |
| 3143 |
} |
--- |
3143 |
} |
--- |
| 3144 |
|
--- |
3144 |
|
--- |
| 3145 |
if (Ops.size() == 1) |
0 |
3145 |
if (Ops.size() == 1) |
0 |
| 3146 |
return Ops[0]; |
0 |
3146 |
return Ops[0]; |
0 |
| 3147 |
} |
--- |
3147 |
} |
--- |
| 3148 |
|
--- |
3148 |
|
--- |
| 3149 |
// Delay expensive flag strengthening until necessary. |
--- |
3149 |
// Delay expensive flag strengthening until necessary. |
--- |
| 3150 |
auto ComputeFlags = [this, OrigFlags](const ArrayRef Ops) { |
0 |
3150 |
auto ComputeFlags = [this, OrigFlags](const ArrayRef Ops) { |
0 |
| 3151 |
return StrengthenNoWrapFlags(this, scMulExpr, Ops, OrigFlags); |
0 |
3151 |
return StrengthenNoWrapFlags(this, scMulExpr, Ops, OrigFlags); |
0 |
| 3152 |
}; |
0 |
3152 |
}; |
0 |
| 3153 |
|
--- |
3153 |
|
--- |
| 3154 |
// Limit recursion calls depth. |
--- |
3154 |
// Limit recursion calls depth. |
--- |
| 3155 |
if (Depth > MaxArithDepth || hasHugeExpression(Ops)) |
0 |
3155 |
if (Depth > MaxArithDepth || hasHugeExpression(Ops)) |
0 |
| 3156 |
return getOrCreateMulExpr(Ops, ComputeFlags(Ops)); |
0 |
3156 |
return getOrCreateMulExpr(Ops, ComputeFlags(Ops)); |
0 |
| 3157 |
|
--- |
3157 |
|
--- |
| 3158 |
if (SCEV *S = findExistingSCEVInCache(scMulExpr, Ops)) { |
0 |
3158 |
if (SCEV *S = findExistingSCEVInCache(scMulExpr, Ops)) { |
0 |
| 3159 |
// Don't strengthen flags if we have no new information. |
--- |
3159 |
// Don't strengthen flags if we have no new information. |
--- |
| 3160 |
SCEVMulExpr *Mul = static_cast(S); |
0 |
3160 |
SCEVMulExpr *Mul = static_cast(S); |
0 |
| 3161 |
if (Mul->getNoWrapFlags(OrigFlags) != OrigFlags) |
0 |
3161 |
if (Mul->getNoWrapFlags(OrigFlags) != OrigFlags) |
0 |
| 3162 |
Mul->setNoWrapFlags(ComputeFlags(Ops)); |
0 |
3162 |
Mul->setNoWrapFlags(ComputeFlags(Ops)); |
0 |
| 3163 |
return S; |
0 |
3163 |
return S; |
0 |
| 3164 |
} |
--- |
3164 |
} |
--- |
| 3165 |
|
--- |
3165 |
|
--- |
| 3166 |
if (const SCEVConstant *LHSC = dyn_cast(Ops[0])) { |
0 |
3166 |
if (const SCEVConstant *LHSC = dyn_cast(Ops[0])) { |
0 |
| 3167 |
if (Ops.size() == 2) { |
0 |
3167 |
if (Ops.size() == 2) { |
0 |
| 3168 |
// C1*(C2+V) -> C1*C2 + C1*V |
--- |
3168 |
// C1*(C2+V) -> C1*C2 + C1*V |
--- |
| 3169 |
if (const SCEVAddExpr *Add = dyn_cast(Ops[1])) |
0 |
3169 |
if (const SCEVAddExpr *Add = dyn_cast(Ops[1])) |
0 |
| 3170 |
// If any of Add's ops are Adds or Muls with a constant, apply this |
--- |
3170 |
// If any of Add's ops are Adds or Muls with a constant, apply this |
--- |
| 3171 |
// transformation as well. |
--- |
3171 |
// transformation as well. |
--- |
| 3172 |
// |
--- |
3172 |
// |
--- |
| 3173 |
// TODO: There are some cases where this transformation is not |
--- |
3173 |
// TODO: There are some cases where this transformation is not |
--- |
| 3174 |
// profitable; for example, Add = (C0 + X) * Y + Z. Maybe the scope of |
--- |
3174 |
// profitable; for example, Add = (C0 + X) * Y + Z. Maybe the scope of |
--- |
| 3175 |
// this transformation should be narrowed down. |
--- |
3175 |
// this transformation should be narrowed down. |
--- |
| 3176 |
if (Add->getNumOperands() == 2 && containsConstantInAddMulChain(Add)) { |
0 |
3176 |
if (Add->getNumOperands() == 2 && containsConstantInAddMulChain(Add)) { |
0 |
| 3177 |
const SCEV *LHS = getMulExpr(LHSC, Add->getOperand(0), |
0 |
3177 |
const SCEV *LHS = getMulExpr(LHSC, Add->getOperand(0), |
0 |
| 3178 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
3178 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
| 3179 |
const SCEV *RHS = getMulExpr(LHSC, Add->getOperand(1), |
0 |
3179 |
const SCEV *RHS = getMulExpr(LHSC, Add->getOperand(1), |
0 |
| 3180 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
3180 |
SCEV::FlagAnyWrap, Depth + 1); |
--- |
| 3181 |
return getAddExpr(LHS, RHS, SCEV::FlagAnyWrap, Depth + 1); |
0 |
3181 |
return getAddExpr(LHS, RHS, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 3182 |
} |
--- |
3182 |
} |
--- |
| 3183 |
|
--- |
3183 |
|
--- |
| 3184 |
if (Ops[0]->isAllOnesValue()) { |
0 |
3184 |
if (Ops[0]->isAllOnesValue()) { |
0 |
| 3185 |
// If we have a mul by -1 of an add, try distributing the -1 among the |
--- |
3185 |
// If we have a mul by -1 of an add, try distributing the -1 among the |
--- |
| 3186 |
// add operands. |
--- |
3186 |
// add operands. |
--- |
| 3187 |
if (const SCEVAddExpr *Add = dyn_cast(Ops[1])) { |
0 |
3187 |
if (const SCEVAddExpr *Add = dyn_cast(Ops[1])) { |
0 |
| 3188 |
SmallVector NewOps; |
0 |
3188 |
SmallVector NewOps; |
0 |
| 3189 |
bool AnyFolded = false; |
0 |
3189 |
bool AnyFolded = false; |
0 |
| 3190 |
for (const SCEV *AddOp : Add->operands()) { |
0 |
3190 |
for (const SCEV *AddOp : Add->operands()) { |
0 |
| 3191 |
const SCEV *Mul = getMulExpr(Ops[0], AddOp, SCEV::FlagAnyWrap, |
0 |
3191 |
const SCEV *Mul = getMulExpr(Ops[0], AddOp, SCEV::FlagAnyWrap, |
0 |
| 3192 |
Depth + 1); |
0 |
3192 |
Depth + 1); |
0 |
| 3193 |
if (!isa(Mul)) AnyFolded = true; |
0 |
3193 |
if (!isa(Mul)) AnyFolded = true; |
0 |
| 3194 |
NewOps.push_back(Mul); |
0 |
3194 |
NewOps.push_back(Mul); |
0 |
| 3195 |
} |
--- |
3195 |
} |
--- |
| 3196 |
if (AnyFolded) |
0 |
3196 |
if (AnyFolded) |
0 |
| 3197 |
return getAddExpr(NewOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
3197 |
return getAddExpr(NewOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 3198 |
} else if (const auto *AddRec = dyn_cast(Ops[1])) { |
0 |
3198 |
} else if (const auto *AddRec = dyn_cast(Ops[1])) { |
0 |
| 3199 |
// Negation preserves a recurrence's no self-wrap property. |
--- |
3199 |
// Negation preserves a recurrence's no self-wrap property. |
--- |
| 3200 |
SmallVector Operands; |
0 |
3200 |
SmallVector Operands; |
0 |
| 3201 |
for (const SCEV *AddRecOp : AddRec->operands()) |
0 |
3201 |
for (const SCEV *AddRecOp : AddRec->operands()) |
0 |
| 3202 |
Operands.push_back(getMulExpr(Ops[0], AddRecOp, SCEV::FlagAnyWrap, |
0 |
3202 |
Operands.push_back(getMulExpr(Ops[0], AddRecOp, SCEV::FlagAnyWrap, |
0 |
| 3203 |
Depth + 1)); |
--- |
3203 |
Depth + 1)); |
--- |
| 3204 |
// Let M be the minimum representable signed value. AddRec with nsw |
--- |
3204 |
// Let M be the minimum representable signed value. AddRec with nsw |
--- |
| 3205 |
// multiplied by -1 can have signed overflow if and only if it takes a |
--- |
3205 |
// multiplied by -1 can have signed overflow if and only if it takes a |
--- |
| 3206 |
// value of M: M * (-1) would stay M and (M + 1) * (-1) would be the |
--- |
3206 |
// value of M: M * (-1) would stay M and (M + 1) * (-1) would be the |
--- |
| 3207 |
// maximum signed value. In all other cases signed overflow is |
--- |
3207 |
// maximum signed value. In all other cases signed overflow is |
--- |
| 3208 |
// impossible. |
--- |
3208 |
// impossible. |
--- |
| 3209 |
auto FlagsMask = SCEV::FlagNW; |
0 |
3209 |
auto FlagsMask = SCEV::FlagNW; |
0 |
| 3210 |
if (hasFlags(AddRec->getNoWrapFlags(), SCEV::FlagNSW)) { |
0 |
3210 |
if (hasFlags(AddRec->getNoWrapFlags(), SCEV::FlagNSW)) { |
0 |
| 3211 |
auto MinInt = |
--- |
3211 |
auto MinInt = |
--- |
| 3212 |
APInt::getSignedMinValue(getTypeSizeInBits(AddRec->getType())); |
0 |
3212 |
APInt::getSignedMinValue(getTypeSizeInBits(AddRec->getType())); |
0 |
| 3213 |
if (getSignedRangeMin(AddRec) != MinInt) |
0 |
3213 |
if (getSignedRangeMin(AddRec) != MinInt) |
0 |
| 3214 |
FlagsMask = setFlags(FlagsMask, SCEV::FlagNSW); |
0 |
3214 |
FlagsMask = setFlags(FlagsMask, SCEV::FlagNSW); |
0 |
| 3215 |
} |
0 |
3215 |
} |
0 |
| 3216 |
return getAddRecExpr(Operands, AddRec->getLoop(), |
0 |
3216 |
return getAddRecExpr(Operands, AddRec->getLoop(), |
0 |
| 3217 |
AddRec->getNoWrapFlags(FlagsMask)); |
0 |
3217 |
AddRec->getNoWrapFlags(FlagsMask)); |
0 |
| 3218 |
} |
0 |
3218 |
} |
0 |
| 3219 |
} |
--- |
3219 |
} |
--- |
| 3220 |
} |
--- |
3220 |
} |
--- |
| 3221 |
} |
--- |
3221 |
} |
--- |
| 3222 |
|
--- |
3222 |
|
--- |
| 3223 |
// Skip over the add expression until we get to a multiply. |
--- |
3223 |
// Skip over the add expression until we get to a multiply. |
--- |
| 3224 |
while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
0 |
3224 |
while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
0 |
| 3225 |
++Idx; |
0 |
3225 |
++Idx; |
0 |
| 3226 |
|
--- |
3226 |
|
--- |
| 3227 |
// If there are mul operands inline them all into this expression. |
--- |
3227 |
// If there are mul operands inline them all into this expression. |
--- |
| 3228 |
if (Idx < Ops.size()) { |
0 |
3228 |
if (Idx < Ops.size()) { |
0 |
| 3229 |
bool DeletedMul = false; |
0 |
3229 |
bool DeletedMul = false; |
0 |
| 3230 |
while (const SCEVMulExpr *Mul = dyn_cast(Ops[Idx])) { |
0 |
3230 |
while (const SCEVMulExpr *Mul = dyn_cast(Ops[Idx])) { |
0 |
| 3231 |
if (Ops.size() > MulOpsInlineThreshold) |
0 |
3231 |
if (Ops.size() > MulOpsInlineThreshold) |
0 |
| 3232 |
break; |
0 |
3232 |
break; |
0 |
| 3233 |
// If we have an mul, expand the mul operands onto the end of the |
--- |
3233 |
// If we have an mul, expand the mul operands onto the end of the |
--- |
| 3234 |
// operands list. |
--- |
3234 |
// operands list. |
--- |
| 3235 |
Ops.erase(Ops.begin()+Idx); |
0 |
3235 |
Ops.erase(Ops.begin()+Idx); |
0 |
| 3236 |
append_range(Ops, Mul->operands()); |
0 |
3236 |
append_range(Ops, Mul->operands()); |
0 |
| 3237 |
DeletedMul = true; |
0 |
3237 |
DeletedMul = true; |
0 |
| 3238 |
} |
0 |
3238 |
} |
0 |
| 3239 |
|
--- |
3239 |
|
--- |
| 3240 |
// If we deleted at least one mul, we added operands to the end of the |
--- |
3240 |
// If we deleted at least one mul, we added operands to the end of the |
--- |
| 3241 |
// list, and they are not necessarily sorted. Recurse to resort and |
--- |
3241 |
// list, and they are not necessarily sorted. Recurse to resort and |
--- |
| 3242 |
// resimplify any operands we just acquired. |
--- |
3242 |
// resimplify any operands we just acquired. |
--- |
| 3243 |
if (DeletedMul) |
0 |
3243 |
if (DeletedMul) |
0 |
| 3244 |
return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
3244 |
return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 3245 |
} |
--- |
3245 |
} |
--- |
| 3246 |
|
--- |
3246 |
|
--- |
| 3247 |
// If there are any add recurrences in the operands list, see if any other |
--- |
3247 |
// If there are any add recurrences in the operands list, see if any other |
--- |
| 3248 |
// added values are loop invariant. If so, we can fold them into the |
--- |
3248 |
// added values are loop invariant. If so, we can fold them into the |
--- |
| 3249 |
// recurrence. |
--- |
3249 |
// recurrence. |
--- |
| 3250 |
while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
0 |
3250 |
while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
0 |
| 3251 |
++Idx; |
0 |
3251 |
++Idx; |
0 |
| 3252 |
|
--- |
3252 |
|
--- |
| 3253 |
// Scan over all recurrences, trying to fold loop invariants into them. |
--- |
3253 |
// Scan over all recurrences, trying to fold loop invariants into them. |
--- |
| 3254 |
for (; Idx < Ops.size() && isa(Ops[Idx]); ++Idx) { |
0 |
3254 |
for (; Idx < Ops.size() && isa(Ops[Idx]); ++Idx) { |
0 |
| 3255 |
// Scan all of the other operands to this mul and add them to the vector |
--- |
3255 |
// Scan all of the other operands to this mul and add them to the vector |
--- |
| 3256 |
// if they are loop invariant w.r.t. the recurrence. |
--- |
3256 |
// if they are loop invariant w.r.t. the recurrence. |
--- |
| 3257 |
SmallVector LIOps; |
0 |
3257 |
SmallVector LIOps; |
0 |
| 3258 |
const SCEVAddRecExpr *AddRec = cast(Ops[Idx]); |
0 |
3258 |
const SCEVAddRecExpr *AddRec = cast(Ops[Idx]); |
0 |
| 3259 |
for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
0 |
3259 |
for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
0 |
| 3260 |
if (isAvailableAtLoopEntry(Ops[i], AddRec->getLoop())) { |
0 |
3260 |
if (isAvailableAtLoopEntry(Ops[i], AddRec->getLoop())) { |
0 |
| 3261 |
LIOps.push_back(Ops[i]); |
0 |
3261 |
LIOps.push_back(Ops[i]); |
0 |
| 3262 |
Ops.erase(Ops.begin()+i); |
0 |
3262 |
Ops.erase(Ops.begin()+i); |
0 |
| 3263 |
--i; --e; |
0 |
3263 |
--i; --e; |
0 |
| 3264 |
} |
--- |
3264 |
} |
--- |
| 3265 |
|
--- |
3265 |
|
--- |
| 3266 |
// If we found some loop invariants, fold them into the recurrence. |
--- |
3266 |
// If we found some loop invariants, fold them into the recurrence. |
--- |
| 3267 |
if (!LIOps.empty()) { |
0 |
3267 |
if (!LIOps.empty()) { |
0 |
| 3268 |
// NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} |
--- |
3268 |
// NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} |
--- |
| 3269 |
SmallVector NewOps; |
0 |
3269 |
SmallVector NewOps; |
0 |
| 3270 |
NewOps.reserve(AddRec->getNumOperands()); |
0 |
3270 |
NewOps.reserve(AddRec->getNumOperands()); |
0 |
| 3271 |
const SCEV *Scale = getMulExpr(LIOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
3271 |
const SCEV *Scale = getMulExpr(LIOps, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 3272 |
for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
0 |
3272 |
for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
0 |
| 3273 |
NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i), |
0 |
3273 |
NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i), |
0 |
| 3274 |
SCEV::FlagAnyWrap, Depth + 1)); |
--- |
3274 |
SCEV::FlagAnyWrap, Depth + 1)); |
--- |
| 3275 |
|
--- |
3275 |
|
--- |
| 3276 |
// Build the new addrec. Propagate the NUW and NSW flags if both the |
--- |
3276 |
// Build the new addrec. Propagate the NUW and NSW flags if both the |
--- |
| 3277 |
// outer mul and the inner addrec are guaranteed to have no overflow. |
--- |
3277 |
// outer mul and the inner addrec are guaranteed to have no overflow. |
--- |
| 3278 |
// |
--- |
3278 |
// |
--- |
| 3279 |
// No self-wrap cannot be guaranteed after changing the step size, but |
--- |
3279 |
// No self-wrap cannot be guaranteed after changing the step size, but |
--- |
| 3280 |
// will be inferred if either NUW or NSW is true. |
--- |
3280 |
// will be inferred if either NUW or NSW is true. |
--- |
| 3281 |
SCEV::NoWrapFlags Flags = ComputeFlags({Scale, AddRec}); |
0 |
3281 |
SCEV::NoWrapFlags Flags = ComputeFlags({Scale, AddRec}); |
0 |
| 3282 |
const SCEV *NewRec = getAddRecExpr( |
0 |
3282 |
const SCEV *NewRec = getAddRecExpr( |
0 |
| 3283 |
NewOps, AddRec->getLoop(), AddRec->getNoWrapFlags(Flags)); |
--- |
3283 |
NewOps, AddRec->getLoop(), AddRec->getNoWrapFlags(Flags)); |
--- |
| 3284 |
|
--- |
3284 |
|
--- |
| 3285 |
// If all of the other operands were loop invariant, we are done. |
--- |
3285 |
// If all of the other operands were loop invariant, we are done. |
--- |
| 3286 |
if (Ops.size() == 1) return NewRec; |
0 |
3286 |
if (Ops.size() == 1) return NewRec; |
0 |
| 3287 |
|
--- |
3287 |
|
--- |
| 3288 |
// Otherwise, multiply the folded AddRec by the non-invariant parts. |
--- |
3288 |
// Otherwise, multiply the folded AddRec by the non-invariant parts. |
--- |
| 3289 |
for (unsigned i = 0;; ++i) |
0 |
3289 |
for (unsigned i = 0;; ++i) |
0 |
| 3290 |
if (Ops[i] == AddRec) { |
0 |
3290 |
if (Ops[i] == AddRec) { |
0 |
| 3291 |
Ops[i] = NewRec; |
0 |
3291 |
Ops[i] = NewRec; |
0 |
| 3292 |
break; |
0 |
3292 |
break; |
0 |
| 3293 |
} |
--- |
3293 |
} |
--- |
| 3294 |
return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
3294 |
return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 3295 |
} |
0 |
3295 |
} |
0 |
| 3296 |
|
--- |
3296 |
|
--- |
| 3297 |
// Okay, if there weren't any loop invariants to be folded, check to see |
--- |
3297 |
// Okay, if there weren't any loop invariants to be folded, check to see |
--- |
| 3298 |
// if there are multiple AddRec's with the same loop induction variable |
--- |
3298 |
// if there are multiple AddRec's with the same loop induction variable |
--- |
| 3299 |
// being multiplied together. If so, we can fold them. |
--- |
3299 |
// being multiplied together. If so, we can fold them. |
--- |
| 3300 |
|
--- |
3300 |
|
--- |
| 3301 |
// {A1,+,A2,+,...,+,An} * {B1,+,B2,+,...,+,Bn} |
--- |
3301 |
// {A1,+,A2,+,...,+,An} * {B1,+,B2,+,...,+,Bn} |
--- |
| 3302 |
// = {x=1 in [ sum y=x..2x [ sum z=max(y-x, y-n)..min(x,n) [ |
--- |
3302 |
// = {x=1 in [ sum y=x..2x [ sum z=max(y-x, y-n)..min(x,n) [ |
--- |
| 3303 |
// choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z |
--- |
3303 |
// choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z |
--- |
| 3304 |
// ]]],+,...up to x=2n}. |
--- |
3304 |
// ]]],+,...up to x=2n}. |
--- |
| 3305 |
// Note that the arguments to choose() are always integers with values |
--- |
3305 |
// Note that the arguments to choose() are always integers with values |
--- |
| 3306 |
// known at compile time, never SCEV objects. |
--- |
3306 |
// known at compile time, never SCEV objects. |
--- |
| 3307 |
// |
--- |
3307 |
// |
--- |
| 3308 |
// The implementation avoids pointless extra computations when the two |
--- |
3308 |
// The implementation avoids pointless extra computations when the two |
--- |
| 3309 |
// addrec's are of different length (mathematically, it's equivalent to |
--- |
3309 |
// addrec's are of different length (mathematically, it's equivalent to |
--- |
| 3310 |
// an infinite stream of zeros on the right). |
--- |
3310 |
// an infinite stream of zeros on the right). |
--- |
| 3311 |
bool OpsModified = false; |
0 |
3311 |
bool OpsModified = false; |
0 |
| 3312 |
for (unsigned OtherIdx = Idx+1; |
0 |
3312 |
for (unsigned OtherIdx = Idx+1; |
0 |
| 3313 |
OtherIdx != Ops.size() && isa(Ops[OtherIdx]); |
0 |
3313 |
OtherIdx != Ops.size() && isa(Ops[OtherIdx]); |
0 |
| 3314 |
++OtherIdx) { |
--- |
3314 |
++OtherIdx) { |
--- |
| 3315 |
const SCEVAddRecExpr *OtherAddRec = |
--- |
3315 |
const SCEVAddRecExpr *OtherAddRec = |
--- |
| 3316 |
dyn_cast(Ops[OtherIdx]); |
0 |
3316 |
dyn_cast(Ops[OtherIdx]); |
0 |
| 3317 |
if (!OtherAddRec || OtherAddRec->getLoop() != AddRec->getLoop()) |
0 |
3317 |
if (!OtherAddRec || OtherAddRec->getLoop() != AddRec->getLoop()) |
0 |
| 3318 |
continue; |
0 |
3318 |
continue; |
0 |
| 3319 |
|
--- |
3319 |
|
--- |
| 3320 |
// Limit max number of arguments to avoid creation of unreasonably big |
--- |
3320 |
// Limit max number of arguments to avoid creation of unreasonably big |
--- |
| 3321 |
// SCEVAddRecs with very complex operands. |
--- |
3321 |
// SCEVAddRecs with very complex operands. |
--- |
| 3322 |
if (AddRec->getNumOperands() + OtherAddRec->getNumOperands() - 1 > |
0 |
3322 |
if (AddRec->getNumOperands() + OtherAddRec->getNumOperands() - 1 > |
0 |
| 3323 |
MaxAddRecSize || hasHugeExpression({AddRec, OtherAddRec})) |
0 |
3323 |
MaxAddRecSize || hasHugeExpression({AddRec, OtherAddRec})) |
0 |
| 3324 |
continue; |
0 |
3324 |
continue; |
0 |
| 3325 |
|
--- |
3325 |
|
--- |
| 3326 |
bool Overflow = false; |
0 |
3326 |
bool Overflow = false; |
0 |
| 3327 |
Type *Ty = AddRec->getType(); |
0 |
3327 |
Type *Ty = AddRec->getType(); |
0 |
| 3328 |
bool LargerThan64Bits = getTypeSizeInBits(Ty) > 64; |
0 |
3328 |
bool LargerThan64Bits = getTypeSizeInBits(Ty) > 64; |
0 |
| 3329 |
SmallVector AddRecOps; |
0 |
3329 |
SmallVector AddRecOps; |
0 |
| 3330 |
for (int x = 0, xe = AddRec->getNumOperands() + |
0 |
3330 |
for (int x = 0, xe = AddRec->getNumOperands() + |
0 |
| 3331 |
OtherAddRec->getNumOperands() - 1; x != xe && !Overflow; ++x) { |
0 |
3331 |
OtherAddRec->getNumOperands() - 1; x != xe && !Overflow; ++x) { |
0 |
| 3332 |
SmallVector SumOps; |
0 |
3332 |
SmallVector SumOps; |
0 |
| 3333 |
for (int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) { |
0 |
3333 |
for (int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) { |
0 |
| 3334 |
uint64_t Coeff1 = Choose(x, 2*x - y, Overflow); |
0 |
3334 |
uint64_t Coeff1 = Choose(x, 2*x - y, Overflow); |
0 |
| 3335 |
for (int z = std::max(y-x, y-(int)AddRec->getNumOperands()+1), |
0 |
3335 |
for (int z = std::max(y-x, y-(int)AddRec->getNumOperands()+1), |
0 |
| 3336 |
ze = std::min(x+1, (int)OtherAddRec->getNumOperands()); |
0 |
3336 |
ze = std::min(x+1, (int)OtherAddRec->getNumOperands()); |
0 |
| 3337 |
z < ze && !Overflow; ++z) { |
0 |
3337 |
z < ze && !Overflow; ++z) { |
0 |
| 3338 |
uint64_t Coeff2 = Choose(2*x - y, x-z, Overflow); |
0 |
3338 |
uint64_t Coeff2 = Choose(2*x - y, x-z, Overflow); |
0 |
| 3339 |
uint64_t Coeff; |
--- |
3339 |
uint64_t Coeff; |
--- |
| 3340 |
if (LargerThan64Bits) |
0 |
3340 |
if (LargerThan64Bits) |
0 |
| 3341 |
Coeff = umul_ov(Coeff1, Coeff2, Overflow); |
0 |
3341 |
Coeff = umul_ov(Coeff1, Coeff2, Overflow); |
0 |
| 3342 |
else |
--- |
3342 |
else |
--- |
| 3343 |
Coeff = Coeff1*Coeff2; |
0 |
3343 |
Coeff = Coeff1*Coeff2; |
0 |
| 3344 |
const SCEV *CoeffTerm = getConstant(Ty, Coeff); |
0 |
3344 |
const SCEV *CoeffTerm = getConstant(Ty, Coeff); |
0 |
| 3345 |
const SCEV *Term1 = AddRec->getOperand(y-z); |
0 |
3345 |
const SCEV *Term1 = AddRec->getOperand(y-z); |
0 |
| 3346 |
const SCEV *Term2 = OtherAddRec->getOperand(z); |
0 |
3346 |
const SCEV *Term2 = OtherAddRec->getOperand(z); |
0 |
| 3347 |
SumOps.push_back(getMulExpr(CoeffTerm, Term1, Term2, |
0 |
3347 |
SumOps.push_back(getMulExpr(CoeffTerm, Term1, Term2, |
0 |
| 3348 |
SCEV::FlagAnyWrap, Depth + 1)); |
--- |
3348 |
SCEV::FlagAnyWrap, Depth + 1)); |
--- |
| 3349 |
} |
--- |
3349 |
} |
--- |
| 3350 |
} |
--- |
3350 |
} |
--- |
| 3351 |
if (SumOps.empty()) |
0 |
3351 |
if (SumOps.empty()) |
0 |
| 3352 |
SumOps.push_back(getZero(Ty)); |
0 |
3352 |
SumOps.push_back(getZero(Ty)); |
0 |
| 3353 |
AddRecOps.push_back(getAddExpr(SumOps, SCEV::FlagAnyWrap, Depth + 1)); |
0 |
3353 |
AddRecOps.push_back(getAddExpr(SumOps, SCEV::FlagAnyWrap, Depth + 1)); |
0 |
| 3354 |
} |
0 |
3354 |
} |
0 |
| 3355 |
if (!Overflow) { |
0 |
3355 |
if (!Overflow) { |
0 |
| 3356 |
const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRec->getLoop(), |
0 |
3356 |
const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRec->getLoop(), |
0 |
| 3357 |
SCEV::FlagAnyWrap); |
--- |
3357 |
SCEV::FlagAnyWrap); |
--- |
| 3358 |
if (Ops.size() == 2) return NewAddRec; |
0 |
3358 |
if (Ops.size() == 2) return NewAddRec; |
0 |
| 3359 |
Ops[Idx] = NewAddRec; |
0 |
3359 |
Ops[Idx] = NewAddRec; |
0 |
| 3360 |
Ops.erase(Ops.begin() + OtherIdx); --OtherIdx; |
0 |
3360 |
Ops.erase(Ops.begin() + OtherIdx); --OtherIdx; |
0 |
| 3361 |
OpsModified = true; |
0 |
3361 |
OpsModified = true; |
0 |
| 3362 |
AddRec = dyn_cast(NewAddRec); |
0 |
3362 |
AddRec = dyn_cast(NewAddRec); |
0 |
| 3363 |
if (!AddRec) |
0 |
3363 |
if (!AddRec) |
0 |
| 3364 |
break; |
0 |
3364 |
break; |
0 |
| 3365 |
} |
--- |
3365 |
} |
--- |
| 3366 |
} |
0 |
3366 |
} |
0 |
| 3367 |
if (OpsModified) |
0 |
3367 |
if (OpsModified) |
0 |
| 3368 |
return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
3368 |
return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); |
0 |
| 3369 |
|
--- |
3369 |
|
--- |
| 3370 |
// Otherwise couldn't fold anything into this recurrence. Move onto the |
--- |
3370 |
// Otherwise couldn't fold anything into this recurrence. Move onto the |
--- |
| 3371 |
// next one. |
--- |
3371 |
// next one. |
--- |
| 3372 |
} |
0 |
3372 |
} |
0 |
| 3373 |
|
--- |
3373 |
|
--- |
| 3374 |
// Okay, it looks like we really DO need an mul expr. Check to see if we |
--- |
3374 |
// Okay, it looks like we really DO need an mul expr. Check to see if we |
--- |
| 3375 |
// already have one, otherwise create a new one. |
--- |
3375 |
// already have one, otherwise create a new one. |
--- |
| 3376 |
return getOrCreateMulExpr(Ops, ComputeFlags(Ops)); |
0 |
3376 |
return getOrCreateMulExpr(Ops, ComputeFlags(Ops)); |
0 |
| 3377 |
} |
--- |
3377 |
} |
--- |
| 3378 |
|
--- |
3378 |
|
--- |
| 3379 |
/// Represents an unsigned remainder expression based on unsigned division. |
--- |
3379 |
/// Represents an unsigned remainder expression based on unsigned division. |
--- |
| 3380 |
const SCEV *ScalarEvolution::getURemExpr(const SCEV *LHS, |
0 |
3380 |
const SCEV *ScalarEvolution::getURemExpr(const SCEV *LHS, |
0 |
| 3381 |
const SCEV *RHS) { |
--- |
3381 |
const SCEV *RHS) { |
--- |
| 3382 |
assert(getEffectiveSCEVType(LHS->getType()) == |
0 |
3382 |
assert(getEffectiveSCEVType(LHS->getType()) == |
0 |
| 3383 |
getEffectiveSCEVType(RHS->getType()) && |
--- |
3383 |
getEffectiveSCEVType(RHS->getType()) && |
--- |
| 3384 |
"SCEVURemExpr operand types don't match!"); |
--- |
3384 |
"SCEVURemExpr operand types don't match!"); |
--- |
| 3385 |
|
--- |
3385 |
|
--- |
| 3386 |
// Short-circuit easy cases |
--- |
3386 |
// Short-circuit easy cases |
--- |
| 3387 |
if (const SCEVConstant *RHSC = dyn_cast(RHS)) { |
0 |
3387 |
if (const SCEVConstant *RHSC = dyn_cast(RHS)) { |
0 |
| 3388 |
// If constant is one, the result is trivial |
--- |
3388 |
// If constant is one, the result is trivial |
--- |
| 3389 |
if (RHSC->getValue()->isOne()) |
0 |
3389 |
if (RHSC->getValue()->isOne()) |
0 |
| 3390 |
return getZero(LHS->getType()); // X urem 1 --> 0 |
0 |
3390 |
return getZero(LHS->getType()); // X urem 1 --> 0 |
0 |
| 3391 |
|
--- |
3391 |
|
--- |
| 3392 |
// If constant is a power of two, fold into a zext(trunc(LHS)). |
--- |
3392 |
// If constant is a power of two, fold into a zext(trunc(LHS)). |
--- |
| 3393 |
if (RHSC->getAPInt().isPowerOf2()) { |
0 |
3393 |
if (RHSC->getAPInt().isPowerOf2()) { |
0 |
| 3394 |
Type *FullTy = LHS->getType(); |
0 |
3394 |
Type *FullTy = LHS->getType(); |
0 |
| 3395 |
Type *TruncTy = |
--- |
3395 |
Type *TruncTy = |
--- |
| 3396 |
IntegerType::get(getContext(), RHSC->getAPInt().logBase2()); |
0 |
3396 |
IntegerType::get(getContext(), RHSC->getAPInt().logBase2()); |
0 |
| 3397 |
return getZeroExtendExpr(getTruncateExpr(LHS, TruncTy), FullTy); |
0 |
3397 |
return getZeroExtendExpr(getTruncateExpr(LHS, TruncTy), FullTy); |
0 |
| 3398 |
} |
--- |
3398 |
} |
--- |
| 3399 |
} |
--- |
3399 |
} |
--- |
| 3400 |
|
--- |
3400 |
|
--- |
| 3401 |
// Fallback to %a == %x urem %y == %x - ((%x udiv %y) * %y) |
--- |
3401 |
// Fallback to %a == %x urem %y == %x - ((%x udiv %y) * %y) |
--- |
| 3402 |
const SCEV *UDiv = getUDivExpr(LHS, RHS); |
0 |
3402 |
const SCEV *UDiv = getUDivExpr(LHS, RHS); |
0 |
| 3403 |
const SCEV *Mult = getMulExpr(UDiv, RHS, SCEV::FlagNUW); |
0 |
3403 |
const SCEV *Mult = getMulExpr(UDiv, RHS, SCEV::FlagNUW); |
0 |
| 3404 |
return getMinusSCEV(LHS, Mult, SCEV::FlagNUW); |
0 |
3404 |
return getMinusSCEV(LHS, Mult, SCEV::FlagNUW); |
0 |
| 3405 |
} |
--- |
3405 |
} |
--- |
| 3406 |
|
--- |
3406 |
|
--- |
| 3407 |
/// Get a canonical unsigned division expression, or something simpler if |
--- |
3407 |
/// Get a canonical unsigned division expression, or something simpler if |
--- |
| 3408 |
/// possible. |
--- |
3408 |
/// possible. |
--- |
| 3409 |
const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, |
0 |
3409 |
const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, |
0 |
| 3410 |
const SCEV *RHS) { |
--- |
3410 |
const SCEV *RHS) { |
--- |
| 3411 |
assert(!LHS->getType()->isPointerTy() && |
0 |
3411 |
assert(!LHS->getType()->isPointerTy() && |
0 |
| 3412 |
"SCEVUDivExpr operand can't be pointer!"); |
--- |
3412 |
"SCEVUDivExpr operand can't be pointer!"); |
--- |
| 3413 |
assert(LHS->getType() == RHS->getType() && |
0 |
3413 |
assert(LHS->getType() == RHS->getType() && |
0 |
| 3414 |
"SCEVUDivExpr operand types don't match!"); |
--- |
3414 |
"SCEVUDivExpr operand types don't match!"); |
--- |
| 3415 |
|
--- |
3415 |
|
--- |
| 3416 |
FoldingSetNodeID ID; |
0 |
3416 |
FoldingSetNodeID ID; |
0 |
| 3417 |
ID.AddInteger(scUDivExpr); |
0 |
3417 |
ID.AddInteger(scUDivExpr); |
0 |
| 3418 |
ID.AddPointer(LHS); |
0 |
3418 |
ID.AddPointer(LHS); |
0 |
| 3419 |
ID.AddPointer(RHS); |
0 |
3419 |
ID.AddPointer(RHS); |
0 |
| 3420 |
void *IP = nullptr; |
0 |
3420 |
void *IP = nullptr; |
0 |
| 3421 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) |
0 |
3421 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) |
0 |
| 3422 |
return S; |
0 |
3422 |
return S; |
0 |
| 3423 |
|
--- |
3423 |
|
--- |
| 3424 |
// 0 udiv Y == 0 |
--- |
3424 |
// 0 udiv Y == 0 |
--- |
| 3425 |
if (const SCEVConstant *LHSC = dyn_cast(LHS)) |
0 |
3425 |
if (const SCEVConstant *LHSC = dyn_cast(LHS)) |
0 |
| 3426 |
if (LHSC->getValue()->isZero()) |
0 |
3426 |
if (LHSC->getValue()->isZero()) |
0 |
| 3427 |
return LHS; |
0 |
3427 |
return LHS; |
0 |
| 3428 |
|
--- |
3428 |
|
--- |
| 3429 |
if (const SCEVConstant *RHSC = dyn_cast(RHS)) { |
0 |
3429 |
if (const SCEVConstant *RHSC = dyn_cast(RHS)) { |
0 |
| 3430 |
if (RHSC->getValue()->isOne()) |
0 |
3430 |
if (RHSC->getValue()->isOne()) |
0 |
| 3431 |
return LHS; // X udiv 1 --> x |
0 |
3431 |
return LHS; // X udiv 1 --> x |
0 |
| 3432 |
// If the denominator is zero, the result of the udiv is undefined. Don't |
--- |
3432 |
// If the denominator is zero, the result of the udiv is undefined. Don't |
--- |
| 3433 |
// try to analyze it, because the resolution chosen here may differ from |
--- |
3433 |
// try to analyze it, because the resolution chosen here may differ from |
--- |
| 3434 |
// the resolution chosen in other parts of the compiler. |
--- |
3434 |
// the resolution chosen in other parts of the compiler. |
--- |
| 3435 |
if (!RHSC->getValue()->isZero()) { |
0 |
3435 |
if (!RHSC->getValue()->isZero()) { |
0 |
| 3436 |
// Determine if the division can be folded into the operands of |
--- |
3436 |
// Determine if the division can be folded into the operands of |
--- |
| 3437 |
// its operands. |
--- |
3437 |
// its operands. |
--- |
| 3438 |
// TODO: Generalize this to non-constants by using known-bits information. |
--- |
3438 |
// TODO: Generalize this to non-constants by using known-bits information. |
--- |
| 3439 |
Type *Ty = LHS->getType(); |
0 |
3439 |
Type *Ty = LHS->getType(); |
0 |
| 3440 |
unsigned LZ = RHSC->getAPInt().countl_zero(); |
0 |
3440 |
unsigned LZ = RHSC->getAPInt().countl_zero(); |
0 |
| 3441 |
unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1; |
0 |
3441 |
unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1; |
0 |
| 3442 |
// For non-power-of-two values, effectively round the value up to the |
--- |
3442 |
// For non-power-of-two values, effectively round the value up to the |
--- |
| 3443 |
// nearest power of two. |
--- |
3443 |
// nearest power of two. |
--- |
| 3444 |
if (!RHSC->getAPInt().isPowerOf2()) |
0 |
3444 |
if (!RHSC->getAPInt().isPowerOf2()) |
0 |
| 3445 |
++MaxShiftAmt; |
0 |
3445 |
++MaxShiftAmt; |
0 |
| 3446 |
IntegerType *ExtTy = |
--- |
3446 |
IntegerType *ExtTy = |
--- |
| 3447 |
IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt); |
0 |
3447 |
IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt); |
0 |
| 3448 |
if (const SCEVAddRecExpr *AR = dyn_cast(LHS)) |
0 |
3448 |
if (const SCEVAddRecExpr *AR = dyn_cast(LHS)) |
0 |
| 3449 |
if (const SCEVConstant *Step = |
0 |
3449 |
if (const SCEVConstant *Step = |
0 |
| 3450 |
dyn_cast(AR->getStepRecurrence(*this))) { |
0 |
3450 |
dyn_cast(AR->getStepRecurrence(*this))) { |
0 |
| 3451 |
// {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. |
--- |
3451 |
// {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. |
--- |
| 3452 |
const APInt &StepInt = Step->getAPInt(); |
0 |
3452 |
const APInt &StepInt = Step->getAPInt(); |
0 |
| 3453 |
const APInt &DivInt = RHSC->getAPInt(); |
0 |
3453 |
const APInt &DivInt = RHSC->getAPInt(); |
0 |
| 3454 |
if (!StepInt.urem(DivInt) && |
0 |
3454 |
if (!StepInt.urem(DivInt) && |
0 |
| 3455 |
getZeroExtendExpr(AR, ExtTy) == |
0 |
3455 |
getZeroExtendExpr(AR, ExtTy) == |
0 |
| 3456 |
getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
0 |
3456 |
getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
0 |
| 3457 |
getZeroExtendExpr(Step, ExtTy), |
--- |
3457 |
getZeroExtendExpr(Step, ExtTy), |
--- |
| 3458 |
AR->getLoop(), SCEV::FlagAnyWrap)) { |
--- |
3458 |
AR->getLoop(), SCEV::FlagAnyWrap)) { |
--- |
| 3459 |
SmallVector Operands; |
0 |
3459 |
SmallVector Operands; |
0 |
| 3460 |
for (const SCEV *Op : AR->operands()) |
0 |
3460 |
for (const SCEV *Op : AR->operands()) |
0 |
| 3461 |
Operands.push_back(getUDivExpr(Op, RHS)); |
0 |
3461 |
Operands.push_back(getUDivExpr(Op, RHS)); |
0 |
| 3462 |
return getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagNW); |
0 |
3462 |
return getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagNW); |
0 |
| 3463 |
} |
0 |
3463 |
} |
0 |
| 3464 |
/// Get a canonical UDivExpr for a recurrence. |
--- |
3464 |
/// Get a canonical UDivExpr for a recurrence. |
--- |
| 3465 |
/// {X,+,N}/C => {Y,+,N}/C where Y=X-(X%N). Safe when C%N=0. |
--- |
3465 |
/// {X,+,N}/C => {Y,+,N}/C where Y=X-(X%N). Safe when C%N=0. |
--- |
| 3466 |
// We can currently only fold X%N if X is constant. |
--- |
3466 |
// We can currently only fold X%N if X is constant. |
--- |
| 3467 |
const SCEVConstant *StartC = dyn_cast(AR->getStart()); |
0 |
3467 |
const SCEVConstant *StartC = dyn_cast(AR->getStart()); |
0 |
| 3468 |
if (StartC && !DivInt.urem(StepInt) && |
0 |
3468 |
if (StartC && !DivInt.urem(StepInt) && |
0 |
| 3469 |
getZeroExtendExpr(AR, ExtTy) == |
0 |
3469 |
getZeroExtendExpr(AR, ExtTy) == |
0 |
| 3470 |
getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
0 |
3470 |
getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
0 |
| 3471 |
getZeroExtendExpr(Step, ExtTy), |
--- |
3471 |
getZeroExtendExpr(Step, ExtTy), |
--- |
| 3472 |
AR->getLoop(), SCEV::FlagAnyWrap)) { |
--- |
3472 |
AR->getLoop(), SCEV::FlagAnyWrap)) { |
--- |
| 3473 |
const APInt &StartInt = StartC->getAPInt(); |
0 |
3473 |
const APInt &StartInt = StartC->getAPInt(); |
0 |
| 3474 |
const APInt &StartRem = StartInt.urem(StepInt); |
0 |
3474 |
const APInt &StartRem = StartInt.urem(StepInt); |
0 |
| 3475 |
if (StartRem != 0) { |
0 |
3475 |
if (StartRem != 0) { |
0 |
| 3476 |
const SCEV *NewLHS = |
--- |
3476 |
const SCEV *NewLHS = |
--- |
| 3477 |
getAddRecExpr(getConstant(StartInt - StartRem), Step, |
0 |
3477 |
getAddRecExpr(getConstant(StartInt - StartRem), Step, |
0 |
| 3478 |
AR->getLoop(), SCEV::FlagNW); |
--- |
3478 |
AR->getLoop(), SCEV::FlagNW); |
--- |
| 3479 |
if (LHS != NewLHS) { |
0 |
3479 |
if (LHS != NewLHS) { |
0 |
| 3480 |
LHS = NewLHS; |
0 |
3480 |
LHS = NewLHS; |
0 |
| 3481 |
|
--- |
3481 |
|
--- |
| 3482 |
// Reset the ID to include the new LHS, and check if it is |
--- |
3482 |
// Reset the ID to include the new LHS, and check if it is |
--- |
| 3483 |
// already cached. |
--- |
3483 |
// already cached. |
--- |
| 3484 |
ID.clear(); |
0 |
3484 |
ID.clear(); |
0 |
| 3485 |
ID.AddInteger(scUDivExpr); |
0 |
3485 |
ID.AddInteger(scUDivExpr); |
0 |
| 3486 |
ID.AddPointer(LHS); |
0 |
3486 |
ID.AddPointer(LHS); |
0 |
| 3487 |
ID.AddPointer(RHS); |
0 |
3487 |
ID.AddPointer(RHS); |
0 |
| 3488 |
IP = nullptr; |
0 |
3488 |
IP = nullptr; |
0 |
| 3489 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) |
0 |
3489 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) |
0 |
| 3490 |
return S; |
0 |
3490 |
return S; |
0 |
| 3491 |
} |
--- |
3491 |
} |
--- |
| 3492 |
} |
--- |
3492 |
} |
--- |
| 3493 |
} |
0 |
3493 |
} |
0 |
| 3494 |
} |
--- |
3494 |
} |
--- |
| 3495 |
// (A*B)/C --> A*(B/C) if safe and B/C can be folded. |
--- |
3495 |
// (A*B)/C --> A*(B/C) if safe and B/C can be folded. |
--- |
| 3496 |
if (const SCEVMulExpr *M = dyn_cast(LHS)) { |
0 |
3496 |
if (const SCEVMulExpr *M = dyn_cast(LHS)) { |
0 |
| 3497 |
SmallVector Operands; |
0 |
3497 |
SmallVector Operands; |
0 |
| 3498 |
for (const SCEV *Op : M->operands()) |
0 |
3498 |
for (const SCEV *Op : M->operands()) |
0 |
| 3499 |
Operands.push_back(getZeroExtendExpr(Op, ExtTy)); |
0 |
3499 |
Operands.push_back(getZeroExtendExpr(Op, ExtTy)); |
0 |
| 3500 |
if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) |
0 |
3500 |
if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) |
0 |
| 3501 |
// Find an operand that's safely divisible. |
--- |
3501 |
// Find an operand that's safely divisible. |
--- |
| 3502 |
for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
0 |
3502 |
for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
0 |
| 3503 |
const SCEV *Op = M->getOperand(i); |
0 |
3503 |
const SCEV *Op = M->getOperand(i); |
0 |
| 3504 |
const SCEV *Div = getUDivExpr(Op, RHSC); |
0 |
3504 |
const SCEV *Div = getUDivExpr(Op, RHSC); |
0 |
| 3505 |
if (!isa(Div) && getMulExpr(Div, RHSC) == Op) { |
0 |
3505 |
if (!isa(Div) && getMulExpr(Div, RHSC) == Op) { |
0 |
| 3506 |
Operands = SmallVector(M->operands()); |
0 |
3506 |
Operands = SmallVector(M->operands()); |
0 |
| 3507 |
Operands[i] = Div; |
0 |
3507 |
Operands[i] = Div; |
0 |
| 3508 |
return getMulExpr(Operands); |
0 |
3508 |
return getMulExpr(Operands); |
0 |
| 3509 |
} |
--- |
3509 |
} |
--- |
| 3510 |
} |
--- |
3510 |
} |
--- |
| 3511 |
} |
0 |
3511 |
} |
0 |
| 3512 |
|
--- |
3512 |
|
--- |
| 3513 |
// (A/B)/C --> A/(B*C) if safe and B*C can be folded. |
--- |
3513 |
// (A/B)/C --> A/(B*C) if safe and B*C can be folded. |
--- |
| 3514 |
if (const SCEVUDivExpr *OtherDiv = dyn_cast(LHS)) { |
0 |
3514 |
if (const SCEVUDivExpr *OtherDiv = dyn_cast(LHS)) { |
0 |
| 3515 |
if (auto *DivisorConstant = |
0 |
3515 |
if (auto *DivisorConstant = |
0 |
| 3516 |
dyn_cast(OtherDiv->getRHS())) { |
0 |
3516 |
dyn_cast(OtherDiv->getRHS())) { |
0 |
| 3517 |
bool Overflow = false; |
0 |
3517 |
bool Overflow = false; |
0 |
| 3518 |
APInt NewRHS = |
--- |
3518 |
APInt NewRHS = |
--- |
| 3519 |
DivisorConstant->getAPInt().umul_ov(RHSC->getAPInt(), Overflow); |
0 |
3519 |
DivisorConstant->getAPInt().umul_ov(RHSC->getAPInt(), Overflow); |
0 |
| 3520 |
if (Overflow) { |
0 |
3520 |
if (Overflow) { |
0 |
| 3521 |
return getConstant(RHSC->getType(), 0, false); |
0 |
3521 |
return getConstant(RHSC->getType(), 0, false); |
0 |
| 3522 |
} |
--- |
3522 |
} |
--- |
| 3523 |
return getUDivExpr(OtherDiv->getLHS(), getConstant(NewRHS)); |
0 |
3523 |
return getUDivExpr(OtherDiv->getLHS(), getConstant(NewRHS)); |
0 |
| 3524 |
} |
0 |
3524 |
} |
0 |
| 3525 |
} |
--- |
3525 |
} |
--- |
| 3526 |
|
--- |
3526 |
|
--- |
| 3527 |
// (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. |
--- |
3527 |
// (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. |
--- |
| 3528 |
if (const SCEVAddExpr *A = dyn_cast(LHS)) { |
0 |
3528 |
if (const SCEVAddExpr *A = dyn_cast(LHS)) { |
0 |
| 3529 |
SmallVector Operands; |
0 |
3529 |
SmallVector Operands; |
0 |
| 3530 |
for (const SCEV *Op : A->operands()) |
0 |
3530 |
for (const SCEV *Op : A->operands()) |
0 |
| 3531 |
Operands.push_back(getZeroExtendExpr(Op, ExtTy)); |
0 |
3531 |
Operands.push_back(getZeroExtendExpr(Op, ExtTy)); |
0 |
| 3532 |
if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { |
0 |
3532 |
if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { |
0 |
| 3533 |
Operands.clear(); |
0 |
3533 |
Operands.clear(); |
0 |
| 3534 |
for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
0 |
3534 |
for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
0 |
| 3535 |
const SCEV *Op = getUDivExpr(A->getOperand(i), RHS); |
0 |
3535 |
const SCEV *Op = getUDivExpr(A->getOperand(i), RHS); |
0 |
| 3536 |
if (isa(Op) || |
0 |
3536 |
if (isa(Op) || |
0 |
| 3537 |
getMulExpr(Op, RHS) != A->getOperand(i)) |
0 |
3537 |
getMulExpr(Op, RHS) != A->getOperand(i)) |
0 |
| 3538 |
break; |
0 |
3538 |
break; |
0 |
| 3539 |
Operands.push_back(Op); |
0 |
3539 |
Operands.push_back(Op); |
0 |
| 3540 |
} |
--- |
3540 |
} |
--- |
| 3541 |
if (Operands.size() == A->getNumOperands()) |
0 |
3541 |
if (Operands.size() == A->getNumOperands()) |
0 |
| 3542 |
return getAddExpr(Operands); |
0 |
3542 |
return getAddExpr(Operands); |
0 |
| 3543 |
} |
--- |
3543 |
} |
--- |
| 3544 |
} |
0 |
3544 |
} |
0 |
| 3545 |
|
--- |
3545 |
|
--- |
| 3546 |
// Fold if both operands are constant. |
--- |
3546 |
// Fold if both operands are constant. |
--- |
| 3547 |
if (const SCEVConstant *LHSC = dyn_cast(LHS)) |
0 |
3547 |
if (const SCEVConstant *LHSC = dyn_cast(LHS)) |
0 |
| 3548 |
return getConstant(LHSC->getAPInt().udiv(RHSC->getAPInt())); |
0 |
3548 |
return getConstant(LHSC->getAPInt().udiv(RHSC->getAPInt())); |
0 |
| 3549 |
} |
--- |
3549 |
} |
--- |
| 3550 |
} |
--- |
3550 |
} |
--- |
| 3551 |
|
--- |
3551 |
|
--- |
| 3552 |
// The Insertion Point (IP) might be invalid by now (due to UniqueSCEVs |
--- |
3552 |
// The Insertion Point (IP) might be invalid by now (due to UniqueSCEVs |
--- |
| 3553 |
// changes). Make sure we get a new one. |
--- |
3553 |
// changes). Make sure we get a new one. |
--- |
| 3554 |
IP = nullptr; |
0 |
3554 |
IP = nullptr; |
0 |
| 3555 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
0 |
3555 |
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
0 |
| 3556 |
SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator), |
0 |
3556 |
SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator), |
0 |
| 3557 |
LHS, RHS); |
0 |
3557 |
LHS, RHS); |
0 |
| 3558 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
3558 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 3559 |
registerUser(S, {LHS, RHS}); |
0 |
3559 |
registerUser(S, {LHS, RHS}); |
0 |
| 3560 |
return S; |
0 |
3560 |
return S; |
0 |
| 3561 |
} |
0 |
3561 |
} |
0 |
| 3562 |
|
--- |
3562 |
|
--- |
| 3563 |
APInt gcd(const SCEVConstant *C1, const SCEVConstant *C2) { |
0 |
3563 |
APInt gcd(const SCEVConstant *C1, const SCEVConstant *C2) { |
0 |
| 3564 |
APInt A = C1->getAPInt().abs(); |
0 |
3564 |
APInt A = C1->getAPInt().abs(); |
0 |
| 3565 |
APInt B = C2->getAPInt().abs(); |
0 |
3565 |
APInt B = C2->getAPInt().abs(); |
0 |
| 3566 |
uint32_t ABW = A.getBitWidth(); |
0 |
3566 |
uint32_t ABW = A.getBitWidth(); |
0 |
| 3567 |
uint32_t BBW = B.getBitWidth(); |
0 |
3567 |
uint32_t BBW = B.getBitWidth(); |
0 |
| 3568 |
|
--- |
3568 |
|
--- |
| 3569 |
if (ABW > BBW) |
0 |
3569 |
if (ABW > BBW) |
0 |
| 3570 |
B = B.zext(ABW); |
0 |
3570 |
B = B.zext(ABW); |
0 |
| 3571 |
else if (ABW < BBW) |
0 |
3571 |
else if (ABW < BBW) |
0 |
| 3572 |
A = A.zext(BBW); |
0 |
3572 |
A = A.zext(BBW); |
0 |
| 3573 |
|
--- |
3573 |
|
--- |
| 3574 |
return APIntOps::GreatestCommonDivisor(std::move(A), std::move(B)); |
0 |
3574 |
return APIntOps::GreatestCommonDivisor(std::move(A), std::move(B)); |
0 |
| 3575 |
} |
0 |
3575 |
} |
0 |
| 3576 |
|
--- |
3576 |
|
--- |
| 3577 |
/// Get a canonical unsigned division expression, or something simpler if |
--- |
3577 |
/// Get a canonical unsigned division expression, or something simpler if |
--- |
| 3578 |
/// possible. There is no representation for an exact udiv in SCEV IR, but we |
--- |
3578 |
/// possible. There is no representation for an exact udiv in SCEV IR, but we |
--- |
| 3579 |
/// can attempt to remove factors from the LHS and RHS. We can't do this when |
--- |
3579 |
/// can attempt to remove factors from the LHS and RHS. We can't do this when |
--- |
| 3580 |
/// it's not exact because the udiv may be clearing bits. |
--- |
3580 |
/// it's not exact because the udiv may be clearing bits. |
--- |
| 3581 |
const SCEV *ScalarEvolution::getUDivExactExpr(const SCEV *LHS, |
0 |
3581 |
const SCEV *ScalarEvolution::getUDivExactExpr(const SCEV *LHS, |
0 |
| 3582 |
const SCEV *RHS) { |
--- |
3582 |
const SCEV *RHS) { |
--- |
| 3583 |
// TODO: we could try to find factors in all sorts of things, but for now we |
--- |
3583 |
// TODO: we could try to find factors in all sorts of things, but for now we |
--- |
| 3584 |
// just deal with u/exact (multiply, constant). See SCEVDivision towards the |
--- |
3584 |
// just deal with u/exact (multiply, constant). See SCEVDivision towards the |
--- |
| 3585 |
// end of this file for inspiration. |
--- |
3585 |
// end of this file for inspiration. |
--- |
| 3586 |
|
--- |
3586 |
|
--- |
| 3587 |
const SCEVMulExpr *Mul = dyn_cast(LHS); |
0 |
3587 |
const SCEVMulExpr *Mul = dyn_cast(LHS); |
0 |
| 3588 |
if (!Mul || !Mul->hasNoUnsignedWrap()) |
0 |
3588 |
if (!Mul || !Mul->hasNoUnsignedWrap()) |
0 |
| 3589 |
return getUDivExpr(LHS, RHS); |
0 |
3589 |
return getUDivExpr(LHS, RHS); |
0 |
| 3590 |
|
--- |
3590 |
|
--- |
| 3591 |
if (const SCEVConstant *RHSCst = dyn_cast(RHS)) { |
0 |
3591 |
if (const SCEVConstant *RHSCst = dyn_cast(RHS)) { |
0 |
| 3592 |
// If the mulexpr multiplies by a constant, then that constant must be the |
--- |
3592 |
// If the mulexpr multiplies by a constant, then that constant must be the |
--- |
| 3593 |
// first element of the mulexpr. |
--- |
3593 |
// first element of the mulexpr. |
--- |
| 3594 |
if (const auto *LHSCst = dyn_cast(Mul->getOperand(0))) { |
0 |
3594 |
if (const auto *LHSCst = dyn_cast(Mul->getOperand(0))) { |
0 |
| 3595 |
if (LHSCst == RHSCst) { |
0 |
3595 |
if (LHSCst == RHSCst) { |
0 |
| 3596 |
SmallVector Operands(drop_begin(Mul->operands())); |
0 |
3596 |
SmallVector Operands(drop_begin(Mul->operands())); |
0 |
| 3597 |
return getMulExpr(Operands); |
0 |
3597 |
return getMulExpr(Operands); |
0 |
| 3598 |
} |
0 |
3598 |
} |
0 |
| 3599 |
|
--- |
3599 |
|
--- |
| 3600 |
// We can't just assume that LHSCst divides RHSCst cleanly, it could be |
--- |
3600 |
// We can't just assume that LHSCst divides RHSCst cleanly, it could be |
--- |
| 3601 |
// that there's a factor provided by one of the other terms. We need to |
--- |
3601 |
// that there's a factor provided by one of the other terms. We need to |
--- |
| 3602 |
// check. |
--- |
3602 |
// check. |
--- |
| 3603 |
APInt Factor = gcd(LHSCst, RHSCst); |
0 |
3603 |
APInt Factor = gcd(LHSCst, RHSCst); |
0 |
| 3604 |
if (!Factor.isIntN(1)) { |
0 |
3604 |
if (!Factor.isIntN(1)) { |
0 |
| 3605 |
LHSCst = |
--- |
3605 |
LHSCst = |
--- |
| 3606 |
cast(getConstant(LHSCst->getAPInt().udiv(Factor))); |
0 |
3606 |
cast(getConstant(LHSCst->getAPInt().udiv(Factor))); |
0 |
| 3607 |
RHSCst = |
--- |
3607 |
RHSCst = |
--- |
| 3608 |
cast(getConstant(RHSCst->getAPInt().udiv(Factor))); |
0 |
3608 |
cast(getConstant(RHSCst->getAPInt().udiv(Factor))); |
0 |
| 3609 |
SmallVector Operands; |
0 |
3609 |
SmallVector Operands; |
0 |
| 3610 |
Operands.push_back(LHSCst); |
0 |
3610 |
Operands.push_back(LHSCst); |
0 |
| 3611 |
append_range(Operands, Mul->operands().drop_front()); |
0 |
3611 |
append_range(Operands, Mul->operands().drop_front()); |
0 |
| 3612 |
LHS = getMulExpr(Operands); |
0 |
3612 |
LHS = getMulExpr(Operands); |
0 |
| 3613 |
RHS = RHSCst; |
0 |
3613 |
RHS = RHSCst; |
0 |
| 3614 |
Mul = dyn_cast(LHS); |
0 |
3614 |
Mul = dyn_cast(LHS); |
0 |
| 3615 |
if (!Mul) |
0 |
3615 |
if (!Mul) |
0 |
| 3616 |
return getUDivExactExpr(LHS, RHS); |
0 |
3616 |
return getUDivExactExpr(LHS, RHS); |
0 |
| 3617 |
} |
0 |
3617 |
} |
0 |
| 3618 |
} |
0 |
3618 |
} |
0 |
| 3619 |
} |
--- |
3619 |
} |
--- |
| 3620 |
|
--- |
3620 |
|
--- |
| 3621 |
for (int i = 0, e = Mul->getNumOperands(); i != e; ++i) { |
0 |
3621 |
for (int i = 0, e = Mul->getNumOperands(); i != e; ++i) { |
0 |
| 3622 |
if (Mul->getOperand(i) == RHS) { |
0 |
3622 |
if (Mul->getOperand(i) == RHS) { |
0 |
| 3623 |
SmallVector Operands; |
0 |
3623 |
SmallVector Operands; |
0 |
| 3624 |
append_range(Operands, Mul->operands().take_front(i)); |
0 |
3624 |
append_range(Operands, Mul->operands().take_front(i)); |
0 |
| 3625 |
append_range(Operands, Mul->operands().drop_front(i + 1)); |
0 |
3625 |
append_range(Operands, Mul->operands().drop_front(i + 1)); |
0 |
| 3626 |
return getMulExpr(Operands); |
0 |
3626 |
return getMulExpr(Operands); |
0 |
| 3627 |
} |
0 |
3627 |
} |
0 |
| 3628 |
} |
--- |
3628 |
} |
--- |
| 3629 |
|
--- |
3629 |
|
--- |
| 3630 |
return getUDivExpr(LHS, RHS); |
0 |
3630 |
return getUDivExpr(LHS, RHS); |
0 |
| 3631 |
} |
--- |
3631 |
} |
--- |
| 3632 |
|
--- |
3632 |
|
--- |
| 3633 |
/// Get an add recurrence expression for the specified loop. Simplify the |
--- |
3633 |
/// Get an add recurrence expression for the specified loop. Simplify the |
--- |
| 3634 |
/// expression as much as possible. |
--- |
3634 |
/// expression as much as possible. |
--- |
| 3635 |
const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, const SCEV *Step, |
0 |
3635 |
const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, const SCEV *Step, |
0 |
| 3636 |
const Loop *L, |
--- |
3636 |
const Loop *L, |
--- |
| 3637 |
SCEV::NoWrapFlags Flags) { |
--- |
3637 |
SCEV::NoWrapFlags Flags) { |
--- |
| 3638 |
SmallVector Operands; |
0 |
3638 |
SmallVector Operands; |
0 |
| 3639 |
Operands.push_back(Start); |
0 |
3639 |
Operands.push_back(Start); |
0 |
| 3640 |
if (const SCEVAddRecExpr *StepChrec = dyn_cast(Step)) |
0 |
3640 |
if (const SCEVAddRecExpr *StepChrec = dyn_cast(Step)) |
0 |
| 3641 |
if (StepChrec->getLoop() == L) { |
0 |
3641 |
if (StepChrec->getLoop() == L) { |
0 |
| 3642 |
append_range(Operands, StepChrec->operands()); |
0 |
3642 |
append_range(Operands, StepChrec->operands()); |
0 |
| 3643 |
return getAddRecExpr(Operands, L, maskFlags(Flags, SCEV::FlagNW)); |
0 |
3643 |
return getAddRecExpr(Operands, L, maskFlags(Flags, SCEV::FlagNW)); |
0 |
| 3644 |
} |
--- |
3644 |
} |
--- |
| 3645 |
|
--- |
3645 |
|
--- |
| 3646 |
Operands.push_back(Step); |
0 |
3646 |
Operands.push_back(Step); |
0 |
| 3647 |
return getAddRecExpr(Operands, L, Flags); |
0 |
3647 |
return getAddRecExpr(Operands, L, Flags); |
0 |
| 3648 |
} |
0 |
3648 |
} |
0 |
| 3649 |
|
--- |
3649 |
|
--- |
| 3650 |
/// Get an add recurrence expression for the specified loop. Simplify the |
--- |
3650 |
/// Get an add recurrence expression for the specified loop. Simplify the |
--- |
| 3651 |
/// expression as much as possible. |
--- |
3651 |
/// expression as much as possible. |
--- |
| 3652 |
const SCEV * |
--- |
3652 |
const SCEV * |
--- |
| 3653 |
ScalarEvolution::getAddRecExpr(SmallVectorImpl &Operands, |
0 |
3653 |
ScalarEvolution::getAddRecExpr(SmallVectorImpl &Operands, |
0 |
| 3654 |
const Loop *L, SCEV::NoWrapFlags Flags) { |
--- |
3654 |
const Loop *L, SCEV::NoWrapFlags Flags) { |
--- |
| 3655 |
if (Operands.size() == 1) return Operands[0]; |
0 |
3655 |
if (Operands.size() == 1) return Operands[0]; |
0 |
| 3656 |
#ifndef NDEBUG |
--- |
3656 |
#ifndef NDEBUG |
--- |
| 3657 |
Type *ETy = getEffectiveSCEVType(Operands[0]->getType()); |
0 |
3657 |
Type *ETy = getEffectiveSCEVType(Operands[0]->getType()); |
0 |
| 3658 |
for (unsigned i = 1, e = Operands.size(); i != e; ++i) { |
0 |
3658 |
for (unsigned i = 1, e = Operands.size(); i != e; ++i) { |
0 |
| 3659 |
assert(getEffectiveSCEVType(Operands[i]->getType()) == ETy && |
0 |
3659 |
assert(getEffectiveSCEVType(Operands[i]->getType()) == ETy && |
0 |
| 3660 |
"SCEVAddRecExpr operand types don't match!"); |
--- |
3660 |
"SCEVAddRecExpr operand types don't match!"); |
--- |
| 3661 |
assert(!Operands[i]->getType()->isPointerTy() && "Step must be integer"); |
0 |
3661 |
assert(!Operands[i]->getType()->isPointerTy() && "Step must be integer"); |
0 |
| 3662 |
} |
--- |
3662 |
} |
--- |
| 3663 |
for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
0 |
3663 |
for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
0 |
| 3664 |
assert(isLoopInvariant(Operands[i], L) && |
0 |
3664 |
assert(isLoopInvariant(Operands[i], L) && |
0 |
| 3665 |
"SCEVAddRecExpr operand is not loop-invariant!"); |
--- |
3665 |
"SCEVAddRecExpr operand is not loop-invariant!"); |
--- |
| 3666 |
#endif |
--- |
3666 |
#endif |
--- |
| 3667 |
|
--- |
3667 |
|
--- |
| 3668 |
if (Operands.back()->isZero()) { |
0 |
3668 |
if (Operands.back()->isZero()) { |
0 |
| 3669 |
Operands.pop_back(); |
0 |
3669 |
Operands.pop_back(); |
0 |
| 3670 |
return getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); // {X,+,0} --> X |
0 |
3670 |
return getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); // {X,+,0} --> X |
0 |
| 3671 |
} |
--- |
3671 |
} |
--- |
| 3672 |
|
--- |
3672 |
|
--- |
| 3673 |
// It's tempting to want to call getConstantMaxBackedgeTakenCount count here and |
--- |
3673 |
// It's tempting to want to call getConstantMaxBackedgeTakenCount count here and |
--- |
| 3674 |
// use that information to infer NUW and NSW flags. However, computing a |
--- |
3674 |
// use that information to infer NUW and NSW flags. However, computing a |
--- |
| 3675 |
// BE count requires calling getAddRecExpr, so we may not yet have a |
--- |
3675 |
// BE count requires calling getAddRecExpr, so we may not yet have a |
--- |
| 3676 |
// meaningful BE count at this point (and if we don't, we'd be stuck |
--- |
3676 |
// meaningful BE count at this point (and if we don't, we'd be stuck |
--- |
| 3677 |
// with a SCEVCouldNotCompute as the cached BE count). |
--- |
3677 |
// with a SCEVCouldNotCompute as the cached BE count). |
--- |
| 3678 |
|
--- |
3678 |
|
--- |
| 3679 |
Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags); |
0 |
3679 |
Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags); |
0 |
| 3680 |
|
--- |
3680 |
|
--- |
| 3681 |
// Canonicalize nested AddRecs in by nesting them in order of loop depth. |
--- |
3681 |
// Canonicalize nested AddRecs in by nesting them in order of loop depth. |
--- |
| 3682 |
if (const SCEVAddRecExpr *NestedAR = dyn_cast(Operands[0])) { |
0 |
3682 |
if (const SCEVAddRecExpr *NestedAR = dyn_cast(Operands[0])) { |
0 |
| 3683 |
const Loop *NestedLoop = NestedAR->getLoop(); |
0 |
3683 |
const Loop *NestedLoop = NestedAR->getLoop(); |
0 |
| 3684 |
if (L->contains(NestedLoop) |
0 |
3684 |
if (L->contains(NestedLoop) |
0 |
| 3685 |
? (L->getLoopDepth() < NestedLoop->getLoopDepth()) |
0 |
3685 |
? (L->getLoopDepth() < NestedLoop->getLoopDepth()) |
0 |
| 3686 |
: (!NestedLoop->contains(L) && |
0 |
3686 |
: (!NestedLoop->contains(L) && |
0 |
| 3687 |
DT.dominates(L->getHeader(), NestedLoop->getHeader()))) { |
0 |
3687 |
DT.dominates(L->getHeader(), NestedLoop->getHeader()))) { |
0 |
| 3688 |
SmallVector NestedOperands(NestedAR->operands()); |
0 |
3688 |
SmallVector NestedOperands(NestedAR->operands()); |
0 |
| 3689 |
Operands[0] = NestedAR->getStart(); |
0 |
3689 |
Operands[0] = NestedAR->getStart(); |
0 |
| 3690 |
// AddRecs require their operands be loop-invariant with respect to their |
--- |
3690 |
// AddRecs require their operands be loop-invariant with respect to their |
--- |
| 3691 |
// loops. Don't perform this transformation if it would break this |
--- |
3691 |
// loops. Don't perform this transformation if it would break this |
--- |
| 3692 |
// requirement. |
--- |
3692 |
// requirement. |
--- |
| 3693 |
bool AllInvariant = all_of( |
0 |
3693 |
bool AllInvariant = all_of( |
0 |
| 3694 |
Operands, [&](const SCEV *Op) { return isLoopInvariant(Op, L); }); |
0 |
3694 |
Operands, [&](const SCEV *Op) { return isLoopInvariant(Op, L); }); |
0 |
| 3695 |
|
--- |
3695 |
|
--- |
| 3696 |
if (AllInvariant) { |
0 |
3696 |
if (AllInvariant) { |
0 |
| 3697 |
// Create a recurrence for the outer loop with the same step size. |
--- |
3697 |
// Create a recurrence for the outer loop with the same step size. |
--- |
| 3698 |
// |
--- |
3698 |
// |
--- |
| 3699 |
// The outer recurrence keeps its NW flag but only keeps NUW/NSW if the |
--- |
3699 |
// The outer recurrence keeps its NW flag but only keeps NUW/NSW if the |
--- |
| 3700 |
// inner recurrence has the same property. |
--- |
3700 |
// inner recurrence has the same property. |
--- |
| 3701 |
SCEV::NoWrapFlags OuterFlags = |
--- |
3701 |
SCEV::NoWrapFlags OuterFlags = |
--- |
| 3702 |
maskFlags(Flags, SCEV::FlagNW | NestedAR->getNoWrapFlags()); |
0 |
3702 |
maskFlags(Flags, SCEV::FlagNW | NestedAR->getNoWrapFlags()); |
0 |
| 3703 |
|
--- |
3703 |
|
--- |
| 3704 |
NestedOperands[0] = getAddRecExpr(Operands, L, OuterFlags); |
0 |
3704 |
NestedOperands[0] = getAddRecExpr(Operands, L, OuterFlags); |
0 |
| 3705 |
AllInvariant = all_of(NestedOperands, [&](const SCEV *Op) { |
0 |
3705 |
AllInvariant = all_of(NestedOperands, [&](const SCEV *Op) { |
0 |
| 3706 |
return isLoopInvariant(Op, NestedLoop); |
0 |
3706 |
return isLoopInvariant(Op, NestedLoop); |
0 |
| 3707 |
}); |
--- |
3707 |
}); |
--- |
| 3708 |
|
--- |
3708 |
|
--- |
| 3709 |
if (AllInvariant) { |
0 |
3709 |
if (AllInvariant) { |
0 |
| 3710 |
// Ok, both add recurrences are valid after the transformation. |
--- |
3710 |
// Ok, both add recurrences are valid after the transformation. |
--- |
| 3711 |
// |
--- |
3711 |
// |
--- |
| 3712 |
// The inner recurrence keeps its NW flag but only keeps NUW/NSW if |
--- |
3712 |
// The inner recurrence keeps its NW flag but only keeps NUW/NSW if |
--- |
| 3713 |
// the outer recurrence has the same property. |
--- |
3713 |
// the outer recurrence has the same property. |
--- |
| 3714 |
SCEV::NoWrapFlags InnerFlags = |
--- |
3714 |
SCEV::NoWrapFlags InnerFlags = |
--- |
| 3715 |
maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | Flags); |
0 |
3715 |
maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | Flags); |
0 |
| 3716 |
return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags); |
0 |
3716 |
return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags); |
0 |
| 3717 |
} |
--- |
3717 |
} |
--- |
| 3718 |
} |
--- |
3718 |
} |
--- |
| 3719 |
// Reset Operands to its original state. |
--- |
3719 |
// Reset Operands to its original state. |
--- |
| 3720 |
Operands[0] = NestedAR; |
0 |
3720 |
Operands[0] = NestedAR; |
0 |
| 3721 |
} |
0 |
3721 |
} |
0 |
| 3722 |
} |
--- |
3722 |
} |
--- |
| 3723 |
|
--- |
3723 |
|
--- |
| 3724 |
// Okay, it looks like we really DO need an addrec expr. Check to see if we |
--- |
3724 |
// Okay, it looks like we really DO need an addrec expr. Check to see if we |
--- |
| 3725 |
// already have one, otherwise create a new one. |
--- |
3725 |
// already have one, otherwise create a new one. |
--- |
| 3726 |
return getOrCreateAddRecExpr(Operands, L, Flags); |
0 |
3726 |
return getOrCreateAddRecExpr(Operands, L, Flags); |
0 |
| 3727 |
} |
--- |
3727 |
} |
--- |
| 3728 |
|
--- |
3728 |
|
--- |
| 3729 |
const SCEV * |
--- |
3729 |
const SCEV * |
--- |
| 3730 |
ScalarEvolution::getGEPExpr(GEPOperator *GEP, |
0 |
3730 |
ScalarEvolution::getGEPExpr(GEPOperator *GEP, |
0 |
| 3731 |
const SmallVectorImpl &IndexExprs) { |
--- |
3731 |
const SmallVectorImpl &IndexExprs) { |
--- |
| 3732 |
const SCEV *BaseExpr = getSCEV(GEP->getPointerOperand()); |
0 |
3732 |
const SCEV *BaseExpr = getSCEV(GEP->getPointerOperand()); |
0 |
| 3733 |
// getSCEV(Base)->getType() has the same address space as Base->getType() |
--- |
3733 |
// getSCEV(Base)->getType() has the same address space as Base->getType() |
--- |
| 3734 |
// because SCEV::getType() preserves the address space. |
--- |
3734 |
// because SCEV::getType() preserves the address space. |
--- |
| 3735 |
Type *IntIdxTy = getEffectiveSCEVType(BaseExpr->getType()); |
0 |
3735 |
Type *IntIdxTy = getEffectiveSCEVType(BaseExpr->getType()); |
0 |
| 3736 |
const bool AssumeInBoundsFlags = [&]() { |
0 |
3736 |
const bool AssumeInBoundsFlags = [&]() { |
0 |
| 3737 |
if (!GEP->isInBounds()) |
0 |
3737 |
if (!GEP->isInBounds()) |
0 |
| 3738 |
return false; |
0 |
3738 |
return false; |
0 |
| 3739 |
|
--- |
3739 |
|
--- |
| 3740 |
// We'd like to propagate flags from the IR to the corresponding SCEV nodes, |
--- |
3740 |
// We'd like to propagate flags from the IR to the corresponding SCEV nodes, |
--- |
| 3741 |
// but to do that, we have to ensure that said flag is valid in the entire |
--- |
3741 |
// but to do that, we have to ensure that said flag is valid in the entire |
--- |
| 3742 |
// defined scope of the SCEV. |
--- |
3742 |
// defined scope of the SCEV. |
--- |
| 3743 |
auto *GEPI = dyn_cast(GEP); |
0 |
3743 |
auto *GEPI = dyn_cast(GEP); |
0 |
| 3744 |
// TODO: non-instructions have global scope. We might be able to prove |
--- |
3744 |
// TODO: non-instructions have global scope. We might be able to prove |
--- |
| 3745 |
// some global scope cases |
--- |
3745 |
// some global scope cases |
--- |
| 3746 |
return GEPI && isSCEVExprNeverPoison(GEPI); |
0 |
3746 |
return GEPI && isSCEVExprNeverPoison(GEPI); |
0 |
| 3747 |
}(); |
0 |
3747 |
}(); |
0 |
| 3748 |
|
--- |
3748 |
|
--- |
| 3749 |
SCEV::NoWrapFlags OffsetWrap = |
0 |
3749 |
SCEV::NoWrapFlags OffsetWrap = |
0 |
| 3750 |
AssumeInBoundsFlags ? SCEV::FlagNSW : SCEV::FlagAnyWrap; |
0 |
3750 |
AssumeInBoundsFlags ? SCEV::FlagNSW : SCEV::FlagAnyWrap; |
0 |
| 3751 |
|
--- |
3751 |
|
--- |
| 3752 |
Type *CurTy = GEP->getType(); |
0 |
3752 |
Type *CurTy = GEP->getType(); |
0 |
| 3753 |
bool FirstIter = true; |
0 |
3753 |
bool FirstIter = true; |
0 |
| 3754 |
SmallVector Offsets; |
0 |
3754 |
SmallVector Offsets; |
0 |
| 3755 |
for (const SCEV *IndexExpr : IndexExprs) { |
0 |
3755 |
for (const SCEV *IndexExpr : IndexExprs) { |
0 |
| 3756 |
// Compute the (potentially symbolic) offset in bytes for this index. |
--- |
3756 |
// Compute the (potentially symbolic) offset in bytes for this index. |
--- |
| 3757 |
if (StructType *STy = dyn_cast(CurTy)) { |
0 |
3757 |
if (StructType *STy = dyn_cast(CurTy)) { |
0 |
| 3758 |
// For a struct, add the member offset. |
--- |
3758 |
// For a struct, add the member offset. |
--- |
| 3759 |
ConstantInt *Index = cast(IndexExpr)->getValue(); |
0 |
3759 |
ConstantInt *Index = cast(IndexExpr)->getValue(); |
0 |
| 3760 |
unsigned FieldNo = Index->getZExtValue(); |
0 |
3760 |
unsigned FieldNo = Index->getZExtValue(); |
0 |
| 3761 |
const SCEV *FieldOffset = getOffsetOfExpr(IntIdxTy, STy, FieldNo); |
0 |
3761 |
const SCEV *FieldOffset = getOffsetOfExpr(IntIdxTy, STy, FieldNo); |
0 |
| 3762 |
Offsets.push_back(FieldOffset); |
0 |
3762 |
Offsets.push_back(FieldOffset); |
0 |
| 3763 |
|
--- |
3763 |
|
--- |
| 3764 |
// Update CurTy to the type of the field at Index. |
--- |
3764 |
// Update CurTy to the type of the field at Index. |
--- |
| 3765 |
CurTy = STy->getTypeAtIndex(Index); |
0 |
3765 |
CurTy = STy->getTypeAtIndex(Index); |
0 |
| 3766 |
} else { |
--- |
3766 |
} else { |
--- |
| 3767 |
// Update CurTy to its element type. |
--- |
3767 |
// Update CurTy to its element type. |
--- |
| 3768 |
if (FirstIter) { |
0 |
3768 |
if (FirstIter) { |
0 |
| 3769 |
assert(isa(CurTy) && |
0 |
3769 |
assert(isa(CurTy) && |
0 |
| 3770 |
"The first index of a GEP indexes a pointer"); |
--- |
3770 |
"The first index of a GEP indexes a pointer"); |
--- |
| 3771 |
CurTy = GEP->getSourceElementType(); |
0 |
3771 |
CurTy = GEP->getSourceElementType(); |
0 |
| 3772 |
FirstIter = false; |
0 |
3772 |
FirstIter = false; |
0 |
| 3773 |
} else { |
--- |
3773 |
} else { |
--- |
| 3774 |
CurTy = GetElementPtrInst::getTypeAtIndex(CurTy, (uint64_t)0); |
0 |
3774 |
CurTy = GetElementPtrInst::getTypeAtIndex(CurTy, (uint64_t)0); |
0 |
| 3775 |
} |
--- |
3775 |
} |
--- |
| 3776 |
// For an array, add the element offset, explicitly scaled. |
--- |
3776 |
// For an array, add the element offset, explicitly scaled. |
--- |
| 3777 |
const SCEV *ElementSize = getSizeOfExpr(IntIdxTy, CurTy); |
0 |
3777 |
const SCEV *ElementSize = getSizeOfExpr(IntIdxTy, CurTy); |
0 |
| 3778 |
// Getelementptr indices are signed. |
--- |
3778 |
// Getelementptr indices are signed. |
--- |
| 3779 |
IndexExpr = getTruncateOrSignExtend(IndexExpr, IntIdxTy); |
0 |
3779 |
IndexExpr = getTruncateOrSignExtend(IndexExpr, IntIdxTy); |
0 |
| 3780 |
|
--- |
3780 |
|
--- |
| 3781 |
// Multiply the index by the element size to compute the element offset. |
--- |
3781 |
// Multiply the index by the element size to compute the element offset. |
--- |
| 3782 |
const SCEV *LocalOffset = getMulExpr(IndexExpr, ElementSize, OffsetWrap); |
0 |
3782 |
const SCEV *LocalOffset = getMulExpr(IndexExpr, ElementSize, OffsetWrap); |
0 |
| 3783 |
Offsets.push_back(LocalOffset); |
0 |
3783 |
Offsets.push_back(LocalOffset); |
0 |
| 3784 |
} |
--- |
3784 |
} |
--- |
| 3785 |
} |
--- |
3785 |
} |
--- |
| 3786 |
|
--- |
3786 |
|
--- |
| 3787 |
// Handle degenerate case of GEP without offsets. |
--- |
3787 |
// Handle degenerate case of GEP without offsets. |
--- |
| 3788 |
if (Offsets.empty()) |
0 |
3788 |
if (Offsets.empty()) |
0 |
| 3789 |
return BaseExpr; |
0 |
3789 |
return BaseExpr; |
0 |
| 3790 |
|
--- |
3790 |
|
--- |
| 3791 |
// Add the offsets together, assuming nsw if inbounds. |
--- |
3791 |
// Add the offsets together, assuming nsw if inbounds. |
--- |
| 3792 |
const SCEV *Offset = getAddExpr(Offsets, OffsetWrap); |
0 |
3792 |
const SCEV *Offset = getAddExpr(Offsets, OffsetWrap); |
0 |
| 3793 |
// Add the base address and the offset. We cannot use the nsw flag, as the |
--- |
3793 |
// Add the base address and the offset. We cannot use the nsw flag, as the |
--- |
| 3794 |
// base address is unsigned. However, if we know that the offset is |
--- |
3794 |
// base address is unsigned. However, if we know that the offset is |
--- |
| 3795 |
// non-negative, we can use nuw. |
--- |
3795 |
// non-negative, we can use nuw. |
--- |
| 3796 |
SCEV::NoWrapFlags BaseWrap = AssumeInBoundsFlags && isKnownNonNegative(Offset) |
0 |
3796 |
SCEV::NoWrapFlags BaseWrap = AssumeInBoundsFlags && isKnownNonNegative(Offset) |
0 |
| 3797 |
? SCEV::FlagNUW : SCEV::FlagAnyWrap; |
0 |
3797 |
? SCEV::FlagNUW : SCEV::FlagAnyWrap; |
0 |
| 3798 |
auto *GEPExpr = getAddExpr(BaseExpr, Offset, BaseWrap); |
0 |
3798 |
auto *GEPExpr = getAddExpr(BaseExpr, Offset, BaseWrap); |
0 |
| 3799 |
assert(BaseExpr->getType() == GEPExpr->getType() && |
0 |
3799 |
assert(BaseExpr->getType() == GEPExpr->getType() && |
0 |
| 3800 |
"GEP should not change type mid-flight."); |
--- |
3800 |
"GEP should not change type mid-flight."); |
--- |
| 3801 |
return GEPExpr; |
0 |
3801 |
return GEPExpr; |
0 |
| 3802 |
} |
0 |
3802 |
} |
0 |
| 3803 |
|
--- |
3803 |
|
--- |
| 3804 |
SCEV *ScalarEvolution::findExistingSCEVInCache(SCEVTypes SCEVType, |
0 |
3804 |
SCEV *ScalarEvolution::findExistingSCEVInCache(SCEVTypes SCEVType, |
0 |
| 3805 |
ArrayRef Ops) { |
--- |
3805 |
ArrayRef Ops) { |
--- |
| 3806 |
FoldingSetNodeID ID; |
0 |
3806 |
FoldingSetNodeID ID; |
0 |
| 3807 |
ID.AddInteger(SCEVType); |
0 |
3807 |
ID.AddInteger(SCEVType); |
0 |
| 3808 |
for (const SCEV *Op : Ops) |
0 |
3808 |
for (const SCEV *Op : Ops) |
0 |
| 3809 |
ID.AddPointer(Op); |
0 |
3809 |
ID.AddPointer(Op); |
0 |
| 3810 |
void *IP = nullptr; |
0 |
3810 |
void *IP = nullptr; |
0 |
| 3811 |
return UniqueSCEVs.FindNodeOrInsertPos(ID, IP); |
0 |
3811 |
return UniqueSCEVs.FindNodeOrInsertPos(ID, IP); |
0 |
| 3812 |
} |
0 |
3812 |
} |
0 |
| 3813 |
|
--- |
3813 |
|
--- |
| 3814 |
const SCEV *ScalarEvolution::getAbsExpr(const SCEV *Op, bool IsNSW) { |
0 |
3814 |
const SCEV *ScalarEvolution::getAbsExpr(const SCEV *Op, bool IsNSW) { |
0 |
| 3815 |
SCEV::NoWrapFlags Flags = IsNSW ? SCEV::FlagNSW : SCEV::FlagAnyWrap; |
0 |
3815 |
SCEV::NoWrapFlags Flags = IsNSW ? SCEV::FlagNSW : SCEV::FlagAnyWrap; |
0 |
| 3816 |
return getSMaxExpr(Op, getNegativeSCEV(Op, Flags)); |
0 |
3816 |
return getSMaxExpr(Op, getNegativeSCEV(Op, Flags)); |
0 |
| 3817 |
} |
--- |
3817 |
} |
--- |
| 3818 |
|
--- |
3818 |
|
--- |
| 3819 |
const SCEV *ScalarEvolution::getMinMaxExpr(SCEVTypes Kind, |
0 |
3819 |
const SCEV *ScalarEvolution::getMinMaxExpr(SCEVTypes Kind, |
0 |
| 3820 |
SmallVectorImpl &Ops) { |
--- |
3820 |
SmallVectorImpl &Ops) { |
--- |
| 3821 |
assert(SCEVMinMaxExpr::isMinMaxType(Kind) && "Not a SCEVMinMaxExpr!"); |
0 |
3821 |
assert(SCEVMinMaxExpr::isMinMaxType(Kind) && "Not a SCEVMinMaxExpr!"); |
0 |
| 3822 |
assert(!Ops.empty() && "Cannot get empty (u|s)(min|max)!"); |
0 |
3822 |
assert(!Ops.empty() && "Cannot get empty (u|s)(min|max)!"); |
0 |
| 3823 |
if (Ops.size() == 1) return Ops[0]; |
0 |
3823 |
if (Ops.size() == 1) return Ops[0]; |
0 |
| 3824 |
#ifndef NDEBUG |
--- |
3824 |
#ifndef NDEBUG |
--- |
| 3825 |
Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
0 |
3825 |
Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
0 |
| 3826 |
for (unsigned i = 1, e = Ops.size(); i != e; ++i) { |
0 |
3826 |
for (unsigned i = 1, e = Ops.size(); i != e; ++i) { |
0 |
| 3827 |
assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
0 |
3827 |
assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
0 |
| 3828 |
"Operand types don't match!"); |
--- |
3828 |
"Operand types don't match!"); |
--- |
| 3829 |
assert(Ops[0]->getType()->isPointerTy() == |
0 |
3829 |
assert(Ops[0]->getType()->isPointerTy() == |
0 |
| 3830 |
Ops[i]->getType()->isPointerTy() && |
--- |
3830 |
Ops[i]->getType()->isPointerTy() && |
--- |
| 3831 |
"min/max should be consistently pointerish"); |
--- |
3831 |
"min/max should be consistently pointerish"); |
--- |
| 3832 |
} |
--- |
3832 |
} |
--- |
| 3833 |
#endif |
--- |
3833 |
#endif |
--- |
| 3834 |
|
--- |
3834 |
|
--- |
| 3835 |
bool IsSigned = Kind == scSMaxExpr || Kind == scSMinExpr; |
0 |
3835 |
bool IsSigned = Kind == scSMaxExpr || Kind == scSMinExpr; |
0 |
| 3836 |
bool IsMax = Kind == scSMaxExpr || Kind == scUMaxExpr; |
0 |
3836 |
bool IsMax = Kind == scSMaxExpr || Kind == scUMaxExpr; |
0 |
| 3837 |
|
--- |
3837 |
|
--- |
| 3838 |
// Sort by complexity, this groups all similar expression types together. |
--- |
3838 |
// Sort by complexity, this groups all similar expression types together. |
--- |
| 3839 |
GroupByComplexity(Ops, &LI, DT); |
0 |
3839 |
GroupByComplexity(Ops, &LI, DT); |
0 |
| 3840 |
|
--- |
3840 |
|
--- |
| 3841 |
// Check if we have created the same expression before. |
--- |
3841 |
// Check if we have created the same expression before. |
--- |
| 3842 |
if (const SCEV *S = findExistingSCEVInCache(Kind, Ops)) { |
0 |
3842 |
if (const SCEV *S = findExistingSCEVInCache(Kind, Ops)) { |
0 |
| 3843 |
return S; |
0 |
3843 |
return S; |
0 |
| 3844 |
} |
--- |
3844 |
} |
--- |
| 3845 |
|
--- |
3845 |
|
--- |
| 3846 |
// If there are any constants, fold them together. |
--- |
3846 |
// If there are any constants, fold them together. |
--- |
| 3847 |
unsigned Idx = 0; |
0 |
3847 |
unsigned Idx = 0; |
0 |
| 3848 |
if (const SCEVConstant *LHSC = dyn_cast(Ops[0])) { |
0 |
3848 |
if (const SCEVConstant *LHSC = dyn_cast(Ops[0])) { |
0 |
| 3849 |
++Idx; |
0 |
3849 |
++Idx; |
0 |
| 3850 |
assert(Idx < Ops.size()); |
0 |
3850 |
assert(Idx < Ops.size()); |
0 |
| 3851 |
auto FoldOp = [&](const APInt &LHS, const APInt &RHS) { |
0 |
3851 |
auto FoldOp = [&](const APInt &LHS, const APInt &RHS) { |
0 |
| 3852 |
switch (Kind) { |
0 |
3852 |
switch (Kind) { |
0 |
| 3853 |
case scSMaxExpr: |
0 |
3853 |
case scSMaxExpr: |
0 |
| 3854 |
return APIntOps::smax(LHS, RHS); |
0 |
3854 |
return APIntOps::smax(LHS, RHS); |
0 |
| 3855 |
case scSMinExpr: |
0 |
3855 |
case scSMinExpr: |
0 |
| 3856 |
return APIntOps::smin(LHS, RHS); |
0 |
3856 |
return APIntOps::smin(LHS, RHS); |
0 |
| 3857 |
case scUMaxExpr: |
0 |
3857 |
case scUMaxExpr: |
0 |
| 3858 |
return APIntOps::umax(LHS, RHS); |
0 |
3858 |
return APIntOps::umax(LHS, RHS); |
0 |
| 3859 |
case scUMinExpr: |
0 |
3859 |
case scUMinExpr: |
0 |
| 3860 |
return APIntOps::umin(LHS, RHS); |
0 |
3860 |
return APIntOps::umin(LHS, RHS); |
0 |
| 3861 |
default: |
0 |
3861 |
default: |
0 |
| 3862 |
llvm_unreachable("Unknown SCEV min/max opcode"); |
0 |
3862 |
llvm_unreachable("Unknown SCEV min/max opcode"); |
0 |
| 3863 |
} |
--- |
3863 |
} |
--- |
| 3864 |
}; |
0 |
3864 |
}; |
0 |
| 3865 |
|
--- |
3865 |
|
--- |
| 3866 |
while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) { |
0 |
3866 |
while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) { |
0 |
| 3867 |
// We found two constants, fold them together! |
--- |
3867 |
// We found two constants, fold them together! |
--- |
| 3868 |
ConstantInt *Fold = ConstantInt::get( |
0 |
3868 |
ConstantInt *Fold = ConstantInt::get( |
0 |
| 3869 |
getContext(), FoldOp(LHSC->getAPInt(), RHSC->getAPInt())); |
0 |
3869 |
getContext(), FoldOp(LHSC->getAPInt(), RHSC->getAPInt())); |
0 |
| 3870 |
Ops[0] = getConstant(Fold); |
0 |
3870 |
Ops[0] = getConstant(Fold); |
0 |
| 3871 |
Ops.erase(Ops.begin()+1); // Erase the folded element |
0 |
3871 |
Ops.erase(Ops.begin()+1); // Erase the folded element |
0 |
| 3872 |
if (Ops.size() == 1) return Ops[0]; |
0 |
3872 |
if (Ops.size() == 1) return Ops[0]; |
0 |
| 3873 |
LHSC = cast(Ops[0]); |
0 |
3873 |
LHSC = cast(Ops[0]); |
0 |
| 3874 |
} |
0 |
3874 |
} |
0 |
| 3875 |
|
--- |
3875 |
|
--- |
| 3876 |
bool IsMinV = LHSC->getValue()->isMinValue(IsSigned); |
0 |
3876 |
bool IsMinV = LHSC->getValue()->isMinValue(IsSigned); |
0 |
| 3877 |
bool IsMaxV = LHSC->getValue()->isMaxValue(IsSigned); |
0 |
3877 |
bool IsMaxV = LHSC->getValue()->isMaxValue(IsSigned); |
0 |
| 3878 |
|
--- |
3878 |
|
--- |
| 3879 |
if (IsMax ? IsMinV : IsMaxV) { |
0 |
3879 |
if (IsMax ? IsMinV : IsMaxV) { |
0 |
| 3880 |
// If we are left with a constant minimum(/maximum)-int, strip it off. |
--- |
3880 |
// If we are left with a constant minimum(/maximum)-int, strip it off. |
--- |
| 3881 |
Ops.erase(Ops.begin()); |
0 |
3881 |
Ops.erase(Ops.begin()); |
0 |
| 3882 |
--Idx; |
0 |
3882 |
--Idx; |
0 |
| 3883 |
} else if (IsMax ? IsMaxV : IsMinV) { |
0 |
3883 |
} else if (IsMax ? IsMaxV : IsMinV) { |
0 |
| 3884 |
// If we have a max(/min) with a constant maximum(/minimum)-int, |
--- |
3884 |
// If we have a max(/min) with a constant maximum(/minimum)-int, |
--- |
| 3885 |
// it will always be the extremum. |
--- |
3885 |
// it will always be the extremum. |
--- |
| 3886 |
return LHSC; |
0 |
3886 |
return LHSC; |
0 |
| 3887 |
} |
--- |
3887 |
} |
--- |
| 3888 |
|
--- |
3888 |
|
--- |
| 3889 |
if (Ops.size() == 1) return Ops[0]; |
0 |
3889 |
if (Ops.size() == 1) return Ops[0]; |
0 |
| 3890 |
} |
--- |
3890 |
} |
--- |
| 3891 |
|
--- |
3891 |
|
--- |
| 3892 |
// Find the first operation of the same kind |
--- |
3892 |
// Find the first operation of the same kind |
--- |
| 3893 |
while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < Kind) |
0 |
3893 |
while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < Kind) |
0 |
| 3894 |
++Idx; |
0 |
3894 |
++Idx; |
0 |
| 3895 |
|
--- |
3895 |
|
--- |
| 3896 |
// Check to see if one of the operands is of the same kind. If so, expand its |
--- |
3896 |
// Check to see if one of the operands is of the same kind. If so, expand its |
--- |
| 3897 |
// operands onto our operand list, and recurse to simplify. |
--- |
3897 |
// operands onto our operand list, and recurse to simplify. |
--- |
| 3898 |
if (Idx < Ops.size()) { |
0 |
3898 |
if (Idx < Ops.size()) { |
0 |
| 3899 |
bool DeletedAny = false; |
0 |
3899 |
bool DeletedAny = false; |
0 |
| 3900 |
while (Ops[Idx]->getSCEVType() == Kind) { |
0 |
3900 |
while (Ops[Idx]->getSCEVType() == Kind) { |
0 |
| 3901 |
const SCEVMinMaxExpr *SMME = cast(Ops[Idx]); |
0 |
3901 |
const SCEVMinMaxExpr *SMME = cast(Ops[Idx]); |
0 |
| 3902 |
Ops.erase(Ops.begin()+Idx); |
0 |
3902 |
Ops.erase(Ops.begin()+Idx); |
0 |
| 3903 |
append_range(Ops, SMME->operands()); |
0 |
3903 |
append_range(Ops, SMME->operands()); |
0 |
| 3904 |
DeletedAny = true; |
0 |
3904 |
DeletedAny = true; |
0 |
| 3905 |
} |
--- |
3905 |
} |
--- |
| 3906 |
|
--- |
3906 |
|
--- |
| 3907 |
if (DeletedAny) |
0 |
3907 |
if (DeletedAny) |
0 |
| 3908 |
return getMinMaxExpr(Kind, Ops); |
0 |
3908 |
return getMinMaxExpr(Kind, Ops); |
0 |
| 3909 |
} |
--- |
3909 |
} |
--- |
| 3910 |
|
--- |
3910 |
|
--- |
| 3911 |
// Okay, check to see if the same value occurs in the operand list twice. If |
--- |
3911 |
// Okay, check to see if the same value occurs in the operand list twice. If |
--- |
| 3912 |
// so, delete one. Since we sorted the list, these values are required to |
--- |
3912 |
// so, delete one. Since we sorted the list, these values are required to |
--- |
| 3913 |
// be adjacent. |
--- |
3913 |
// be adjacent. |
--- |
| 3914 |
llvm::CmpInst::Predicate GEPred = |
0 |
3914 |
llvm::CmpInst::Predicate GEPred = |
0 |
| 3915 |
IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; |
0 |
3915 |
IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; |
0 |
| 3916 |
llvm::CmpInst::Predicate LEPred = |
0 |
3916 |
llvm::CmpInst::Predicate LEPred = |
0 |
| 3917 |
IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; |
0 |
3917 |
IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; |
0 |
| 3918 |
llvm::CmpInst::Predicate FirstPred = IsMax ? GEPred : LEPred; |
0 |
3918 |
llvm::CmpInst::Predicate FirstPred = IsMax ? GEPred : LEPred; |
0 |
| 3919 |
llvm::CmpInst::Predicate SecondPred = IsMax ? LEPred : GEPred; |
0 |
3919 |
llvm::CmpInst::Predicate SecondPred = IsMax ? LEPred : GEPred; |
0 |
| 3920 |
for (unsigned i = 0, e = Ops.size() - 1; i != e; ++i) { |
0 |
3920 |
for (unsigned i = 0, e = Ops.size() - 1; i != e; ++i) { |
0 |
| 3921 |
if (Ops[i] == Ops[i + 1] || |
0 |
3921 |
if (Ops[i] == Ops[i + 1] || |
0 |
| 3922 |
isKnownViaNonRecursiveReasoning(FirstPred, Ops[i], Ops[i + 1])) { |
0 |
3922 |
isKnownViaNonRecursiveReasoning(FirstPred, Ops[i], Ops[i + 1])) { |
0 |
| 3923 |
// X op Y op Y --> X op Y |
--- |
3923 |
// X op Y op Y --> X op Y |
--- |
| 3924 |
// X op Y --> X, if we know X, Y are ordered appropriately |
--- |
3924 |
// X op Y --> X, if we know X, Y are ordered appropriately |
--- |
| 3925 |
Ops.erase(Ops.begin() + i + 1, Ops.begin() + i + 2); |
0 |
3925 |
Ops.erase(Ops.begin() + i + 1, Ops.begin() + i + 2); |
0 |
| 3926 |
--i; |
0 |
3926 |
--i; |
0 |
| 3927 |
--e; |
0 |
3927 |
--e; |
0 |
| 3928 |
} else if (isKnownViaNonRecursiveReasoning(SecondPred, Ops[i], |
0 |
3928 |
} else if (isKnownViaNonRecursiveReasoning(SecondPred, Ops[i], |
0 |
| 3929 |
Ops[i + 1])) { |
0 |
3929 |
Ops[i + 1])) { |
0 |
| 3930 |
// X op Y --> Y, if we know X, Y are ordered appropriately |
--- |
3930 |
// X op Y --> Y, if we know X, Y are ordered appropriately |
--- |
| 3931 |
Ops.erase(Ops.begin() + i, Ops.begin() + i + 1); |
0 |
3931 |
Ops.erase(Ops.begin() + i, Ops.begin() + i + 1); |
0 |
| 3932 |
--i; |
0 |
3932 |
--i; |
0 |
| 3933 |
--e; |
0 |
3933 |
--e; |
0 |
| 3934 |
} |
--- |
3934 |
} |
--- |
| 3935 |
} |
--- |
3935 |
} |
--- |
| 3936 |
|
--- |
3936 |
|
--- |
| 3937 |
if (Ops.size() == 1) return Ops[0]; |
0 |
3937 |
if (Ops.size() == 1) return Ops[0]; |
0 |
| 3938 |
|
--- |
3938 |
|
--- |
| 3939 |
assert(!Ops.empty() && "Reduced smax down to nothing!"); |
0 |
3939 |
assert(!Ops.empty() && "Reduced smax down to nothing!"); |
0 |
| 3940 |
|
--- |
3940 |
|
--- |
| 3941 |
// Okay, it looks like we really DO need an expr. Check to see if we |
--- |
3941 |
// Okay, it looks like we really DO need an expr. Check to see if we |
--- |
| 3942 |
// already have one, otherwise create a new one. |
--- |
3942 |
// already have one, otherwise create a new one. |
--- |
| 3943 |
FoldingSetNodeID ID; |
0 |
3943 |
FoldingSetNodeID ID; |
0 |
| 3944 |
ID.AddInteger(Kind); |
0 |
3944 |
ID.AddInteger(Kind); |
0 |
| 3945 |
for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
0 |
3945 |
for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
0 |
| 3946 |
ID.AddPointer(Ops[i]); |
0 |
3946 |
ID.AddPointer(Ops[i]); |
0 |
| 3947 |
void *IP = nullptr; |
0 |
3947 |
void *IP = nullptr; |
0 |
| 3948 |
const SCEV *ExistingSCEV = UniqueSCEVs.FindNodeOrInsertPos(ID, IP); |
0 |
3948 |
const SCEV *ExistingSCEV = UniqueSCEVs.FindNodeOrInsertPos(ID, IP); |
0 |
| 3949 |
if (ExistingSCEV) |
0 |
3949 |
if (ExistingSCEV) |
0 |
| 3950 |
return ExistingSCEV; |
0 |
3950 |
return ExistingSCEV; |
0 |
| 3951 |
const SCEV **O = SCEVAllocator.Allocate(Ops.size()); |
0 |
3951 |
const SCEV **O = SCEVAllocator.Allocate(Ops.size()); |
0 |
| 3952 |
std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
0 |
3952 |
std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
0 |
| 3953 |
SCEV *S = new (SCEVAllocator) |
0 |
3953 |
SCEV *S = new (SCEVAllocator) |
0 |
| 3954 |
SCEVMinMaxExpr(ID.Intern(SCEVAllocator), Kind, O, Ops.size()); |
0 |
3954 |
SCEVMinMaxExpr(ID.Intern(SCEVAllocator), Kind, O, Ops.size()); |
0 |
| 3955 |
|
--- |
3955 |
|
--- |
| 3956 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
3956 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 3957 |
registerUser(S, Ops); |
0 |
3957 |
registerUser(S, Ops); |
0 |
| 3958 |
return S; |
0 |
3958 |
return S; |
0 |
| 3959 |
} |
0 |
3959 |
} |
0 |
| 3960 |
|
--- |
3960 |
|
--- |
| 3961 |
namespace { |
--- |
3961 |
namespace { |
--- |
| 3962 |
|
--- |
3962 |
|
--- |
| 3963 |
class SCEVSequentialMinMaxDeduplicatingVisitor final |
--- |
3963 |
class SCEVSequentialMinMaxDeduplicatingVisitor final |
--- |
| 3964 |
: public SCEVVisitor
| --- |
3964 |
: public SCEVVisitor
| --- |
| |
| 3965 |
std::optional> { |
--- |
3965 |
std::optional> { |
--- |
| 3966 |
using RetVal = std::optional; |
--- |
3966 |
using RetVal = std::optional; |
--- |
| 3967 |
using Base = SCEVVisitor; |
--- |
3967 |
using Base = SCEVVisitor; |
--- |
| 3968 |
|
--- |
3968 |
|
--- |
| 3969 |
ScalarEvolution &SE; |
--- |
3969 |
ScalarEvolution &SE; |
--- |
| 3970 |
const SCEVTypes RootKind; // Must be a sequential min/max expression. |
--- |
3970 |
const SCEVTypes RootKind; // Must be a sequential min/max expression. |
--- |
| 3971 |
const SCEVTypes NonSequentialRootKind; // Non-sequential variant of RootKind. |
--- |
3971 |
const SCEVTypes NonSequentialRootKind; // Non-sequential variant of RootKind. |
--- |
| 3972 |
SmallPtrSet SeenOps; |
--- |
3972 |
SmallPtrSet SeenOps; |
--- |
| 3973 |
|
--- |
3973 |
|
--- |
| 3974 |
bool canRecurseInto(SCEVTypes Kind) const { |
0 |
3974 |
bool canRecurseInto(SCEVTypes Kind) const { |
0 |
| 3975 |
// We can only recurse into the SCEV expression of the same effective type |
--- |
3975 |
// We can only recurse into the SCEV expression of the same effective type |
--- |
| 3976 |
// as the type of our root SCEV expression. |
--- |
3976 |
// as the type of our root SCEV expression. |
--- |
| 3977 |
return RootKind == Kind || NonSequentialRootKind == Kind; |
0 |
3977 |
return RootKind == Kind || NonSequentialRootKind == Kind; |
0 |
| 3978 |
}; |
--- |
3978 |
}; |
--- |
| 3979 |
|
--- |
3979 |
|
--- |
| 3980 |
RetVal visitAnyMinMaxExpr(const SCEV *S) { |
0 |
3980 |
RetVal visitAnyMinMaxExpr(const SCEV *S) { |
0 |
| 3981 |
assert((isa(S) || isa(S)) && |
0 |
3981 |
assert((isa(S) || isa(S)) && |
0 |
| 3982 |
"Only for min/max expressions."); |
--- |
3982 |
"Only for min/max expressions."); |
--- |
| 3983 |
SCEVTypes Kind = S->getSCEVType(); |
0 |
3983 |
SCEVTypes Kind = S->getSCEVType(); |
0 |
| 3984 |
|
--- |
3984 |
|
--- |
| 3985 |
if (!canRecurseInto(Kind)) |
0 |
3985 |
if (!canRecurseInto(Kind)) |
0 |
| 3986 |
return S; |
0 |
3986 |
return S; |
0 |
| 3987 |
|
--- |
3987 |
|
--- |
| 3988 |
auto *NAry = cast(S); |
0 |
3988 |
auto *NAry = cast(S); |
0 |
| 3989 |
SmallVector NewOps; |
0 |
3989 |
SmallVector NewOps; |
0 |
| 3990 |
bool Changed = visit(Kind, NAry->operands(), NewOps); |
0 |
3990 |
bool Changed = visit(Kind, NAry->operands(), NewOps); |
0 |
| 3991 |
|
--- |
3991 |
|
--- |
| 3992 |
if (!Changed) |
0 |
3992 |
if (!Changed) |
0 |
| 3993 |
return S; |
0 |
3993 |
return S; |
0 |
| 3994 |
if (NewOps.empty()) |
0 |
3994 |
if (NewOps.empty()) |
0 |
| 3995 |
return std::nullopt; |
0 |
3995 |
return std::nullopt; |
0 |
| 3996 |
|
--- |
3996 |
|
--- |
| 3997 |
return isa(S) |
0 |
3997 |
return isa(S) |
0 |
| 3998 |
? SE.getSequentialMinMaxExpr(Kind, NewOps) |
0 |
3998 |
? SE.getSequentialMinMaxExpr(Kind, NewOps) |
0 |
| 3999 |
: SE.getMinMaxExpr(Kind, NewOps); |
0 |
3999 |
: SE.getMinMaxExpr(Kind, NewOps); |
0 |
| 4000 |
} |
0 |
4000 |
} |
0 |
| 4001 |
|
--- |
4001 |
|
--- |
| 4002 |
RetVal visit(const SCEV *S) { |
0 |
4002 |
RetVal visit(const SCEV *S) { |
0 |
| 4003 |
// Has the whole operand been seen already? |
--- |
4003 |
// Has the whole operand been seen already? |
--- |
| 4004 |
if (!SeenOps.insert(S).second) |
0 |
4004 |
if (!SeenOps.insert(S).second) |
0 |
| 4005 |
return std::nullopt; |
0 |
4005 |
return std::nullopt; |
0 |
| 4006 |
return Base::visit(S); |
0 |
4006 |
return Base::visit(S); |
0 |
| 4007 |
} |
--- |
4007 |
} |
--- |
| 4008 |
|
--- |
4008 |
|
--- |
| 4009 |
public: |
--- |
4009 |
public: |
--- |
| 4010 |
SCEVSequentialMinMaxDeduplicatingVisitor(ScalarEvolution &SE, |
0 |
4010 |
SCEVSequentialMinMaxDeduplicatingVisitor(ScalarEvolution &SE, |
0 |
| 4011 |
SCEVTypes RootKind) |
--- |
4011 |
SCEVTypes RootKind) |
--- |
| 4012 |
: SE(SE), RootKind(RootKind), |
0 |
4012 |
: SE(SE), RootKind(RootKind), |
0 |
| 4013 |
NonSequentialRootKind( |
0 |
4013 |
NonSequentialRootKind( |
0 |
| 4014 |
SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType( |
0 |
4014 |
SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType( |
0 |
| 4015 |
RootKind)) {} |
0 |
4015 |
RootKind)) {} |
0 |
| 4016 |
|
--- |
4016 |
|
--- |
| 4017 |
bool /*Changed*/ visit(SCEVTypes Kind, ArrayRef OrigOps, |
0 |
4017 |
bool /*Changed*/ visit(SCEVTypes Kind, ArrayRef OrigOps, |
0 |
| 4018 |
SmallVectorImpl &NewOps) { |
--- |
4018 |
SmallVectorImpl &NewOps) { |
--- |
| 4019 |
bool Changed = false; |
0 |
4019 |
bool Changed = false; |
0 |
| 4020 |
SmallVector Ops; |
0 |
4020 |
SmallVector Ops; |
0 |
| 4021 |
Ops.reserve(OrigOps.size()); |
0 |
4021 |
Ops.reserve(OrigOps.size()); |
0 |
| 4022 |
|
--- |
4022 |
|
--- |
| 4023 |
for (const SCEV *Op : OrigOps) { |
0 |
4023 |
for (const SCEV *Op : OrigOps) { |
0 |
| 4024 |
RetVal NewOp = visit(Op); |
0 |
4024 |
RetVal NewOp = visit(Op); |
0 |
| 4025 |
if (NewOp != Op) |
0 |
4025 |
if (NewOp != Op) |
0 |
| 4026 |
Changed = true; |
0 |
4026 |
Changed = true; |
0 |
| 4027 |
if (NewOp) |
0 |
4027 |
if (NewOp) |
0 |
| 4028 |
Ops.emplace_back(*NewOp); |
0 |
4028 |
Ops.emplace_back(*NewOp); |
0 |
| 4029 |
} |
--- |
4029 |
} |
--- |
| 4030 |
|
--- |
4030 |
|
--- |
| 4031 |
if (Changed) |
0 |
4031 |
if (Changed) |
0 |
| 4032 |
NewOps = std::move(Ops); |
0 |
4032 |
NewOps = std::move(Ops); |
0 |
| 4033 |
return Changed; |
0 |
4033 |
return Changed; |
0 |
| 4034 |
} |
0 |
4034 |
} |
0 |
| 4035 |
|
--- |
4035 |
|
--- |
| 4036 |
RetVal visitConstant(const SCEVConstant *Constant) { return Constant; } |
0 |
4036 |
RetVal visitConstant(const SCEVConstant *Constant) { return Constant; } |
0 |
| 4037 |
|
--- |
4037 |
|
--- |
| 4038 |
RetVal visitVScale(const SCEVVScale *VScale) { return VScale; } |
0 |
4038 |
RetVal visitVScale(const SCEVVScale *VScale) { return VScale; } |
0 |
| 4039 |
|
--- |
4039 |
|
--- |
| 4040 |
RetVal visitPtrToIntExpr(const SCEVPtrToIntExpr *Expr) { return Expr; } |
0 |
4040 |
RetVal visitPtrToIntExpr(const SCEVPtrToIntExpr *Expr) { return Expr; } |
0 |
| 4041 |
|
--- |
4041 |
|
--- |
| 4042 |
RetVal visitTruncateExpr(const SCEVTruncateExpr *Expr) { return Expr; } |
0 |
4042 |
RetVal visitTruncateExpr(const SCEVTruncateExpr *Expr) { return Expr; } |
0 |
| 4043 |
|
--- |
4043 |
|
--- |
| 4044 |
RetVal visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { return Expr; } |
0 |
4044 |
RetVal visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { return Expr; } |
0 |
| 4045 |
|
--- |
4045 |
|
--- |
| 4046 |
RetVal visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { return Expr; } |
0 |
4046 |
RetVal visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { return Expr; } |
0 |
| 4047 |
|
--- |
4047 |
|
--- |
| 4048 |
RetVal visitAddExpr(const SCEVAddExpr *Expr) { return Expr; } |
0 |
4048 |
RetVal visitAddExpr(const SCEVAddExpr *Expr) { return Expr; } |
0 |
| 4049 |
|
--- |
4049 |
|
--- |
| 4050 |
RetVal visitMulExpr(const SCEVMulExpr *Expr) { return Expr; } |
0 |
4050 |
RetVal visitMulExpr(const SCEVMulExpr *Expr) { return Expr; } |
0 |
| 4051 |
|
--- |
4051 |
|
--- |
| 4052 |
RetVal visitUDivExpr(const SCEVUDivExpr *Expr) { return Expr; } |
0 |
4052 |
RetVal visitUDivExpr(const SCEVUDivExpr *Expr) { return Expr; } |
0 |
| 4053 |
|
--- |
4053 |
|
--- |
| 4054 |
RetVal visitAddRecExpr(const SCEVAddRecExpr *Expr) { return Expr; } |
0 |
4054 |
RetVal visitAddRecExpr(const SCEVAddRecExpr *Expr) { return Expr; } |
0 |
| 4055 |
|
--- |
4055 |
|
--- |
| 4056 |
RetVal visitSMaxExpr(const SCEVSMaxExpr *Expr) { |
0 |
4056 |
RetVal visitSMaxExpr(const SCEVSMaxExpr *Expr) { |
0 |
| 4057 |
return visitAnyMinMaxExpr(Expr); |
0 |
4057 |
return visitAnyMinMaxExpr(Expr); |
0 |
| 4058 |
} |
--- |
4058 |
} |
--- |
| 4059 |
|
--- |
4059 |
|
--- |
| 4060 |
RetVal visitUMaxExpr(const SCEVUMaxExpr *Expr) { |
0 |
4060 |
RetVal visitUMaxExpr(const SCEVUMaxExpr *Expr) { |
0 |
| 4061 |
return visitAnyMinMaxExpr(Expr); |
0 |
4061 |
return visitAnyMinMaxExpr(Expr); |
0 |
| 4062 |
} |
--- |
4062 |
} |
--- |
| 4063 |
|
--- |
4063 |
|
--- |
| 4064 |
RetVal visitSMinExpr(const SCEVSMinExpr *Expr) { |
0 |
4064 |
RetVal visitSMinExpr(const SCEVSMinExpr *Expr) { |
0 |
| 4065 |
return visitAnyMinMaxExpr(Expr); |
0 |
4065 |
return visitAnyMinMaxExpr(Expr); |
0 |
| 4066 |
} |
--- |
4066 |
} |
--- |
| 4067 |
|
--- |
4067 |
|
--- |
| 4068 |
RetVal visitUMinExpr(const SCEVUMinExpr *Expr) { |
0 |
4068 |
RetVal visitUMinExpr(const SCEVUMinExpr *Expr) { |
0 |
| 4069 |
return visitAnyMinMaxExpr(Expr); |
0 |
4069 |
return visitAnyMinMaxExpr(Expr); |
0 |
| 4070 |
} |
--- |
4070 |
} |
--- |
| 4071 |
|
--- |
4071 |
|
--- |
| 4072 |
RetVal visitSequentialUMinExpr(const SCEVSequentialUMinExpr *Expr) { |
0 |
4072 |
RetVal visitSequentialUMinExpr(const SCEVSequentialUMinExpr *Expr) { |
0 |
| 4073 |
return visitAnyMinMaxExpr(Expr); |
0 |
4073 |
return visitAnyMinMaxExpr(Expr); |
0 |
| 4074 |
} |
--- |
4074 |
} |
--- |
| 4075 |
|
--- |
4075 |
|
--- |
| 4076 |
RetVal visitUnknown(const SCEVUnknown *Expr) { return Expr; } |
0 |
4076 |
RetVal visitUnknown(const SCEVUnknown *Expr) { return Expr; } |
0 |
| 4077 |
|
--- |
4077 |
|
--- |
| 4078 |
RetVal visitCouldNotCompute(const SCEVCouldNotCompute *Expr) { return Expr; } |
0 |
4078 |
RetVal visitCouldNotCompute(const SCEVCouldNotCompute *Expr) { return Expr; } |
0 |
| 4079 |
}; |
--- |
4079 |
}; |
--- |
| 4080 |
|
--- |
4080 |
|
--- |
| 4081 |
} // namespace |
--- |
4081 |
} // namespace |
--- |
| 4082 |
|
--- |
4082 |
|
--- |
| 4083 |
static bool scevUnconditionallyPropagatesPoisonFromOperands(SCEVTypes Kind) { |
0 |
4083 |
static bool scevUnconditionallyPropagatesPoisonFromOperands(SCEVTypes Kind) { |
0 |
| 4084 |
switch (Kind) { |
0 |
4084 |
switch (Kind) { |
0 |
| 4085 |
case scConstant: |
0 |
4085 |
case scConstant: |
0 |
| 4086 |
case scVScale: |
--- |
4086 |
case scVScale: |
--- |
| 4087 |
case scTruncate: |
--- |
4087 |
case scTruncate: |
--- |
| 4088 |
case scZeroExtend: |
--- |
4088 |
case scZeroExtend: |
--- |
| 4089 |
case scSignExtend: |
--- |
4089 |
case scSignExtend: |
--- |
| 4090 |
case scPtrToInt: |
--- |
4090 |
case scPtrToInt: |
--- |
| 4091 |
case scAddExpr: |
--- |
4091 |
case scAddExpr: |
--- |
| 4092 |
case scMulExpr: |
--- |
4092 |
case scMulExpr: |
--- |
| 4093 |
case scUDivExpr: |
--- |
4093 |
case scUDivExpr: |
--- |
| 4094 |
case scAddRecExpr: |
--- |
4094 |
case scAddRecExpr: |
--- |
| 4095 |
case scUMaxExpr: |
--- |
4095 |
case scUMaxExpr: |
--- |
| 4096 |
case scSMaxExpr: |
--- |
4096 |
case scSMaxExpr: |
--- |
| 4097 |
case scUMinExpr: |
--- |
4097 |
case scUMinExpr: |
--- |
| 4098 |
case scSMinExpr: |
--- |
4098 |
case scSMinExpr: |
--- |
| 4099 |
case scUnknown: |
--- |
4099 |
case scUnknown: |
--- |
| 4100 |
// If any operand is poison, the whole expression is poison. |
--- |
4100 |
// If any operand is poison, the whole expression is poison. |
--- |
| 4101 |
return true; |
0 |
4101 |
return true; |
0 |
| 4102 |
case scSequentialUMinExpr: |
0 |
4102 |
case scSequentialUMinExpr: |
0 |
| 4103 |
// FIXME: if the *first* operand is poison, the whole expression is poison. |
--- |
4103 |
// FIXME: if the *first* operand is poison, the whole expression is poison. |
--- |
| 4104 |
return false; // Pessimistically, say that it does not propagate poison. |
0 |
4104 |
return false; // Pessimistically, say that it does not propagate poison. |
0 |
| 4105 |
case scCouldNotCompute: |
0 |
4105 |
case scCouldNotCompute: |
0 |
| 4106 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
4106 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
| 4107 |
} |
--- |
4107 |
} |
--- |
| 4108 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
4108 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
| 4109 |
} |
--- |
4109 |
} |
--- |
| 4110 |
|
--- |
4110 |
|
--- |
| 4111 |
/// Return true if V is poison given that AssumedPoison is already poison. |
--- |
4111 |
/// Return true if V is poison given that AssumedPoison is already poison. |
--- |
| 4112 |
static bool impliesPoison(const SCEV *AssumedPoison, const SCEV *S) { |
0 |
4112 |
static bool impliesPoison(const SCEV *AssumedPoison, const SCEV *S) { |
0 |
| 4113 |
// The only way poison may be introduced in a SCEV expression is from a |
--- |
4113 |
// The only way poison may be introduced in a SCEV expression is from a |
--- |
| 4114 |
// poison SCEVUnknown (ConstantExprs are also represented as SCEVUnknown, |
--- |
4114 |
// poison SCEVUnknown (ConstantExprs are also represented as SCEVUnknown, |
--- |
| 4115 |
// not SCEVConstant). Notably, nowrap flags in SCEV nodes can *not* |
--- |
4115 |
// not SCEVConstant). Notably, nowrap flags in SCEV nodes can *not* |
--- |
| 4116 |
// introduce poison -- they encode guaranteed, non-speculated knowledge. |
--- |
4116 |
// introduce poison -- they encode guaranteed, non-speculated knowledge. |
--- |
| 4117 |
// |
--- |
4117 |
// |
--- |
| 4118 |
// Additionally, all SCEV nodes propagate poison from inputs to outputs, |
--- |
4118 |
// Additionally, all SCEV nodes propagate poison from inputs to outputs, |
--- |
| 4119 |
// with the notable exception of umin_seq, where only poison from the first |
--- |
4119 |
// with the notable exception of umin_seq, where only poison from the first |
--- |
| 4120 |
// operand is (unconditionally) propagated. |
--- |
4120 |
// operand is (unconditionally) propagated. |
--- |
| 4121 |
struct SCEVPoisonCollector { |
--- |
4121 |
struct SCEVPoisonCollector { |
--- |
| 4122 |
bool LookThroughMaybePoisonBlocking; |
--- |
4122 |
bool LookThroughMaybePoisonBlocking; |
--- |
| 4123 |
SmallPtrSet MaybePoison; |
--- |
4123 |
SmallPtrSet MaybePoison; |
--- |
| 4124 |
SCEVPoisonCollector(bool LookThroughMaybePoisonBlocking) |
0 |
4124 |
SCEVPoisonCollector(bool LookThroughMaybePoisonBlocking) |
0 |
| 4125 |
: LookThroughMaybePoisonBlocking(LookThroughMaybePoisonBlocking) {} |
0 |
4125 |
: LookThroughMaybePoisonBlocking(LookThroughMaybePoisonBlocking) {} |
0 |
| 4126 |
|
--- |
4126 |
|
--- |
| 4127 |
bool follow(const SCEV *S) { |
0 |
4127 |
bool follow(const SCEV *S) { |
0 |
| 4128 |
if (!LookThroughMaybePoisonBlocking && |
0 |
4128 |
if (!LookThroughMaybePoisonBlocking && |
0 |
| 4129 |
!scevUnconditionallyPropagatesPoisonFromOperands(S->getSCEVType())) |
0 |
4129 |
!scevUnconditionallyPropagatesPoisonFromOperands(S->getSCEVType())) |
0 |
| 4130 |
return false; |
0 |
4130 |
return false; |
0 |
| 4131 |
|
--- |
4131 |
|
--- |
| 4132 |
if (auto *SU = dyn_cast(S)) { |
0 |
4132 |
if (auto *SU = dyn_cast(S)) { |
0 |
| 4133 |
if (!isGuaranteedNotToBePoison(SU->getValue())) |
0 |
4133 |
if (!isGuaranteedNotToBePoison(SU->getValue())) |
0 |
| 4134 |
MaybePoison.insert(S); |
0 |
4134 |
MaybePoison.insert(S); |
0 |
| 4135 |
} |
--- |
4135 |
} |
--- |
| 4136 |
return true; |
0 |
4136 |
return true; |
0 |
| 4137 |
} |
--- |
4137 |
} |
--- |
| 4138 |
bool isDone() const { return false; } |
0 |
4138 |
bool isDone() const { return false; } |
0 |
| 4139 |
}; |
--- |
4139 |
}; |
--- |
| 4140 |
|
--- |
4140 |
|
--- |
| 4141 |
// First collect all SCEVs that might result in AssumedPoison to be poison. |
--- |
4141 |
// First collect all SCEVs that might result in AssumedPoison to be poison. |
--- |
| 4142 |
// We need to look through potentially poison-blocking operations here, |
--- |
4142 |
// We need to look through potentially poison-blocking operations here, |
--- |
| 4143 |
// because we want to find all SCEVs that *might* result in poison, not only |
--- |
4143 |
// because we want to find all SCEVs that *might* result in poison, not only |
--- |
| 4144 |
// those that are *required* to. |
--- |
4144 |
// those that are *required* to. |
--- |
| 4145 |
SCEVPoisonCollector PC1(/* LookThroughMaybePoisonBlocking */ true); |
0 |
4145 |
SCEVPoisonCollector PC1(/* LookThroughMaybePoisonBlocking */ true); |
0 |
| 4146 |
visitAll(AssumedPoison, PC1); |
0 |
4146 |
visitAll(AssumedPoison, PC1); |
0 |
| 4147 |
|
--- |
4147 |
|
--- |
| 4148 |
// AssumedPoison is never poison. As the assumption is false, the implication |
--- |
4148 |
// AssumedPoison is never poison. As the assumption is false, the implication |
--- |
| 4149 |
// is true. Don't bother walking the other SCEV in this case. |
--- |
4149 |
// is true. Don't bother walking the other SCEV in this case. |
--- |
| 4150 |
if (PC1.MaybePoison.empty()) |
0 |
4150 |
if (PC1.MaybePoison.empty()) |
0 |
| 4151 |
return true; |
0 |
4151 |
return true; |
0 |
| 4152 |
|
--- |
4152 |
|
--- |
| 4153 |
// Collect all SCEVs in S that, if poison, *will* result in S being poison |
--- |
4153 |
// Collect all SCEVs in S that, if poison, *will* result in S being poison |
--- |
| 4154 |
// as well. We cannot look through potentially poison-blocking operations |
--- |
4154 |
// as well. We cannot look through potentially poison-blocking operations |
--- |
| 4155 |
// here, as their arguments only *may* make the result poison. |
--- |
4155 |
// here, as their arguments only *may* make the result poison. |
--- |
| 4156 |
SCEVPoisonCollector PC2(/* LookThroughMaybePoisonBlocking */ false); |
0 |
4156 |
SCEVPoisonCollector PC2(/* LookThroughMaybePoisonBlocking */ false); |
0 |
| 4157 |
visitAll(S, PC2); |
0 |
4157 |
visitAll(S, PC2); |
0 |
| 4158 |
|
--- |
4158 |
|
--- |
| 4159 |
// Make sure that no matter which SCEV in PC1.MaybePoison is actually poison, |
--- |
4159 |
// Make sure that no matter which SCEV in PC1.MaybePoison is actually poison, |
--- |
| 4160 |
// it will also make S poison by being part of PC2.MaybePoison. |
--- |
4160 |
// it will also make S poison by being part of PC2.MaybePoison. |
--- |
| 4161 |
return all_of(PC1.MaybePoison, |
0 |
4161 |
return all_of(PC1.MaybePoison, |
0 |
| 4162 |
[&](const SCEV *S) { return PC2.MaybePoison.contains(S); }); |
0 |
4162 |
[&](const SCEV *S) { return PC2.MaybePoison.contains(S); }); |
0 |
| 4163 |
} |
0 |
4163 |
} |
0 |
| 4164 |
|
--- |
4164 |
|
--- |
| 4165 |
const SCEV * |
--- |
4165 |
const SCEV * |
--- |
| 4166 |
ScalarEvolution::getSequentialMinMaxExpr(SCEVTypes Kind, |
0 |
4166 |
ScalarEvolution::getSequentialMinMaxExpr(SCEVTypes Kind, |
0 |
| 4167 |
SmallVectorImpl &Ops) { |
--- |
4167 |
SmallVectorImpl &Ops) { |
--- |
| 4168 |
assert(SCEVSequentialMinMaxExpr::isSequentialMinMaxType(Kind) && |
0 |
4168 |
assert(SCEVSequentialMinMaxExpr::isSequentialMinMaxType(Kind) && |
0 |
| 4169 |
"Not a SCEVSequentialMinMaxExpr!"); |
--- |
4169 |
"Not a SCEVSequentialMinMaxExpr!"); |
--- |
| 4170 |
assert(!Ops.empty() && "Cannot get empty (u|s)(min|max)!"); |
0 |
4170 |
assert(!Ops.empty() && "Cannot get empty (u|s)(min|max)!"); |
0 |
| 4171 |
if (Ops.size() == 1) |
0 |
4171 |
if (Ops.size() == 1) |
0 |
| 4172 |
return Ops[0]; |
0 |
4172 |
return Ops[0]; |
0 |
| 4173 |
#ifndef NDEBUG |
--- |
4173 |
#ifndef NDEBUG |
--- |
| 4174 |
Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
0 |
4174 |
Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
0 |
| 4175 |
for (unsigned i = 1, e = Ops.size(); i != e; ++i) { |
0 |
4175 |
for (unsigned i = 1, e = Ops.size(); i != e; ++i) { |
0 |
| 4176 |
assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
0 |
4176 |
assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
0 |
| 4177 |
"Operand types don't match!"); |
--- |
4177 |
"Operand types don't match!"); |
--- |
| 4178 |
assert(Ops[0]->getType()->isPointerTy() == |
0 |
4178 |
assert(Ops[0]->getType()->isPointerTy() == |
0 |
| 4179 |
Ops[i]->getType()->isPointerTy() && |
--- |
4179 |
Ops[i]->getType()->isPointerTy() && |
--- |
| 4180 |
"min/max should be consistently pointerish"); |
--- |
4180 |
"min/max should be consistently pointerish"); |
--- |
| 4181 |
} |
--- |
4181 |
} |
--- |
| 4182 |
#endif |
--- |
4182 |
#endif |
--- |
| 4183 |
|
--- |
4183 |
|
--- |
| 4184 |
// Note that SCEVSequentialMinMaxExpr is *NOT* commutative, |
--- |
4184 |
// Note that SCEVSequentialMinMaxExpr is *NOT* commutative, |
--- |
| 4185 |
// so we can *NOT* do any kind of sorting of the expressions! |
--- |
4185 |
// so we can *NOT* do any kind of sorting of the expressions! |
--- |
| 4186 |
|
--- |
4186 |
|
--- |
| 4187 |
// Check if we have created the same expression before. |
--- |
4187 |
// Check if we have created the same expression before. |
--- |
| 4188 |
if (const SCEV *S = findExistingSCEVInCache(Kind, Ops)) |
0 |
4188 |
if (const SCEV *S = findExistingSCEVInCache(Kind, Ops)) |
0 |
| 4189 |
return S; |
0 |
4189 |
return S; |
0 |
| 4190 |
|
--- |
4190 |
|
--- |
| 4191 |
// FIXME: there are *some* simplifications that we can do here. |
--- |
4191 |
// FIXME: there are *some* simplifications that we can do here. |
--- |
| 4192 |
|
--- |
4192 |
|
--- |
| 4193 |
// Keep only the first instance of an operand. |
--- |
4193 |
// Keep only the first instance of an operand. |
--- |
| 4194 |
{ |
--- |
4194 |
{ |
--- |
| 4195 |
SCEVSequentialMinMaxDeduplicatingVisitor Deduplicator(*this, Kind); |
0 |
4195 |
SCEVSequentialMinMaxDeduplicatingVisitor Deduplicator(*this, Kind); |
0 |
| 4196 |
bool Changed = Deduplicator.visit(Kind, Ops, Ops); |
0 |
4196 |
bool Changed = Deduplicator.visit(Kind, Ops, Ops); |
0 |
| 4197 |
if (Changed) |
0 |
4197 |
if (Changed) |
0 |
| 4198 |
return getSequentialMinMaxExpr(Kind, Ops); |
0 |
4198 |
return getSequentialMinMaxExpr(Kind, Ops); |
0 |
| 4199 |
} |
0 |
4199 |
} |
0 |
| 4200 |
|
--- |
4200 |
|
--- |
| 4201 |
// Check to see if one of the operands is of the same kind. If so, expand its |
--- |
4201 |
// Check to see if one of the operands is of the same kind. If so, expand its |
--- |
| 4202 |
// operands onto our operand list, and recurse to simplify. |
--- |
4202 |
// operands onto our operand list, and recurse to simplify. |
--- |
| 4203 |
{ |
--- |
4203 |
{ |
--- |
| 4204 |
unsigned Idx = 0; |
0 |
4204 |
unsigned Idx = 0; |
0 |
| 4205 |
bool DeletedAny = false; |
0 |
4205 |
bool DeletedAny = false; |
0 |
| 4206 |
while (Idx < Ops.size()) { |
0 |
4206 |
while (Idx < Ops.size()) { |
0 |
| 4207 |
if (Ops[Idx]->getSCEVType() != Kind) { |
0 |
4207 |
if (Ops[Idx]->getSCEVType() != Kind) { |
0 |
| 4208 |
++Idx; |
0 |
4208 |
++Idx; |
0 |
| 4209 |
continue; |
0 |
4209 |
continue; |
0 |
| 4210 |
} |
--- |
4210 |
} |
--- |
| 4211 |
const auto *SMME = cast(Ops[Idx]); |
0 |
4211 |
const auto *SMME = cast(Ops[Idx]); |
0 |
| 4212 |
Ops.erase(Ops.begin() + Idx); |
0 |
4212 |
Ops.erase(Ops.begin() + Idx); |
0 |
| 4213 |
Ops.insert(Ops.begin() + Idx, SMME->operands().begin(), |
0 |
4213 |
Ops.insert(Ops.begin() + Idx, SMME->operands().begin(), |
0 |
| 4214 |
SMME->operands().end()); |
0 |
4214 |
SMME->operands().end()); |
0 |
| 4215 |
DeletedAny = true; |
0 |
4215 |
DeletedAny = true; |
0 |
| 4216 |
} |
--- |
4216 |
} |
--- |
| 4217 |
|
--- |
4217 |
|
--- |
| 4218 |
if (DeletedAny) |
0 |
4218 |
if (DeletedAny) |
0 |
| 4219 |
return getSequentialMinMaxExpr(Kind, Ops); |
0 |
4219 |
return getSequentialMinMaxExpr(Kind, Ops); |
0 |
| 4220 |
} |
--- |
4220 |
} |
--- |
| 4221 |
|
--- |
4221 |
|
--- |
| 4222 |
const SCEV *SaturationPoint; |
--- |
4222 |
const SCEV *SaturationPoint; |
--- |
| 4223 |
ICmpInst::Predicate Pred; |
--- |
4223 |
ICmpInst::Predicate Pred; |
--- |
| 4224 |
switch (Kind) { |
0 |
4224 |
switch (Kind) { |
0 |
| 4225 |
case scSequentialUMinExpr: |
0 |
4225 |
case scSequentialUMinExpr: |
0 |
| 4226 |
SaturationPoint = getZero(Ops[0]->getType()); |
0 |
4226 |
SaturationPoint = getZero(Ops[0]->getType()); |
0 |
| 4227 |
Pred = ICmpInst::ICMP_ULE; |
0 |
4227 |
Pred = ICmpInst::ICMP_ULE; |
0 |
| 4228 |
break; |
0 |
4228 |
break; |
0 |
| 4229 |
default: |
0 |
4229 |
default: |
0 |
| 4230 |
llvm_unreachable("Not a sequential min/max type."); |
0 |
4230 |
llvm_unreachable("Not a sequential min/max type."); |
0 |
| 4231 |
} |
--- |
4231 |
} |
--- |
| 4232 |
|
--- |
4232 |
|
--- |
| 4233 |
for (unsigned i = 1, e = Ops.size(); i != e; ++i) { |
0 |
4233 |
for (unsigned i = 1, e = Ops.size(); i != e; ++i) { |
0 |
| 4234 |
// We can replace %x umin_seq %y with %x umin %y if either: |
--- |
4234 |
// We can replace %x umin_seq %y with %x umin %y if either: |
--- |
| 4235 |
// * %y being poison implies %x is also poison. |
--- |
4235 |
// * %y being poison implies %x is also poison. |
--- |
| 4236 |
// * %x cannot be the saturating value (e.g. zero for umin). |
--- |
4236 |
// * %x cannot be the saturating value (e.g. zero for umin). |
--- |
| 4237 |
if (::impliesPoison(Ops[i], Ops[i - 1]) || |
0 |
4237 |
if (::impliesPoison(Ops[i], Ops[i - 1]) || |
0 |
| 4238 |
isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_NE, Ops[i - 1], |
0 |
4238 |
isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_NE, Ops[i - 1], |
0 |
| 4239 |
SaturationPoint)) { |
--- |
4239 |
SaturationPoint)) { |
--- |
| 4240 |
SmallVector SeqOps = {Ops[i - 1], Ops[i]}; |
0 |
4240 |
SmallVector SeqOps = {Ops[i - 1], Ops[i]}; |
0 |
| 4241 |
Ops[i - 1] = getMinMaxExpr( |
0 |
4241 |
Ops[i - 1] = getMinMaxExpr( |
0 |
| 4242 |
SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType(Kind), |
0 |
4242 |
SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType(Kind), |
0 |
| 4243 |
SeqOps); |
--- |
4243 |
SeqOps); |
--- |
| 4244 |
Ops.erase(Ops.begin() + i); |
0 |
4244 |
Ops.erase(Ops.begin() + i); |
0 |
| 4245 |
return getSequentialMinMaxExpr(Kind, Ops); |
0 |
4245 |
return getSequentialMinMaxExpr(Kind, Ops); |
0 |
| 4246 |
} |
0 |
4246 |
} |
0 |
| 4247 |
// Fold %x umin_seq %y to %x if %x ule %y. |
--- |
4247 |
// Fold %x umin_seq %y to %x if %x ule %y. |
--- |
| 4248 |
// TODO: We might be able to prove the predicate for a later operand. |
--- |
4248 |
// TODO: We might be able to prove the predicate for a later operand. |
--- |
| 4249 |
if (isKnownViaNonRecursiveReasoning(Pred, Ops[i - 1], Ops[i])) { |
0 |
4249 |
if (isKnownViaNonRecursiveReasoning(Pred, Ops[i - 1], Ops[i])) { |
0 |
| 4250 |
Ops.erase(Ops.begin() + i); |
0 |
4250 |
Ops.erase(Ops.begin() + i); |
0 |
| 4251 |
return getSequentialMinMaxExpr(Kind, Ops); |
0 |
4251 |
return getSequentialMinMaxExpr(Kind, Ops); |
0 |
| 4252 |
} |
--- |
4252 |
} |
--- |
| 4253 |
} |
--- |
4253 |
} |
--- |
| 4254 |
|
--- |
4254 |
|
--- |
| 4255 |
// Okay, it looks like we really DO need an expr. Check to see if we |
--- |
4255 |
// Okay, it looks like we really DO need an expr. Check to see if we |
--- |
| 4256 |
// already have one, otherwise create a new one. |
--- |
4256 |
// already have one, otherwise create a new one. |
--- |
| 4257 |
FoldingSetNodeID ID; |
0 |
4257 |
FoldingSetNodeID ID; |
0 |
| 4258 |
ID.AddInteger(Kind); |
0 |
4258 |
ID.AddInteger(Kind); |
0 |
| 4259 |
for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
0 |
4259 |
for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
0 |
| 4260 |
ID.AddPointer(Ops[i]); |
0 |
4260 |
ID.AddPointer(Ops[i]); |
0 |
| 4261 |
void *IP = nullptr; |
0 |
4261 |
void *IP = nullptr; |
0 |
| 4262 |
const SCEV *ExistingSCEV = UniqueSCEVs.FindNodeOrInsertPos(ID, IP); |
0 |
4262 |
const SCEV *ExistingSCEV = UniqueSCEVs.FindNodeOrInsertPos(ID, IP); |
0 |
| 4263 |
if (ExistingSCEV) |
0 |
4263 |
if (ExistingSCEV) |
0 |
| 4264 |
return ExistingSCEV; |
0 |
4264 |
return ExistingSCEV; |
0 |
| 4265 |
|
--- |
4265 |
|
--- |
| 4266 |
const SCEV **O = SCEVAllocator.Allocate(Ops.size()); |
0 |
4266 |
const SCEV **O = SCEVAllocator.Allocate(Ops.size()); |
0 |
| 4267 |
std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
0 |
4267 |
std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
0 |
| 4268 |
SCEV *S = new (SCEVAllocator) |
0 |
4268 |
SCEV *S = new (SCEVAllocator) |
0 |
| 4269 |
SCEVSequentialMinMaxExpr(ID.Intern(SCEVAllocator), Kind, O, Ops.size()); |
0 |
4269 |
SCEVSequentialMinMaxExpr(ID.Intern(SCEVAllocator), Kind, O, Ops.size()); |
0 |
| 4270 |
|
--- |
4270 |
|
--- |
| 4271 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
4271 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 4272 |
registerUser(S, Ops); |
0 |
4272 |
registerUser(S, Ops); |
0 |
| 4273 |
return S; |
0 |
4273 |
return S; |
0 |
| 4274 |
} |
--- |
4274 |
} |
--- |
| 4275 |
|
--- |
4275 |
|
--- |
| 4276 |
const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, const SCEV *RHS) { |
0 |
4276 |
const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, const SCEV *RHS) { |
0 |
| 4277 |
SmallVector Ops = {LHS, RHS}; |
0 |
4277 |
SmallVector Ops = {LHS, RHS}; |
0 |
| 4278 |
return getSMaxExpr(Ops); |
0 |
4278 |
return getSMaxExpr(Ops); |
0 |
| 4279 |
} |
0 |
4279 |
} |
0 |
| 4280 |
|
--- |
4280 |
|
--- |
| 4281 |
const SCEV *ScalarEvolution::getSMaxExpr(SmallVectorImpl &Ops) { |
0 |
4281 |
const SCEV *ScalarEvolution::getSMaxExpr(SmallVectorImpl &Ops) { |
0 |
| 4282 |
return getMinMaxExpr(scSMaxExpr, Ops); |
0 |
4282 |
return getMinMaxExpr(scSMaxExpr, Ops); |
0 |
| 4283 |
} |
--- |
4283 |
} |
--- |
| 4284 |
|
--- |
4284 |
|
--- |
| 4285 |
const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, const SCEV *RHS) { |
0 |
4285 |
const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, const SCEV *RHS) { |
0 |
| 4286 |
SmallVector Ops = {LHS, RHS}; |
0 |
4286 |
SmallVector Ops = {LHS, RHS}; |
0 |
| 4287 |
return getUMaxExpr(Ops); |
0 |
4287 |
return getUMaxExpr(Ops); |
0 |
| 4288 |
} |
0 |
4288 |
} |
0 |
| 4289 |
|
--- |
4289 |
|
--- |
| 4290 |
const SCEV *ScalarEvolution::getUMaxExpr(SmallVectorImpl &Ops) { |
0 |
4290 |
const SCEV *ScalarEvolution::getUMaxExpr(SmallVectorImpl &Ops) { |
0 |
| 4291 |
return getMinMaxExpr(scUMaxExpr, Ops); |
0 |
4291 |
return getMinMaxExpr(scUMaxExpr, Ops); |
0 |
| 4292 |
} |
--- |
4292 |
} |
--- |
| 4293 |
|
--- |
4293 |
|
--- |
| 4294 |
const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, |
0 |
4294 |
const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, |
0 |
| 4295 |
const SCEV *RHS) { |
--- |
4295 |
const SCEV *RHS) { |
--- |
| 4296 |
SmallVector Ops = { LHS, RHS }; |
0 |
4296 |
SmallVector Ops = { LHS, RHS }; |
0 |
| 4297 |
return getSMinExpr(Ops); |
0 |
4297 |
return getSMinExpr(Ops); |
0 |
| 4298 |
} |
0 |
4298 |
} |
0 |
| 4299 |
|
--- |
4299 |
|
--- |
| 4300 |
const SCEV *ScalarEvolution::getSMinExpr(SmallVectorImpl &Ops) { |
0 |
4300 |
const SCEV *ScalarEvolution::getSMinExpr(SmallVectorImpl &Ops) { |
0 |
| 4301 |
return getMinMaxExpr(scSMinExpr, Ops); |
0 |
4301 |
return getMinMaxExpr(scSMinExpr, Ops); |
0 |
| 4302 |
} |
--- |
4302 |
} |
--- |
| 4303 |
|
--- |
4303 |
|
--- |
| 4304 |
const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, const SCEV *RHS, |
0 |
4304 |
const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, const SCEV *RHS, |
0 |
| 4305 |
bool Sequential) { |
--- |
4305 |
bool Sequential) { |
--- |
| 4306 |
SmallVector Ops = { LHS, RHS }; |
0 |
4306 |
SmallVector Ops = { LHS, RHS }; |
0 |
| 4307 |
return getUMinExpr(Ops, Sequential); |
0 |
4307 |
return getUMinExpr(Ops, Sequential); |
0 |
| 4308 |
} |
0 |
4308 |
} |
0 |
| 4309 |
|
--- |
4309 |
|
--- |
| 4310 |
const SCEV *ScalarEvolution::getUMinExpr(SmallVectorImpl &Ops, |
0 |
4310 |
const SCEV *ScalarEvolution::getUMinExpr(SmallVectorImpl &Ops, |
0 |
| 4311 |
bool Sequential) { |
--- |
4311 |
bool Sequential) { |
--- |
| 4312 |
return Sequential ? getSequentialMinMaxExpr(scSequentialUMinExpr, Ops) |
0 |
4312 |
return Sequential ? getSequentialMinMaxExpr(scSequentialUMinExpr, Ops) |
0 |
| 4313 |
: getMinMaxExpr(scUMinExpr, Ops); |
0 |
4313 |
: getMinMaxExpr(scUMinExpr, Ops); |
0 |
| 4314 |
} |
--- |
4314 |
} |
--- |
| 4315 |
|
--- |
4315 |
|
--- |
| 4316 |
const SCEV * |
--- |
4316 |
const SCEV * |
--- |
| 4317 |
ScalarEvolution::getSizeOfExpr(Type *IntTy, TypeSize Size) { |
0 |
4317 |
ScalarEvolution::getSizeOfExpr(Type *IntTy, TypeSize Size) { |
0 |
| 4318 |
const SCEV *Res = getConstant(IntTy, Size.getKnownMinValue()); |
0 |
4318 |
const SCEV *Res = getConstant(IntTy, Size.getKnownMinValue()); |
0 |
| 4319 |
if (Size.isScalable()) |
0 |
4319 |
if (Size.isScalable()) |
0 |
| 4320 |
Res = getMulExpr(Res, getVScale(IntTy)); |
0 |
4320 |
Res = getMulExpr(Res, getVScale(IntTy)); |
0 |
| 4321 |
return Res; |
0 |
4321 |
return Res; |
0 |
| 4322 |
} |
--- |
4322 |
} |
--- |
| 4323 |
|
--- |
4323 |
|
--- |
| 4324 |
const SCEV *ScalarEvolution::getSizeOfExpr(Type *IntTy, Type *AllocTy) { |
0 |
4324 |
const SCEV *ScalarEvolution::getSizeOfExpr(Type *IntTy, Type *AllocTy) { |
0 |
| 4325 |
return getSizeOfExpr(IntTy, getDataLayout().getTypeAllocSize(AllocTy)); |
0 |
4325 |
return getSizeOfExpr(IntTy, getDataLayout().getTypeAllocSize(AllocTy)); |
0 |
| 4326 |
} |
--- |
4326 |
} |
--- |
| 4327 |
|
--- |
4327 |
|
--- |
| 4328 |
const SCEV *ScalarEvolution::getStoreSizeOfExpr(Type *IntTy, Type *StoreTy) { |
0 |
4328 |
const SCEV *ScalarEvolution::getStoreSizeOfExpr(Type *IntTy, Type *StoreTy) { |
0 |
| 4329 |
return getSizeOfExpr(IntTy, getDataLayout().getTypeStoreSize(StoreTy)); |
0 |
4329 |
return getSizeOfExpr(IntTy, getDataLayout().getTypeStoreSize(StoreTy)); |
0 |
| 4330 |
} |
--- |
4330 |
} |
--- |
| 4331 |
|
--- |
4331 |
|
--- |
| 4332 |
const SCEV *ScalarEvolution::getOffsetOfExpr(Type *IntTy, |
0 |
4332 |
const SCEV *ScalarEvolution::getOffsetOfExpr(Type *IntTy, |
0 |
| 4333 |
StructType *STy, |
--- |
4333 |
StructType *STy, |
--- |
| 4334 |
unsigned FieldNo) { |
--- |
4334 |
unsigned FieldNo) { |
--- |
| 4335 |
// We can bypass creating a target-independent constant expression and then |
--- |
4335 |
// We can bypass creating a target-independent constant expression and then |
--- |
| 4336 |
// folding it back into a ConstantInt. This is just a compile-time |
--- |
4336 |
// folding it back into a ConstantInt. This is just a compile-time |
--- |
| 4337 |
// optimization. |
--- |
4337 |
// optimization. |
--- |
| 4338 |
const StructLayout *SL = getDataLayout().getStructLayout(STy); |
0 |
4338 |
const StructLayout *SL = getDataLayout().getStructLayout(STy); |
0 |
| 4339 |
assert(!SL->getSizeInBits().isScalable() && |
0 |
4339 |
assert(!SL->getSizeInBits().isScalable() && |
0 |
| 4340 |
"Cannot get offset for structure containing scalable vector types"); |
--- |
4340 |
"Cannot get offset for structure containing scalable vector types"); |
--- |
| 4341 |
return getConstant(IntTy, SL->getElementOffset(FieldNo)); |
0 |
4341 |
return getConstant(IntTy, SL->getElementOffset(FieldNo)); |
0 |
| 4342 |
} |
--- |
4342 |
} |
--- |
| 4343 |
|
--- |
4343 |
|
--- |
| 4344 |
const SCEV *ScalarEvolution::getUnknown(Value *V) { |
0 |
4344 |
const SCEV *ScalarEvolution::getUnknown(Value *V) { |
0 |
| 4345 |
// Don't attempt to do anything other than create a SCEVUnknown object |
--- |
4345 |
// Don't attempt to do anything other than create a SCEVUnknown object |
--- |
| 4346 |
// here. createSCEV only calls getUnknown after checking for all other |
--- |
4346 |
// here. createSCEV only calls getUnknown after checking for all other |
--- |
| 4347 |
// interesting possibilities, and any other code that calls getUnknown |
--- |
4347 |
// interesting possibilities, and any other code that calls getUnknown |
--- |
| 4348 |
// is doing so in order to hide a value from SCEV canonicalization. |
--- |
4348 |
// is doing so in order to hide a value from SCEV canonicalization. |
--- |
| 4349 |
|
--- |
4349 |
|
--- |
| 4350 |
FoldingSetNodeID ID; |
0 |
4350 |
FoldingSetNodeID ID; |
0 |
| 4351 |
ID.AddInteger(scUnknown); |
0 |
4351 |
ID.AddInteger(scUnknown); |
0 |
| 4352 |
ID.AddPointer(V); |
0 |
4352 |
ID.AddPointer(V); |
0 |
| 4353 |
void *IP = nullptr; |
0 |
4353 |
void *IP = nullptr; |
0 |
| 4354 |
if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) { |
0 |
4354 |
if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) { |
0 |
| 4355 |
assert(cast(S)->getValue() == V && |
0 |
4355 |
assert(cast(S)->getValue() == V && |
0 |
| 4356 |
"Stale SCEVUnknown in uniquing map!"); |
--- |
4356 |
"Stale SCEVUnknown in uniquing map!"); |
--- |
| 4357 |
return S; |
0 |
4357 |
return S; |
0 |
| 4358 |
} |
--- |
4358 |
} |
--- |
| 4359 |
SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this, |
0 |
4359 |
SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this, |
0 |
| 4360 |
FirstUnknown); |
0 |
4360 |
FirstUnknown); |
0 |
| 4361 |
FirstUnknown = cast(S); |
0 |
4361 |
FirstUnknown = cast(S); |
0 |
| 4362 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
4362 |
UniqueSCEVs.InsertNode(S, IP); |
0 |
| 4363 |
return S; |
0 |
4363 |
return S; |
0 |
| 4364 |
} |
0 |
4364 |
} |
0 |
| 4365 |
|
--- |
4365 |
|
--- |
| 4366 |
//===----------------------------------------------------------------------===// |
--- |
4366 |
//===----------------------------------------------------------------------===// |
--- |
| 4367 |
// Basic SCEV Analysis and PHI Idiom Recognition Code |
--- |
4367 |
// Basic SCEV Analysis and PHI Idiom Recognition Code |
--- |
| 4368 |
// |
--- |
4368 |
// |
--- |
| 4369 |
|
--- |
4369 |
|
--- |
| 4370 |
/// Test if values of the given type are analyzable within the SCEV |
--- |
4370 |
/// Test if values of the given type are analyzable within the SCEV |
--- |
| 4371 |
/// framework. This primarily includes integer types, and it can optionally |
--- |
4371 |
/// framework. This primarily includes integer types, and it can optionally |
--- |
| 4372 |
/// include pointer types if the ScalarEvolution class has access to |
--- |
4372 |
/// include pointer types if the ScalarEvolution class has access to |
--- |
| 4373 |
/// target-specific information. |
--- |
4373 |
/// target-specific information. |
--- |
| 4374 |
bool ScalarEvolution::isSCEVable(Type *Ty) const { |
0 |
4374 |
bool ScalarEvolution::isSCEVable(Type *Ty) const { |
0 |
| 4375 |
// Integers and pointers are always SCEVable. |
--- |
4375 |
// Integers and pointers are always SCEVable. |
--- |
| 4376 |
return Ty->isIntOrPtrTy(); |
0 |
4376 |
return Ty->isIntOrPtrTy(); |
0 |
| 4377 |
} |
--- |
4377 |
} |
--- |
| 4378 |
|
--- |
4378 |
|
--- |
| 4379 |
/// Return the size in bits of the specified type, for which isSCEVable must |
--- |
4379 |
/// Return the size in bits of the specified type, for which isSCEVable must |
--- |
| 4380 |
/// return true. |
--- |
4380 |
/// return true. |
--- |
| 4381 |
uint64_t ScalarEvolution::getTypeSizeInBits(Type *Ty) const { |
0 |
4381 |
uint64_t ScalarEvolution::getTypeSizeInBits(Type *Ty) const { |
0 |
| 4382 |
assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
0 |
4382 |
assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
0 |
| 4383 |
if (Ty->isPointerTy()) |
0 |
4383 |
if (Ty->isPointerTy()) |
0 |
| 4384 |
return getDataLayout().getIndexTypeSizeInBits(Ty); |
0 |
4384 |
return getDataLayout().getIndexTypeSizeInBits(Ty); |
0 |
| 4385 |
return getDataLayout().getTypeSizeInBits(Ty); |
0 |
4385 |
return getDataLayout().getTypeSizeInBits(Ty); |
0 |
| 4386 |
} |
--- |
4386 |
} |
--- |
| 4387 |
|
--- |
4387 |
|
--- |
| 4388 |
/// Return a type with the same bitwidth as the given type and which represents |
--- |
4388 |
/// Return a type with the same bitwidth as the given type and which represents |
--- |
| 4389 |
/// how SCEV will treat the given type, for which isSCEVable must return |
--- |
4389 |
/// how SCEV will treat the given type, for which isSCEVable must return |
--- |
| 4390 |
/// true. For pointer types, this is the pointer index sized integer type. |
--- |
4390 |
/// true. For pointer types, this is the pointer index sized integer type. |
--- |
| 4391 |
Type *ScalarEvolution::getEffectiveSCEVType(Type *Ty) const { |
0 |
4391 |
Type *ScalarEvolution::getEffectiveSCEVType(Type *Ty) const { |
0 |
| 4392 |
assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
0 |
4392 |
assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
0 |
| 4393 |
|
--- |
4393 |
|
--- |
| 4394 |
if (Ty->isIntegerTy()) |
0 |
4394 |
if (Ty->isIntegerTy()) |
0 |
| 4395 |
return Ty; |
0 |
4395 |
return Ty; |
0 |
| 4396 |
|
--- |
4396 |
|
--- |
| 4397 |
// The only other support type is pointer. |
--- |
4397 |
// The only other support type is pointer. |
--- |
| 4398 |
assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!"); |
0 |
4398 |
assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!"); |
0 |
| 4399 |
return getDataLayout().getIndexType(Ty); |
0 |
4399 |
return getDataLayout().getIndexType(Ty); |
0 |
| 4400 |
} |
--- |
4400 |
} |
--- |
| 4401 |
|
--- |
4401 |
|
--- |
| 4402 |
Type *ScalarEvolution::getWiderType(Type *T1, Type *T2) const { |
0 |
4402 |
Type *ScalarEvolution::getWiderType(Type *T1, Type *T2) const { |
0 |
| 4403 |
return getTypeSizeInBits(T1) >= getTypeSizeInBits(T2) ? T1 : T2; |
0 |
4403 |
return getTypeSizeInBits(T1) >= getTypeSizeInBits(T2) ? T1 : T2; |
0 |
| 4404 |
} |
--- |
4404 |
} |
--- |
| 4405 |
|
--- |
4405 |
|
--- |
| 4406 |
bool ScalarEvolution::instructionCouldExistWitthOperands(const SCEV *A, |
0 |
4406 |
bool ScalarEvolution::instructionCouldExistWitthOperands(const SCEV *A, |
0 |
| 4407 |
const SCEV *B) { |
--- |
4407 |
const SCEV *B) { |
--- |
| 4408 |
/// For a valid use point to exist, the defining scope of one operand |
--- |
4408 |
/// For a valid use point to exist, the defining scope of one operand |
--- |
| 4409 |
/// must dominate the other. |
--- |
4409 |
/// must dominate the other. |
--- |
| 4410 |
bool PreciseA, PreciseB; |
--- |
4410 |
bool PreciseA, PreciseB; |
--- |
| 4411 |
auto *ScopeA = getDefiningScopeBound({A}, PreciseA); |
0 |
4411 |
auto *ScopeA = getDefiningScopeBound({A}, PreciseA); |
0 |
| 4412 |
auto *ScopeB = getDefiningScopeBound({B}, PreciseB); |
0 |
4412 |
auto *ScopeB = getDefiningScopeBound({B}, PreciseB); |
0 |
| 4413 |
if (!PreciseA || !PreciseB) |
0 |
4413 |
if (!PreciseA || !PreciseB) |
0 |
| 4414 |
// Can't tell. |
--- |
4414 |
// Can't tell. |
--- |
| 4415 |
return false; |
0 |
4415 |
return false; |
0 |
| 4416 |
return (ScopeA == ScopeB) || DT.dominates(ScopeA, ScopeB) || |
0 |
4416 |
return (ScopeA == ScopeB) || DT.dominates(ScopeA, ScopeB) || |
0 |
| 4417 |
DT.dominates(ScopeB, ScopeA); |
0 |
4417 |
DT.dominates(ScopeB, ScopeA); |
0 |
| 4418 |
} |
--- |
4418 |
} |
--- |
| 4419 |
|
--- |
4419 |
|
--- |
| 4420 |
|
--- |
4420 |
|
--- |
| 4421 |
const SCEV *ScalarEvolution::getCouldNotCompute() { |
0 |
4421 |
const SCEV *ScalarEvolution::getCouldNotCompute() { |
0 |
| 4422 |
return CouldNotCompute.get(); |
0 |
4422 |
return CouldNotCompute.get(); |
0 |
| 4423 |
} |
--- |
4423 |
} |
--- |
| 4424 |
|
--- |
4424 |
|
--- |
| 4425 |
bool ScalarEvolution::checkValidity(const SCEV *S) const { |
0 |
4425 |
bool ScalarEvolution::checkValidity(const SCEV *S) const { |
0 |
| 4426 |
bool ContainsNulls = SCEVExprContains(S, [](const SCEV *S) { |
0 |
4426 |
bool ContainsNulls = SCEVExprContains(S, [](const SCEV *S) { |
0 |
| 4427 |
auto *SU = dyn_cast(S); |
0 |
4427 |
auto *SU = dyn_cast(S); |
0 |
| 4428 |
return SU && SU->getValue() == nullptr; |
0 |
4428 |
return SU && SU->getValue() == nullptr; |
0 |
| 4429 |
}); |
--- |
4429 |
}); |
--- |
| 4430 |
|
--- |
4430 |
|
--- |
| 4431 |
return !ContainsNulls; |
0 |
4431 |
return !ContainsNulls; |
0 |
| 4432 |
} |
--- |
4432 |
} |
--- |
| 4433 |
|
--- |
4433 |
|
--- |
| 4434 |
bool ScalarEvolution::containsAddRecurrence(const SCEV *S) { |
0 |
4434 |
bool ScalarEvolution::containsAddRecurrence(const SCEV *S) { |
0 |
| 4435 |
HasRecMapType::iterator I = HasRecMap.find(S); |
0 |
4435 |
HasRecMapType::iterator I = HasRecMap.find(S); |
0 |
| 4436 |
if (I != HasRecMap.end()) |
0 |
4436 |
if (I != HasRecMap.end()) |
0 |
| 4437 |
return I->second; |
0 |
4437 |
return I->second; |
0 |
| 4438 |
|
--- |
4438 |
|
--- |
| 4439 |
bool FoundAddRec = |
--- |
4439 |
bool FoundAddRec = |
--- |
| 4440 |
SCEVExprContains(S, [](const SCEV *S) { return isa(S); }); |
0 |
4440 |
SCEVExprContains(S, [](const SCEV *S) { return isa(S); }); |
0 |
| 4441 |
HasRecMap.insert({S, FoundAddRec}); |
0 |
4441 |
HasRecMap.insert({S, FoundAddRec}); |
0 |
| 4442 |
return FoundAddRec; |
0 |
4442 |
return FoundAddRec; |
0 |
| 4443 |
} |
--- |
4443 |
} |
--- |
| 4444 |
|
--- |
4444 |
|
--- |
| 4445 |
/// Return the ValueOffsetPair set for \p S. \p S can be represented |
--- |
4445 |
/// Return the ValueOffsetPair set for \p S. \p S can be represented |
--- |
| 4446 |
/// by the value and offset from any ValueOffsetPair in the set. |
--- |
4446 |
/// by the value and offset from any ValueOffsetPair in the set. |
--- |
| 4447 |
ArrayRef ScalarEvolution::getSCEVValues(const SCEV *S) { |
0 |
4447 |
ArrayRef ScalarEvolution::getSCEVValues(const SCEV *S) { |
0 |
| 4448 |
ExprValueMapType::iterator SI = ExprValueMap.find_as(S); |
0 |
4448 |
ExprValueMapType::iterator SI = ExprValueMap.find_as(S); |
0 |
| 4449 |
if (SI == ExprValueMap.end()) |
0 |
4449 |
if (SI == ExprValueMap.end()) |
0 |
| 4450 |
return std::nullopt; |
0 |
4450 |
return std::nullopt; |
0 |
| 4451 |
return SI->second.getArrayRef(); |
0 |
4451 |
return SI->second.getArrayRef(); |
0 |
| 4452 |
} |
--- |
4452 |
} |
--- |
| 4453 |
|
--- |
4453 |
|
--- |
| 4454 |
/// Erase Value from ValueExprMap and ExprValueMap. ValueExprMap.erase(V) |
--- |
4454 |
/// Erase Value from ValueExprMap and ExprValueMap. ValueExprMap.erase(V) |
--- |
| 4455 |
/// cannot be used separately. eraseValueFromMap should be used to remove |
--- |
4455 |
/// cannot be used separately. eraseValueFromMap should be used to remove |
--- |
| 4456 |
/// V from ValueExprMap and ExprValueMap at the same time. |
--- |
4456 |
/// V from ValueExprMap and ExprValueMap at the same time. |
--- |
| 4457 |
void ScalarEvolution::eraseValueFromMap(Value *V) { |
0 |
4457 |
void ScalarEvolution::eraseValueFromMap(Value *V) { |
0 |
| 4458 |
ValueExprMapType::iterator I = ValueExprMap.find_as(V); |
0 |
4458 |
ValueExprMapType::iterator I = ValueExprMap.find_as(V); |
0 |
| 4459 |
if (I != ValueExprMap.end()) { |
0 |
4459 |
if (I != ValueExprMap.end()) { |
0 |
| 4460 |
auto EVIt = ExprValueMap.find(I->second); |
0 |
4460 |
auto EVIt = ExprValueMap.find(I->second); |
0 |
| 4461 |
bool Removed = EVIt->second.remove(V); |
0 |
4461 |
bool Removed = EVIt->second.remove(V); |
0 |
| 4462 |
(void) Removed; |
--- |
4462 |
(void) Removed; |
--- |
| 4463 |
assert(Removed && "Value not in ExprValueMap?"); |
0 |
4463 |
assert(Removed && "Value not in ExprValueMap?"); |
0 |
| 4464 |
ValueExprMap.erase(I); |
0 |
4464 |
ValueExprMap.erase(I); |
0 |
| 4465 |
} |
--- |
4465 |
} |
--- |
| 4466 |
} |
0 |
4466 |
} |
0 |
| 4467 |
|
--- |
4467 |
|
--- |
| 4468 |
void ScalarEvolution::insertValueToMap(Value *V, const SCEV *S) { |
0 |
4468 |
void ScalarEvolution::insertValueToMap(Value *V, const SCEV *S) { |
0 |
| 4469 |
// A recursive query may have already computed the SCEV. It should be |
--- |
4469 |
// A recursive query may have already computed the SCEV. It should be |
--- |
| 4470 |
// equivalent, but may not necessarily be exactly the same, e.g. due to lazily |
--- |
4470 |
// equivalent, but may not necessarily be exactly the same, e.g. due to lazily |
--- |
| 4471 |
// inferred nowrap flags. |
--- |
4471 |
// inferred nowrap flags. |
--- |
| 4472 |
auto It = ValueExprMap.find_as(V); |
0 |
4472 |
auto It = ValueExprMap.find_as(V); |
0 |
| 4473 |
if (It == ValueExprMap.end()) { |
0 |
4473 |
if (It == ValueExprMap.end()) { |
0 |
| 4474 |
ValueExprMap.insert({SCEVCallbackVH(V, this), S}); |
0 |
4474 |
ValueExprMap.insert({SCEVCallbackVH(V, this), S}); |
0 |
| 4475 |
ExprValueMap[S].insert(V); |
0 |
4475 |
ExprValueMap[S].insert(V); |
0 |
| 4476 |
} |
--- |
4476 |
} |
--- |
| 4477 |
} |
0 |
4477 |
} |
0 |
| 4478 |
|
--- |
4478 |
|
--- |
| 4479 |
/// Determine whether this instruction is either not SCEVable or will always |
--- |
4479 |
/// Determine whether this instruction is either not SCEVable or will always |
--- |
| 4480 |
/// produce a SCEVUnknown. We do not have to walk past such instructions when |
--- |
4480 |
/// produce a SCEVUnknown. We do not have to walk past such instructions when |
--- |
| 4481 |
/// invalidating. |
--- |
4481 |
/// invalidating. |
--- |
| 4482 |
static bool isAlwaysUnknown(const Instruction *I) { |
0 |
4482 |
static bool isAlwaysUnknown(const Instruction *I) { |
0 |
| 4483 |
switch (I->getOpcode()) { |
0 |
4483 |
switch (I->getOpcode()) { |
0 |
| 4484 |
case Instruction::Load: |
0 |
4484 |
case Instruction::Load: |
0 |
| 4485 |
return true; |
0 |
4485 |
return true; |
0 |
| 4486 |
default: |
0 |
4486 |
default: |
0 |
| 4487 |
return false; |
0 |
4487 |
return false; |
0 |
| 4488 |
} |
--- |
4488 |
} |
--- |
| 4489 |
} |
--- |
4489 |
} |
--- |
| 4490 |
|
--- |
4490 |
|
--- |
| 4491 |
/// Return an existing SCEV if it exists, otherwise analyze the expression and |
--- |
4491 |
/// Return an existing SCEV if it exists, otherwise analyze the expression and |
--- |
| 4492 |
/// create a new one. |
--- |
4492 |
/// create a new one. |
--- |
| 4493 |
const SCEV *ScalarEvolution::getSCEV(Value *V) { |
0 |
4493 |
const SCEV *ScalarEvolution::getSCEV(Value *V) { |
0 |
| 4494 |
assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
0 |
4494 |
assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
0 |
| 4495 |
|
--- |
4495 |
|
--- |
| 4496 |
if (const SCEV *S = getExistingSCEV(V)) |
0 |
4496 |
if (const SCEV *S = getExistingSCEV(V)) |
0 |
| 4497 |
return S; |
0 |
4497 |
return S; |
0 |
| 4498 |
const SCEV *S = createSCEVIter(V); |
0 |
4498 |
const SCEV *S = createSCEVIter(V); |
0 |
| 4499 |
assert((!isa(V) || !isAlwaysUnknown(cast(V)) || |
0 |
4499 |
assert((!isa(V) || !isAlwaysUnknown(cast(V)) || |
0 |
| 4500 |
isa(S)) && |
--- |
4500 |
isa(S)) && |
--- |
| 4501 |
"isAlwaysUnknown() instruction is not SCEVUnknown"); |
--- |
4501 |
"isAlwaysUnknown() instruction is not SCEVUnknown"); |
--- |
| 4502 |
return S; |
0 |
4502 |
return S; |
0 |
| 4503 |
} |
--- |
4503 |
} |
--- |
| 4504 |
|
--- |
4504 |
|
--- |
| 4505 |
const SCEV *ScalarEvolution::getExistingSCEV(Value *V) { |
0 |
4505 |
const SCEV *ScalarEvolution::getExistingSCEV(Value *V) { |
0 |
| 4506 |
assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
0 |
4506 |
assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
0 |
| 4507 |
|
--- |
4507 |
|
--- |
| 4508 |
ValueExprMapType::iterator I = ValueExprMap.find_as(V); |
0 |
4508 |
ValueExprMapType::iterator I = ValueExprMap.find_as(V); |
0 |
| 4509 |
if (I != ValueExprMap.end()) { |
0 |
4509 |
if (I != ValueExprMap.end()) { |
0 |
| 4510 |
const SCEV *S = I->second; |
0 |
4510 |
const SCEV *S = I->second; |
0 |
| 4511 |
assert(checkValidity(S) && |
0 |
4511 |
assert(checkValidity(S) && |
0 |
| 4512 |
"existing SCEV has not been properly invalidated"); |
--- |
4512 |
"existing SCEV has not been properly invalidated"); |
--- |
| 4513 |
return S; |
0 |
4513 |
return S; |
0 |
| 4514 |
} |
--- |
4514 |
} |
--- |
| 4515 |
return nullptr; |
0 |
4515 |
return nullptr; |
0 |
| 4516 |
} |
--- |
4516 |
} |
--- |
| 4517 |
|
--- |
4517 |
|
--- |
| 4518 |
/// Return a SCEV corresponding to -V = -1*V |
--- |
4518 |
/// Return a SCEV corresponding to -V = -1*V |
--- |
| 4519 |
const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V, |
0 |
4519 |
const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V, |
0 |
| 4520 |
SCEV::NoWrapFlags Flags) { |
--- |
4520 |
SCEV::NoWrapFlags Flags) { |
--- |
| 4521 |
if (const SCEVConstant *VC = dyn_cast(V)) |
0 |
4521 |
if (const SCEVConstant *VC = dyn_cast(V)) |
0 |
| 4522 |
return getConstant( |
0 |
4522 |
return getConstant( |
0 |
| 4523 |
cast(ConstantExpr::getNeg(VC->getValue()))); |
0 |
4523 |
cast(ConstantExpr::getNeg(VC->getValue()))); |
0 |
| 4524 |
|
--- |
4524 |
|
--- |
| 4525 |
Type *Ty = V->getType(); |
0 |
4525 |
Type *Ty = V->getType(); |
0 |
| 4526 |
Ty = getEffectiveSCEVType(Ty); |
0 |
4526 |
Ty = getEffectiveSCEVType(Ty); |
0 |
| 4527 |
return getMulExpr(V, getMinusOne(Ty), Flags); |
0 |
4527 |
return getMulExpr(V, getMinusOne(Ty), Flags); |
0 |
| 4528 |
} |
--- |
4528 |
} |
--- |
| 4529 |
|
--- |
4529 |
|
--- |
| 4530 |
/// If Expr computes ~A, return A else return nullptr |
--- |
4530 |
/// If Expr computes ~A, return A else return nullptr |
--- |
| 4531 |
static const SCEV *MatchNotExpr(const SCEV *Expr) { |
0 |
4531 |
static const SCEV *MatchNotExpr(const SCEV *Expr) { |
0 |
| 4532 |
const SCEVAddExpr *Add = dyn_cast(Expr); |
0 |
4532 |
const SCEVAddExpr *Add = dyn_cast(Expr); |
0 |
| 4533 |
if (!Add || Add->getNumOperands() != 2 || |
0 |
4533 |
if (!Add || Add->getNumOperands() != 2 || |
0 |
| 4534 |
!Add->getOperand(0)->isAllOnesValue()) |
0 |
4534 |
!Add->getOperand(0)->isAllOnesValue()) |
0 |
| 4535 |
return nullptr; |
0 |
4535 |
return nullptr; |
0 |
| 4536 |
|
--- |
4536 |
|
--- |
| 4537 |
const SCEVMulExpr *AddRHS = dyn_cast(Add->getOperand(1)); |
0 |
4537 |
const SCEVMulExpr *AddRHS = dyn_cast(Add->getOperand(1)); |
0 |
| 4538 |
if (!AddRHS || AddRHS->getNumOperands() != 2 || |
0 |
4538 |
if (!AddRHS || AddRHS->getNumOperands() != 2 || |
0 |
| 4539 |
!AddRHS->getOperand(0)->isAllOnesValue()) |
0 |
4539 |
!AddRHS->getOperand(0)->isAllOnesValue()) |
0 |
| 4540 |
return nullptr; |
0 |
4540 |
return nullptr; |
0 |
| 4541 |
|
--- |
4541 |
|
--- |
| 4542 |
return AddRHS->getOperand(1); |
0 |
4542 |
return AddRHS->getOperand(1); |
0 |
| 4543 |
} |
--- |
4543 |
} |
--- |
| 4544 |
|
--- |
4544 |
|
--- |
| 4545 |
/// Return a SCEV corresponding to ~V = -1-V |
--- |
4545 |
/// Return a SCEV corresponding to ~V = -1-V |
--- |
| 4546 |
const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) { |
0 |
4546 |
const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) { |
0 |
| 4547 |
assert(!V->getType()->isPointerTy() && "Can't negate pointer"); |
0 |
4547 |
assert(!V->getType()->isPointerTy() && "Can't negate pointer"); |
0 |
| 4548 |
|
--- |
4548 |
|
--- |
| 4549 |
if (const SCEVConstant *VC = dyn_cast(V)) |
0 |
4549 |
if (const SCEVConstant *VC = dyn_cast(V)) |
0 |
| 4550 |
return getConstant( |
0 |
4550 |
return getConstant( |
0 |
| 4551 |
cast(ConstantExpr::getNot(VC->getValue()))); |
0 |
4551 |
cast(ConstantExpr::getNot(VC->getValue()))); |
0 |
| 4552 |
|
--- |
4552 |
|
--- |
| 4553 |
// Fold ~(u|s)(min|max)(~x, ~y) to (u|s)(max|min)(x, y) |
--- |
4553 |
// Fold ~(u|s)(min|max)(~x, ~y) to (u|s)(max|min)(x, y) |
--- |
| 4554 |
if (const SCEVMinMaxExpr *MME = dyn_cast(V)) { |
0 |
4554 |
if (const SCEVMinMaxExpr *MME = dyn_cast(V)) { |
0 |
| 4555 |
auto MatchMinMaxNegation = [&](const SCEVMinMaxExpr *MME) { |
0 |
4555 |
auto MatchMinMaxNegation = [&](const SCEVMinMaxExpr *MME) { |
0 |
| 4556 |
SmallVector MatchedOperands; |
0 |
4556 |
SmallVector MatchedOperands; |
0 |
| 4557 |
for (const SCEV *Operand : MME->operands()) { |
0 |
4557 |
for (const SCEV *Operand : MME->operands()) { |
0 |
| 4558 |
const SCEV *Matched = MatchNotExpr(Operand); |
0 |
4558 |
const SCEV *Matched = MatchNotExpr(Operand); |
0 |
| 4559 |
if (!Matched) |
0 |
4559 |
if (!Matched) |
0 |
| 4560 |
return (const SCEV *)nullptr; |
0 |
4560 |
return (const SCEV *)nullptr; |
0 |
| 4561 |
MatchedOperands.push_back(Matched); |
0 |
4561 |
MatchedOperands.push_back(Matched); |
0 |
| 4562 |
} |
--- |
4562 |
} |
--- |
| 4563 |
return getMinMaxExpr(SCEVMinMaxExpr::negate(MME->getSCEVType()), |
0 |
4563 |
return getMinMaxExpr(SCEVMinMaxExpr::negate(MME->getSCEVType()), |
0 |
| 4564 |
MatchedOperands); |
0 |
4564 |
MatchedOperands); |
0 |
| 4565 |
}; |
0 |
4565 |
}; |
0 |
| 4566 |
if (const SCEV *Replaced = MatchMinMaxNegation(MME)) |
0 |
4566 |
if (const SCEV *Replaced = MatchMinMaxNegation(MME)) |
0 |
| 4567 |
return Replaced; |
0 |
4567 |
return Replaced; |
0 |
| 4568 |
} |
--- |
4568 |
} |
--- |
| 4569 |
|
--- |
4569 |
|
--- |
| 4570 |
Type *Ty = V->getType(); |
0 |
4570 |
Type *Ty = V->getType(); |
0 |
| 4571 |
Ty = getEffectiveSCEVType(Ty); |
0 |
4571 |
Ty = getEffectiveSCEVType(Ty); |
0 |
| 4572 |
return getMinusSCEV(getMinusOne(Ty), V); |
0 |
4572 |
return getMinusSCEV(getMinusOne(Ty), V); |
0 |
| 4573 |
} |
--- |
4573 |
} |
--- |
| 4574 |
|
--- |
4574 |
|
--- |
| 4575 |
const SCEV *ScalarEvolution::removePointerBase(const SCEV *P) { |
0 |
4575 |
const SCEV *ScalarEvolution::removePointerBase(const SCEV *P) { |
0 |
| 4576 |
assert(P->getType()->isPointerTy()); |
0 |
4576 |
assert(P->getType()->isPointerTy()); |
0 |
| 4577 |
|
--- |
4577 |
|
--- |
| 4578 |
if (auto *AddRec = dyn_cast(P)) { |
0 |
4578 |
if (auto *AddRec = dyn_cast(P)) { |
0 |
| 4579 |
// The base of an AddRec is the first operand. |
--- |
4579 |
// The base of an AddRec is the first operand. |
--- |
| 4580 |
SmallVector Ops{AddRec->operands()}; |
0 |
4580 |
SmallVector Ops{AddRec->operands()}; |
0 |
| 4581 |
Ops[0] = removePointerBase(Ops[0]); |
0 |
4581 |
Ops[0] = removePointerBase(Ops[0]); |
0 |
| 4582 |
// Don't try to transfer nowrap flags for now. We could in some cases |
--- |
4582 |
// Don't try to transfer nowrap flags for now. We could in some cases |
--- |
| 4583 |
// (for example, if pointer operand of the AddRec is a SCEVUnknown). |
--- |
4583 |
// (for example, if pointer operand of the AddRec is a SCEVUnknown). |
--- |
| 4584 |
return getAddRecExpr(Ops, AddRec->getLoop(), SCEV::FlagAnyWrap); |
0 |
4584 |
return getAddRecExpr(Ops, AddRec->getLoop(), SCEV::FlagAnyWrap); |
0 |
| 4585 |
} |
0 |
4585 |
} |
0 |
| 4586 |
if (auto *Add = dyn_cast(P)) { |
0 |
4586 |
if (auto *Add = dyn_cast(P)) { |
0 |
| 4587 |
// The base of an Add is the pointer operand. |
--- |
4587 |
// The base of an Add is the pointer operand. |
--- |
| 4588 |
SmallVector Ops{Add->operands()}; |
0 |
4588 |
SmallVector Ops{Add->operands()}; |
0 |
| 4589 |
const SCEV **PtrOp = nullptr; |
0 |
4589 |
const SCEV **PtrOp = nullptr; |
0 |
| 4590 |
for (const SCEV *&AddOp : Ops) { |
0 |
4590 |
for (const SCEV *&AddOp : Ops) { |
0 |
| 4591 |
if (AddOp->getType()->isPointerTy()) { |
0 |
4591 |
if (AddOp->getType()->isPointerTy()) { |
0 |
| 4592 |
assert(!PtrOp && "Cannot have multiple pointer ops"); |
0 |
4592 |
assert(!PtrOp && "Cannot have multiple pointer ops"); |
0 |
| 4593 |
PtrOp = &AddOp; |
0 |
4593 |
PtrOp = &AddOp; |
0 |
| 4594 |
} |
--- |
4594 |
} |
--- |
| 4595 |
} |
--- |
4595 |
} |
--- |
| 4596 |
*PtrOp = removePointerBase(*PtrOp); |
0 |
4596 |
*PtrOp = removePointerBase(*PtrOp); |
0 |
| 4597 |
// Don't try to transfer nowrap flags for now. We could in some cases |
--- |
4597 |
// Don't try to transfer nowrap flags for now. We could in some cases |
--- |
| 4598 |
// (for example, if the pointer operand of the Add is a SCEVUnknown). |
--- |
4598 |
// (for example, if the pointer operand of the Add is a SCEVUnknown). |
--- |
| 4599 |
return getAddExpr(Ops); |
0 |
4599 |
return getAddExpr(Ops); |
0 |
| 4600 |
} |
0 |
4600 |
} |
0 |
| 4601 |
// Any other expression must be a pointer base. |
--- |
4601 |
// Any other expression must be a pointer base. |
--- |
| 4602 |
return getZero(P->getType()); |
0 |
4602 |
return getZero(P->getType()); |
0 |
| 4603 |
} |
--- |
4603 |
} |
--- |
| 4604 |
|
--- |
4604 |
|
--- |
| 4605 |
const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS, |
0 |
4605 |
const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS, |
0 |
| 4606 |
SCEV::NoWrapFlags Flags, |
--- |
4606 |
SCEV::NoWrapFlags Flags, |
--- |
| 4607 |
unsigned Depth) { |
--- |
4607 |
unsigned Depth) { |
--- |
| 4608 |
// Fast path: X - X --> 0. |
--- |
4608 |
// Fast path: X - X --> 0. |
--- |
| 4609 |
if (LHS == RHS) |
0 |
4609 |
if (LHS == RHS) |
0 |
| 4610 |
return getZero(LHS->getType()); |
0 |
4610 |
return getZero(LHS->getType()); |
0 |
| 4611 |
|
--- |
4611 |
|
--- |
| 4612 |
// If we subtract two pointers with different pointer bases, bail. |
--- |
4612 |
// If we subtract two pointers with different pointer bases, bail. |
--- |
| 4613 |
// Eventually, we're going to add an assertion to getMulExpr that we |
--- |
4613 |
// Eventually, we're going to add an assertion to getMulExpr that we |
--- |
| 4614 |
// can't multiply by a pointer. |
--- |
4614 |
// can't multiply by a pointer. |
--- |
| 4615 |
if (RHS->getType()->isPointerTy()) { |
0 |
4615 |
if (RHS->getType()->isPointerTy()) { |
0 |
| 4616 |
if (!LHS->getType()->isPointerTy() || |
0 |
4616 |
if (!LHS->getType()->isPointerTy() || |
0 |
| 4617 |
getPointerBase(LHS) != getPointerBase(RHS)) |
0 |
4617 |
getPointerBase(LHS) != getPointerBase(RHS)) |
0 |
| 4618 |
return getCouldNotCompute(); |
0 |
4618 |
return getCouldNotCompute(); |
0 |
| 4619 |
LHS = removePointerBase(LHS); |
0 |
4619 |
LHS = removePointerBase(LHS); |
0 |
| 4620 |
RHS = removePointerBase(RHS); |
0 |
4620 |
RHS = removePointerBase(RHS); |
0 |
| 4621 |
} |
--- |
4621 |
} |
--- |
| 4622 |
|
--- |
4622 |
|
--- |
| 4623 |
// We represent LHS - RHS as LHS + (-1)*RHS. This transformation |
--- |
4623 |
// We represent LHS - RHS as LHS + (-1)*RHS. This transformation |
--- |
| 4624 |
// makes it so that we cannot make much use of NUW. |
--- |
4624 |
// makes it so that we cannot make much use of NUW. |
--- |
| 4625 |
auto AddFlags = SCEV::FlagAnyWrap; |
0 |
4625 |
auto AddFlags = SCEV::FlagAnyWrap; |
0 |
| 4626 |
const bool RHSIsNotMinSigned = |
--- |
4626 |
const bool RHSIsNotMinSigned = |
--- |
| 4627 |
!getSignedRangeMin(RHS).isMinSignedValue(); |
0 |
4627 |
!getSignedRangeMin(RHS).isMinSignedValue(); |
0 |
| 4628 |
if (hasFlags(Flags, SCEV::FlagNSW)) { |
0 |
4628 |
if (hasFlags(Flags, SCEV::FlagNSW)) { |
0 |
| 4629 |
// Let M be the minimum representable signed value. Then (-1)*RHS |
--- |
4629 |
// Let M be the minimum representable signed value. Then (-1)*RHS |
--- |
| 4630 |
// signed-wraps if and only if RHS is M. That can happen even for |
--- |
4630 |
// signed-wraps if and only if RHS is M. That can happen even for |
--- |
| 4631 |
// a NSW subtraction because e.g. (-1)*M signed-wraps even though |
--- |
4631 |
// a NSW subtraction because e.g. (-1)*M signed-wraps even though |
--- |
| 4632 |
// -1 - M does not. So to transfer NSW from LHS - RHS to LHS + |
--- |
4632 |
// -1 - M does not. So to transfer NSW from LHS - RHS to LHS + |
--- |
| 4633 |
// (-1)*RHS, we need to prove that RHS != M. |
--- |
4633 |
// (-1)*RHS, we need to prove that RHS != M. |
--- |
| 4634 |
// |
--- |
4634 |
// |
--- |
| 4635 |
// If LHS is non-negative and we know that LHS - RHS does not |
--- |
4635 |
// If LHS is non-negative and we know that LHS - RHS does not |
--- |
| 4636 |
// signed-wrap, then RHS cannot be M. So we can rule out signed-wrap |
--- |
4636 |
// signed-wrap, then RHS cannot be M. So we can rule out signed-wrap |
--- |
| 4637 |
// either by proving that RHS > M or that LHS >= 0. |
--- |
4637 |
// either by proving that RHS > M or that LHS >= 0. |
--- |
| 4638 |
if (RHSIsNotMinSigned || isKnownNonNegative(LHS)) { |
0 |
4638 |
if (RHSIsNotMinSigned || isKnownNonNegative(LHS)) { |
0 |
| 4639 |
AddFlags = SCEV::FlagNSW; |
0 |
4639 |
AddFlags = SCEV::FlagNSW; |
0 |
| 4640 |
} |
--- |
4640 |
} |
--- |
| 4641 |
} |
--- |
4641 |
} |
--- |
| 4642 |
|
--- |
4642 |
|
--- |
| 4643 |
// FIXME: Find a correct way to transfer NSW to (-1)*M when LHS - |
--- |
4643 |
// FIXME: Find a correct way to transfer NSW to (-1)*M when LHS - |
--- |
| 4644 |
// RHS is NSW and LHS >= 0. |
--- |
4644 |
// RHS is NSW and LHS >= 0. |
--- |
| 4645 |
// |
--- |
4645 |
// |
--- |
| 4646 |
// The difficulty here is that the NSW flag may have been proven |
--- |
4646 |
// The difficulty here is that the NSW flag may have been proven |
--- |
| 4647 |
// relative to a loop that is to be found in a recurrence in LHS and |
--- |
4647 |
// relative to a loop that is to be found in a recurrence in LHS and |
--- |
| 4648 |
// not in RHS. Applying NSW to (-1)*M may then let the NSW have a |
--- |
4648 |
// not in RHS. Applying NSW to (-1)*M may then let the NSW have a |
--- |
| 4649 |
// larger scope than intended. |
--- |
4649 |
// larger scope than intended. |
--- |
| 4650 |
auto NegFlags = RHSIsNotMinSigned ? SCEV::FlagNSW : SCEV::FlagAnyWrap; |
0 |
4650 |
auto NegFlags = RHSIsNotMinSigned ? SCEV::FlagNSW : SCEV::FlagAnyWrap; |
0 |
| 4651 |
|
--- |
4651 |
|
--- |
| 4652 |
return getAddExpr(LHS, getNegativeSCEV(RHS, NegFlags), AddFlags, Depth); |
0 |
4652 |
return getAddExpr(LHS, getNegativeSCEV(RHS, NegFlags), AddFlags, Depth); |
0 |
| 4653 |
} |
--- |
4653 |
} |
--- |
| 4654 |
|
--- |
4654 |
|
--- |
| 4655 |
const SCEV *ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, Type *Ty, |
0 |
4655 |
const SCEV *ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, Type *Ty, |
0 |
| 4656 |
unsigned Depth) { |
--- |
4656 |
unsigned Depth) { |
--- |
| 4657 |
Type *SrcTy = V->getType(); |
0 |
4657 |
Type *SrcTy = V->getType(); |
0 |
| 4658 |
assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
4658 |
assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
| 4659 |
"Cannot truncate or zero extend with non-integer arguments!"); |
--- |
4659 |
"Cannot truncate or zero extend with non-integer arguments!"); |
--- |
| 4660 |
if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
0 |
4660 |
if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
0 |
| 4661 |
return V; // No conversion |
0 |
4661 |
return V; // No conversion |
0 |
| 4662 |
if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
0 |
4662 |
if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
0 |
| 4663 |
return getTruncateExpr(V, Ty, Depth); |
0 |
4663 |
return getTruncateExpr(V, Ty, Depth); |
0 |
| 4664 |
return getZeroExtendExpr(V, Ty, Depth); |
0 |
4664 |
return getZeroExtendExpr(V, Ty, Depth); |
0 |
| 4665 |
} |
--- |
4665 |
} |
--- |
| 4666 |
|
--- |
4666 |
|
--- |
| 4667 |
const SCEV *ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, Type *Ty, |
0 |
4667 |
const SCEV *ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, Type *Ty, |
0 |
| 4668 |
unsigned Depth) { |
--- |
4668 |
unsigned Depth) { |
--- |
| 4669 |
Type *SrcTy = V->getType(); |
0 |
4669 |
Type *SrcTy = V->getType(); |
0 |
| 4670 |
assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
4670 |
assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
| 4671 |
"Cannot truncate or zero extend with non-integer arguments!"); |
--- |
4671 |
"Cannot truncate or zero extend with non-integer arguments!"); |
--- |
| 4672 |
if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
0 |
4672 |
if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
0 |
| 4673 |
return V; // No conversion |
0 |
4673 |
return V; // No conversion |
0 |
| 4674 |
if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
0 |
4674 |
if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
0 |
| 4675 |
return getTruncateExpr(V, Ty, Depth); |
0 |
4675 |
return getTruncateExpr(V, Ty, Depth); |
0 |
| 4676 |
return getSignExtendExpr(V, Ty, Depth); |
0 |
4676 |
return getSignExtendExpr(V, Ty, Depth); |
0 |
| 4677 |
} |
--- |
4677 |
} |
--- |
| 4678 |
|
--- |
4678 |
|
--- |
| 4679 |
const SCEV * |
--- |
4679 |
const SCEV * |
--- |
| 4680 |
ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, Type *Ty) { |
0 |
4680 |
ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, Type *Ty) { |
0 |
| 4681 |
Type *SrcTy = V->getType(); |
0 |
4681 |
Type *SrcTy = V->getType(); |
0 |
| 4682 |
assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
4682 |
assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
| 4683 |
"Cannot noop or zero extend with non-integer arguments!"); |
--- |
4683 |
"Cannot noop or zero extend with non-integer arguments!"); |
--- |
| 4684 |
assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
0 |
4684 |
assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
0 |
| 4685 |
"getNoopOrZeroExtend cannot truncate!"); |
--- |
4685 |
"getNoopOrZeroExtend cannot truncate!"); |
--- |
| 4686 |
if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
0 |
4686 |
if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
0 |
| 4687 |
return V; // No conversion |
0 |
4687 |
return V; // No conversion |
0 |
| 4688 |
return getZeroExtendExpr(V, Ty); |
0 |
4688 |
return getZeroExtendExpr(V, Ty); |
0 |
| 4689 |
} |
--- |
4689 |
} |
--- |
| 4690 |
|
--- |
4690 |
|
--- |
| 4691 |
const SCEV * |
--- |
4691 |
const SCEV * |
--- |
| 4692 |
ScalarEvolution::getNoopOrSignExtend(const SCEV *V, Type *Ty) { |
0 |
4692 |
ScalarEvolution::getNoopOrSignExtend(const SCEV *V, Type *Ty) { |
0 |
| 4693 |
Type *SrcTy = V->getType(); |
0 |
4693 |
Type *SrcTy = V->getType(); |
0 |
| 4694 |
assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
4694 |
assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
| 4695 |
"Cannot noop or sign extend with non-integer arguments!"); |
--- |
4695 |
"Cannot noop or sign extend with non-integer arguments!"); |
--- |
| 4696 |
assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
0 |
4696 |
assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
0 |
| 4697 |
"getNoopOrSignExtend cannot truncate!"); |
--- |
4697 |
"getNoopOrSignExtend cannot truncate!"); |
--- |
| 4698 |
if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
0 |
4698 |
if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
0 |
| 4699 |
return V; // No conversion |
0 |
4699 |
return V; // No conversion |
0 |
| 4700 |
return getSignExtendExpr(V, Ty); |
0 |
4700 |
return getSignExtendExpr(V, Ty); |
0 |
| 4701 |
} |
--- |
4701 |
} |
--- |
| 4702 |
|
--- |
4702 |
|
--- |
| 4703 |
const SCEV * |
--- |
4703 |
const SCEV * |
--- |
| 4704 |
ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, Type *Ty) { |
0 |
4704 |
ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, Type *Ty) { |
0 |
| 4705 |
Type *SrcTy = V->getType(); |
0 |
4705 |
Type *SrcTy = V->getType(); |
0 |
| 4706 |
assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
4706 |
assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
| 4707 |
"Cannot noop or any extend with non-integer arguments!"); |
--- |
4707 |
"Cannot noop or any extend with non-integer arguments!"); |
--- |
| 4708 |
assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
0 |
4708 |
assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
0 |
| 4709 |
"getNoopOrAnyExtend cannot truncate!"); |
--- |
4709 |
"getNoopOrAnyExtend cannot truncate!"); |
--- |
| 4710 |
if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
0 |
4710 |
if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
0 |
| 4711 |
return V; // No conversion |
0 |
4711 |
return V; // No conversion |
0 |
| 4712 |
return getAnyExtendExpr(V, Ty); |
0 |
4712 |
return getAnyExtendExpr(V, Ty); |
0 |
| 4713 |
} |
--- |
4713 |
} |
--- |
| 4714 |
|
--- |
4714 |
|
--- |
| 4715 |
const SCEV * |
--- |
4715 |
const SCEV * |
--- |
| 4716 |
ScalarEvolution::getTruncateOrNoop(const SCEV *V, Type *Ty) { |
0 |
4716 |
ScalarEvolution::getTruncateOrNoop(const SCEV *V, Type *Ty) { |
0 |
| 4717 |
Type *SrcTy = V->getType(); |
0 |
4717 |
Type *SrcTy = V->getType(); |
0 |
| 4718 |
assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
4718 |
assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && |
0 |
| 4719 |
"Cannot truncate or noop with non-integer arguments!"); |
--- |
4719 |
"Cannot truncate or noop with non-integer arguments!"); |
--- |
| 4720 |
assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && |
0 |
4720 |
assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && |
0 |
| 4721 |
"getTruncateOrNoop cannot extend!"); |
--- |
4721 |
"getTruncateOrNoop cannot extend!"); |
--- |
| 4722 |
if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
0 |
4722 |
if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
0 |
| 4723 |
return V; // No conversion |
0 |
4723 |
return V; // No conversion |
0 |
| 4724 |
return getTruncateExpr(V, Ty); |
0 |
4724 |
return getTruncateExpr(V, Ty); |
0 |
| 4725 |
} |
--- |
4725 |
} |
--- |
| 4726 |
|
--- |
4726 |
|
--- |
| 4727 |
const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, |
0 |
4727 |
const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, |
0 |
| 4728 |
const SCEV *RHS) { |
--- |
4728 |
const SCEV *RHS) { |
--- |
| 4729 |
const SCEV *PromotedLHS = LHS; |
0 |
4729 |
const SCEV *PromotedLHS = LHS; |
0 |
| 4730 |
const SCEV *PromotedRHS = RHS; |
0 |
4730 |
const SCEV *PromotedRHS = RHS; |
0 |
| 4731 |
|
--- |
4731 |
|
--- |
| 4732 |
if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
0 |
4732 |
if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
0 |
| 4733 |
PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
0 |
4733 |
PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
0 |
| 4734 |
else |
--- |
4734 |
else |
--- |
| 4735 |
PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
0 |
4735 |
PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
0 |
| 4736 |
|
--- |
4736 |
|
--- |
| 4737 |
return getUMaxExpr(PromotedLHS, PromotedRHS); |
0 |
4737 |
return getUMaxExpr(PromotedLHS, PromotedRHS); |
0 |
| 4738 |
} |
--- |
4738 |
} |
--- |
| 4739 |
|
--- |
4739 |
|
--- |
| 4740 |
const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, |
0 |
4740 |
const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, |
0 |
| 4741 |
const SCEV *RHS, |
--- |
4741 |
const SCEV *RHS, |
--- |
| 4742 |
bool Sequential) { |
--- |
4742 |
bool Sequential) { |
--- |
| 4743 |
SmallVector Ops = { LHS, RHS }; |
0 |
4743 |
SmallVector Ops = { LHS, RHS }; |
0 |
| 4744 |
return getUMinFromMismatchedTypes(Ops, Sequential); |
0 |
4744 |
return getUMinFromMismatchedTypes(Ops, Sequential); |
0 |
| 4745 |
} |
0 |
4745 |
} |
0 |
| 4746 |
|
--- |
4746 |
|
--- |
| 4747 |
const SCEV * |
--- |
4747 |
const SCEV * |
--- |
| 4748 |
ScalarEvolution::getUMinFromMismatchedTypes(SmallVectorImpl &Ops, |
0 |
4748 |
ScalarEvolution::getUMinFromMismatchedTypes(SmallVectorImpl &Ops, |
0 |
| 4749 |
bool Sequential) { |
--- |
4749 |
bool Sequential) { |
--- |
| 4750 |
assert(!Ops.empty() && "At least one operand must be!"); |
0 |
4750 |
assert(!Ops.empty() && "At least one operand must be!"); |
0 |
| 4751 |
// Trivial case. |
--- |
4751 |
// Trivial case. |
--- |
| 4752 |
if (Ops.size() == 1) |
0 |
4752 |
if (Ops.size() == 1) |
0 |
| 4753 |
return Ops[0]; |
0 |
4753 |
return Ops[0]; |
0 |
| 4754 |
|
--- |
4754 |
|
--- |
| 4755 |
// Find the max type first. |
--- |
4755 |
// Find the max type first. |
--- |
| 4756 |
Type *MaxType = nullptr; |
0 |
4756 |
Type *MaxType = nullptr; |
0 |
| 4757 |
for (const auto *S : Ops) |
0 |
4757 |
for (const auto *S : Ops) |
0 |
| 4758 |
if (MaxType) |
0 |
4758 |
if (MaxType) |
0 |
| 4759 |
MaxType = getWiderType(MaxType, S->getType()); |
0 |
4759 |
MaxType = getWiderType(MaxType, S->getType()); |
0 |
| 4760 |
else |
--- |
4760 |
else |
--- |
| 4761 |
MaxType = S->getType(); |
0 |
4761 |
MaxType = S->getType(); |
0 |
| 4762 |
assert(MaxType && "Failed to find maximum type!"); |
0 |
4762 |
assert(MaxType && "Failed to find maximum type!"); |
0 |
| 4763 |
|
--- |
4763 |
|
--- |
| 4764 |
// Extend all ops to max type. |
--- |
4764 |
// Extend all ops to max type. |
--- |
| 4765 |
SmallVector PromotedOps; |
0 |
4765 |
SmallVector PromotedOps; |
0 |
| 4766 |
for (const auto *S : Ops) |
0 |
4766 |
for (const auto *S : Ops) |
0 |
| 4767 |
PromotedOps.push_back(getNoopOrZeroExtend(S, MaxType)); |
0 |
4767 |
PromotedOps.push_back(getNoopOrZeroExtend(S, MaxType)); |
0 |
| 4768 |
|
--- |
4768 |
|
--- |
| 4769 |
// Generate umin. |
--- |
4769 |
// Generate umin. |
--- |
| 4770 |
return getUMinExpr(PromotedOps, Sequential); |
0 |
4770 |
return getUMinExpr(PromotedOps, Sequential); |
0 |
| 4771 |
} |
0 |
4771 |
} |
0 |
| 4772 |
|
--- |
4772 |
|
--- |
| 4773 |
const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) { |
0 |
4773 |
const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) { |
0 |
| 4774 |
// A pointer operand may evaluate to a nonpointer expression, such as null. |
--- |
4774 |
// A pointer operand may evaluate to a nonpointer expression, such as null. |
--- |
| 4775 |
if (!V->getType()->isPointerTy()) |
0 |
4775 |
if (!V->getType()->isPointerTy()) |
0 |
| 4776 |
return V; |
0 |
4776 |
return V; |
0 |
| 4777 |
|
--- |
4777 |
|
--- |
| 4778 |
while (true) { |
--- |
4778 |
while (true) { |
--- |
| 4779 |
if (auto *AddRec = dyn_cast(V)) { |
0 |
4779 |
if (auto *AddRec = dyn_cast(V)) { |
0 |
| 4780 |
V = AddRec->getStart(); |
0 |
4780 |
V = AddRec->getStart(); |
0 |
| 4781 |
} else if (auto *Add = dyn_cast(V)) { |
0 |
4781 |
} else if (auto *Add = dyn_cast(V)) { |
0 |
| 4782 |
const SCEV *PtrOp = nullptr; |
0 |
4782 |
const SCEV *PtrOp = nullptr; |
0 |
| 4783 |
for (const SCEV *AddOp : Add->operands()) { |
0 |
4783 |
for (const SCEV *AddOp : Add->operands()) { |
0 |
| 4784 |
if (AddOp->getType()->isPointerTy()) { |
0 |
4784 |
if (AddOp->getType()->isPointerTy()) { |
0 |
| 4785 |
assert(!PtrOp && "Cannot have multiple pointer ops"); |
0 |
4785 |
assert(!PtrOp && "Cannot have multiple pointer ops"); |
0 |
| 4786 |
PtrOp = AddOp; |
0 |
4786 |
PtrOp = AddOp; |
0 |
| 4787 |
} |
--- |
4787 |
} |
--- |
| 4788 |
} |
--- |
4788 |
} |
--- |
| 4789 |
assert(PtrOp && "Must have pointer op"); |
0 |
4789 |
assert(PtrOp && "Must have pointer op"); |
0 |
| 4790 |
V = PtrOp; |
0 |
4790 |
V = PtrOp; |
0 |
| 4791 |
} else // Not something we can look further into. |
--- |
4791 |
} else // Not something we can look further into. |
--- |
| 4792 |
return V; |
0 |
4792 |
return V; |
0 |
| 4793 |
} |
0 |
4793 |
} |
0 |
| 4794 |
} |
--- |
4794 |
} |
--- |
| 4795 |
|
--- |
4795 |
|
--- |
| 4796 |
/// Push users of the given Instruction onto the given Worklist. |
--- |
4796 |
/// Push users of the given Instruction onto the given Worklist. |
--- |
| 4797 |
static void PushDefUseChildren(Instruction *I, |
0 |
4797 |
static void PushDefUseChildren(Instruction *I, |
0 |
| 4798 |
SmallVectorImpl &Worklist, |
--- |
4798 |
SmallVectorImpl &Worklist, |
--- |
| 4799 |
SmallPtrSetImpl &Visited) { |
--- |
4799 |
SmallPtrSetImpl &Visited) { |
--- |
| 4800 |
// Push the def-use children onto the Worklist stack. |
--- |
4800 |
// Push the def-use children onto the Worklist stack. |
--- |
| 4801 |
for (User *U : I->users()) { |
0 |
4801 |
for (User *U : I->users()) { |
0 |
| 4802 |
auto *UserInsn = cast(U); |
0 |
4802 |
auto *UserInsn = cast(U); |
0 |
| 4803 |
if (isAlwaysUnknown(UserInsn)) |
0 |
4803 |
if (isAlwaysUnknown(UserInsn)) |
0 |
| 4804 |
continue; |
0 |
4804 |
continue; |
0 |
| 4805 |
if (Visited.insert(UserInsn).second) |
0 |
4805 |
if (Visited.insert(UserInsn).second) |
0 |
| 4806 |
Worklist.push_back(UserInsn); |
0 |
4806 |
Worklist.push_back(UserInsn); |
0 |
| 4807 |
} |
--- |
4807 |
} |
--- |
| 4808 |
} |
0 |
4808 |
} |
0 |
| 4809 |
|
--- |
4809 |
|
--- |
| 4810 |
namespace { |
--- |
4810 |
namespace { |
--- |
| 4811 |
|
--- |
4811 |
|
--- |
| 4812 |
/// Takes SCEV S and Loop L. For each AddRec sub-expression, use its start |
--- |
4812 |
/// Takes SCEV S and Loop L. For each AddRec sub-expression, use its start |
--- |
| 4813 |
/// expression in case its Loop is L. If it is not L then |
--- |
4813 |
/// expression in case its Loop is L. If it is not L then |
--- |
| 4814 |
/// if IgnoreOtherLoops is true then use AddRec itself |
--- |
4814 |
/// if IgnoreOtherLoops is true then use AddRec itself |
--- |
| 4815 |
/// otherwise rewrite cannot be done. |
--- |
4815 |
/// otherwise rewrite cannot be done. |
--- |
| 4816 |
/// If SCEV contains non-invariant unknown SCEV rewrite cannot be done. |
--- |
4816 |
/// If SCEV contains non-invariant unknown SCEV rewrite cannot be done. |
--- |
| 4817 |
class SCEVInitRewriter : public SCEVRewriteVisitor { |
--- |
4817 |
class SCEVInitRewriter : public SCEVRewriteVisitor { |
--- |
| 4818 |
public: |
--- |
4818 |
public: |
--- |
| 4819 |
static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE, |
0 |
4819 |
static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE, |
0 |
| 4820 |
bool IgnoreOtherLoops = true) { |
--- |
4820 |
bool IgnoreOtherLoops = true) { |
--- |
| 4821 |
SCEVInitRewriter Rewriter(L, SE); |
0 |
4821 |
SCEVInitRewriter Rewriter(L, SE); |
0 |
| 4822 |
const SCEV *Result = Rewriter.visit(S); |
0 |
4822 |
const SCEV *Result = Rewriter.visit(S); |
0 |
| 4823 |
if (Rewriter.hasSeenLoopVariantSCEVUnknown()) |
0 |
4823 |
if (Rewriter.hasSeenLoopVariantSCEVUnknown()) |
0 |
| 4824 |
return SE.getCouldNotCompute(); |
0 |
4824 |
return SE.getCouldNotCompute(); |
0 |
| 4825 |
return Rewriter.hasSeenOtherLoops() && !IgnoreOtherLoops |
0 |
4825 |
return Rewriter.hasSeenOtherLoops() && !IgnoreOtherLoops |
0 |
| 4826 |
? SE.getCouldNotCompute() |
0 |
4826 |
? SE.getCouldNotCompute() |
0 |
| 4827 |
: Result; |
0 |
4827 |
: Result; |
0 |
| 4828 |
} |
0 |
4828 |
} |
0 |
| 4829 |
|
--- |
4829 |
|
--- |
| 4830 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
4830 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
| 4831 |
if (!SE.isLoopInvariant(Expr, L)) |
0 |
4831 |
if (!SE.isLoopInvariant(Expr, L)) |
0 |
| 4832 |
SeenLoopVariantSCEVUnknown = true; |
0 |
4832 |
SeenLoopVariantSCEVUnknown = true; |
0 |
| 4833 |
return Expr; |
0 |
4833 |
return Expr; |
0 |
| 4834 |
} |
--- |
4834 |
} |
--- |
| 4835 |
|
--- |
4835 |
|
--- |
| 4836 |
const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { |
0 |
4836 |
const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { |
0 |
| 4837 |
// Only re-write AddRecExprs for this loop. |
--- |
4837 |
// Only re-write AddRecExprs for this loop. |
--- |
| 4838 |
if (Expr->getLoop() == L) |
0 |
4838 |
if (Expr->getLoop() == L) |
0 |
| 4839 |
return Expr->getStart(); |
0 |
4839 |
return Expr->getStart(); |
0 |
| 4840 |
SeenOtherLoops = true; |
0 |
4840 |
SeenOtherLoops = true; |
0 |
| 4841 |
return Expr; |
0 |
4841 |
return Expr; |
0 |
| 4842 |
} |
--- |
4842 |
} |
--- |
| 4843 |
|
--- |
4843 |
|
--- |
| 4844 |
bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; } |
0 |
4844 |
bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; } |
0 |
| 4845 |
|
--- |
4845 |
|
--- |
| 4846 |
bool hasSeenOtherLoops() { return SeenOtherLoops; } |
0 |
4846 |
bool hasSeenOtherLoops() { return SeenOtherLoops; } |
0 |
| 4847 |
|
--- |
4847 |
|
--- |
| 4848 |
private: |
--- |
4848 |
private: |
--- |
| 4849 |
explicit SCEVInitRewriter(const Loop *L, ScalarEvolution &SE) |
0 |
4849 |
explicit SCEVInitRewriter(const Loop *L, ScalarEvolution &SE) |
0 |
| 4850 |
: SCEVRewriteVisitor(SE), L(L) {} |
0 |
4850 |
: SCEVRewriteVisitor(SE), L(L) {} |
0 |
| 4851 |
|
--- |
4851 |
|
--- |
| 4852 |
const Loop *L; |
--- |
4852 |
const Loop *L; |
--- |
| 4853 |
bool SeenLoopVariantSCEVUnknown = false; |
--- |
4853 |
bool SeenLoopVariantSCEVUnknown = false; |
--- |
| 4854 |
bool SeenOtherLoops = false; |
--- |
4854 |
bool SeenOtherLoops = false; |
--- |
| 4855 |
}; |
--- |
4855 |
}; |
--- |
| 4856 |
|
--- |
4856 |
|
--- |
| 4857 |
/// Takes SCEV S and Loop L. For each AddRec sub-expression, use its post |
--- |
4857 |
/// Takes SCEV S and Loop L. For each AddRec sub-expression, use its post |
--- |
| 4858 |
/// increment expression in case its Loop is L. If it is not L then |
--- |
4858 |
/// increment expression in case its Loop is L. If it is not L then |
--- |
| 4859 |
/// use AddRec itself. |
--- |
4859 |
/// use AddRec itself. |
--- |
| 4860 |
/// If SCEV contains non-invariant unknown SCEV rewrite cannot be done. |
--- |
4860 |
/// If SCEV contains non-invariant unknown SCEV rewrite cannot be done. |
--- |
| 4861 |
class SCEVPostIncRewriter : public SCEVRewriteVisitor { |
--- |
4861 |
class SCEVPostIncRewriter : public SCEVRewriteVisitor { |
--- |
| 4862 |
public: |
--- |
4862 |
public: |
--- |
| 4863 |
static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE) { |
0 |
4863 |
static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE) { |
0 |
| 4864 |
SCEVPostIncRewriter Rewriter(L, SE); |
0 |
4864 |
SCEVPostIncRewriter Rewriter(L, SE); |
0 |
| 4865 |
const SCEV *Result = Rewriter.visit(S); |
0 |
4865 |
const SCEV *Result = Rewriter.visit(S); |
0 |
| 4866 |
return Rewriter.hasSeenLoopVariantSCEVUnknown() |
0 |
4866 |
return Rewriter.hasSeenLoopVariantSCEVUnknown() |
0 |
| 4867 |
? SE.getCouldNotCompute() |
0 |
4867 |
? SE.getCouldNotCompute() |
0 |
| 4868 |
: Result; |
0 |
4868 |
: Result; |
0 |
| 4869 |
} |
0 |
4869 |
} |
0 |
| 4870 |
|
--- |
4870 |
|
--- |
| 4871 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
4871 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
| 4872 |
if (!SE.isLoopInvariant(Expr, L)) |
0 |
4872 |
if (!SE.isLoopInvariant(Expr, L)) |
0 |
| 4873 |
SeenLoopVariantSCEVUnknown = true; |
0 |
4873 |
SeenLoopVariantSCEVUnknown = true; |
0 |
| 4874 |
return Expr; |
0 |
4874 |
return Expr; |
0 |
| 4875 |
} |
--- |
4875 |
} |
--- |
| 4876 |
|
--- |
4876 |
|
--- |
| 4877 |
const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { |
0 |
4877 |
const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { |
0 |
| 4878 |
// Only re-write AddRecExprs for this loop. |
--- |
4878 |
// Only re-write AddRecExprs for this loop. |
--- |
| 4879 |
if (Expr->getLoop() == L) |
0 |
4879 |
if (Expr->getLoop() == L) |
0 |
| 4880 |
return Expr->getPostIncExpr(SE); |
0 |
4880 |
return Expr->getPostIncExpr(SE); |
0 |
| 4881 |
SeenOtherLoops = true; |
0 |
4881 |
SeenOtherLoops = true; |
0 |
| 4882 |
return Expr; |
0 |
4882 |
return Expr; |
0 |
| 4883 |
} |
--- |
4883 |
} |
--- |
| 4884 |
|
--- |
4884 |
|
--- |
| 4885 |
bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; } |
0 |
4885 |
bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; } |
0 |
| 4886 |
|
--- |
4886 |
|
--- |
| 4887 |
bool hasSeenOtherLoops() { return SeenOtherLoops; } |
--- |
4887 |
bool hasSeenOtherLoops() { return SeenOtherLoops; } |
--- |
| 4888 |
|
--- |
4888 |
|
--- |
| 4889 |
private: |
--- |
4889 |
private: |
--- |
| 4890 |
explicit SCEVPostIncRewriter(const Loop *L, ScalarEvolution &SE) |
0 |
4890 |
explicit SCEVPostIncRewriter(const Loop *L, ScalarEvolution &SE) |
0 |
| 4891 |
: SCEVRewriteVisitor(SE), L(L) {} |
0 |
4891 |
: SCEVRewriteVisitor(SE), L(L) {} |
0 |
| 4892 |
|
--- |
4892 |
|
--- |
| 4893 |
const Loop *L; |
--- |
4893 |
const Loop *L; |
--- |
| 4894 |
bool SeenLoopVariantSCEVUnknown = false; |
--- |
4894 |
bool SeenLoopVariantSCEVUnknown = false; |
--- |
| 4895 |
bool SeenOtherLoops = false; |
--- |
4895 |
bool SeenOtherLoops = false; |
--- |
| 4896 |
}; |
--- |
4896 |
}; |
--- |
| 4897 |
|
--- |
4897 |
|
--- |
| 4898 |
/// This class evaluates the compare condition by matching it against the |
--- |
4898 |
/// This class evaluates the compare condition by matching it against the |
--- |
| 4899 |
/// condition of loop latch. If there is a match we assume a true value |
--- |
4899 |
/// condition of loop latch. If there is a match we assume a true value |
--- |
| 4900 |
/// for the condition while building SCEV nodes. |
--- |
4900 |
/// for the condition while building SCEV nodes. |
--- |
| 4901 |
class SCEVBackedgeConditionFolder |
--- |
4901 |
class SCEVBackedgeConditionFolder |
--- |
| 4902 |
: public SCEVRewriteVisitor { |
--- |
4902 |
: public SCEVRewriteVisitor { |
--- |
| 4903 |
public: |
--- |
4903 |
public: |
--- |
| 4904 |
static const SCEV *rewrite(const SCEV *S, const Loop *L, |
0 |
4904 |
static const SCEV *rewrite(const SCEV *S, const Loop *L, |
0 |
| 4905 |
ScalarEvolution &SE) { |
--- |
4905 |
ScalarEvolution &SE) { |
--- |
| 4906 |
bool IsPosBECond = false; |
0 |
4906 |
bool IsPosBECond = false; |
0 |
| 4907 |
Value *BECond = nullptr; |
0 |
4907 |
Value *BECond = nullptr; |
0 |
| 4908 |
if (BasicBlock *Latch = L->getLoopLatch()) { |
0 |
4908 |
if (BasicBlock *Latch = L->getLoopLatch()) { |
0 |
| 4909 |
BranchInst *BI = dyn_cast(Latch->getTerminator()); |
0 |
4909 |
BranchInst *BI = dyn_cast(Latch->getTerminator()); |
0 |
| 4910 |
if (BI && BI->isConditional()) { |
0 |
4910 |
if (BI && BI->isConditional()) { |
0 |
| 4911 |
assert(BI->getSuccessor(0) != BI->getSuccessor(1) && |
0 |
4911 |
assert(BI->getSuccessor(0) != BI->getSuccessor(1) && |
0 |
| 4912 |
"Both outgoing branches should not target same header!"); |
--- |
4912 |
"Both outgoing branches should not target same header!"); |
--- |
| 4913 |
BECond = BI->getCondition(); |
0 |
4913 |
BECond = BI->getCondition(); |
0 |
| 4914 |
IsPosBECond = BI->getSuccessor(0) == L->getHeader(); |
0 |
4914 |
IsPosBECond = BI->getSuccessor(0) == L->getHeader(); |
0 |
| 4915 |
} else { |
--- |
4915 |
} else { |
--- |
| 4916 |
return S; |
0 |
4916 |
return S; |
0 |
| 4917 |
} |
--- |
4917 |
} |
--- |
| 4918 |
} |
--- |
4918 |
} |
--- |
| 4919 |
SCEVBackedgeConditionFolder Rewriter(L, BECond, IsPosBECond, SE); |
0 |
4919 |
SCEVBackedgeConditionFolder Rewriter(L, BECond, IsPosBECond, SE); |
0 |
| 4920 |
return Rewriter.visit(S); |
0 |
4920 |
return Rewriter.visit(S); |
0 |
| 4921 |
} |
0 |
4921 |
} |
0 |
| 4922 |
|
--- |
4922 |
|
--- |
| 4923 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
4923 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
| 4924 |
const SCEV *Result = Expr; |
0 |
4924 |
const SCEV *Result = Expr; |
0 |
| 4925 |
bool InvariantF = SE.isLoopInvariant(Expr, L); |
0 |
4925 |
bool InvariantF = SE.isLoopInvariant(Expr, L); |
0 |
| 4926 |
|
--- |
4926 |
|
--- |
| 4927 |
if (!InvariantF) { |
0 |
4927 |
if (!InvariantF) { |
0 |
| 4928 |
Instruction *I = cast(Expr->getValue()); |
0 |
4928 |
Instruction *I = cast(Expr->getValue()); |
0 |
| 4929 |
switch (I->getOpcode()) { |
0 |
4929 |
switch (I->getOpcode()) { |
0 |
| 4930 |
case Instruction::Select: { |
0 |
4930 |
case Instruction::Select: { |
0 |
| 4931 |
SelectInst *SI = cast(I); |
0 |
4931 |
SelectInst *SI = cast(I); |
0 |
| 4932 |
std::optional Res = |
--- |
4932 |
std::optional Res = |
--- |
| 4933 |
compareWithBackedgeCondition(SI->getCondition()); |
0 |
4933 |
compareWithBackedgeCondition(SI->getCondition()); |
0 |
| 4934 |
if (Res) { |
0 |
4934 |
if (Res) { |
0 |
| 4935 |
bool IsOne = cast(*Res)->getValue()->isOne(); |
0 |
4935 |
bool IsOne = cast(*Res)->getValue()->isOne(); |
0 |
| 4936 |
Result = SE.getSCEV(IsOne ? SI->getTrueValue() : SI->getFalseValue()); |
0 |
4936 |
Result = SE.getSCEV(IsOne ? SI->getTrueValue() : SI->getFalseValue()); |
0 |
| 4937 |
} |
--- |
4937 |
} |
--- |
| 4938 |
break; |
0 |
4938 |
break; |
0 |
| 4939 |
} |
--- |
4939 |
} |
--- |
| 4940 |
default: { |
0 |
4940 |
default: { |
0 |
| 4941 |
std::optional Res = compareWithBackedgeCondition(I); |
0 |
4941 |
std::optional Res = compareWithBackedgeCondition(I); |
0 |
| 4942 |
if (Res) |
0 |
4942 |
if (Res) |
0 |
| 4943 |
Result = *Res; |
0 |
4943 |
Result = *Res; |
0 |
| 4944 |
break; |
0 |
4944 |
break; |
0 |
| 4945 |
} |
--- |
4945 |
} |
--- |
| 4946 |
} |
--- |
4946 |
} |
--- |
| 4947 |
} |
--- |
4947 |
} |
--- |
| 4948 |
return Result; |
0 |
4948 |
return Result; |
0 |
| 4949 |
} |
--- |
4949 |
} |
--- |
| 4950 |
|
--- |
4950 |
|
--- |
| 4951 |
private: |
--- |
4951 |
private: |
--- |
| 4952 |
explicit SCEVBackedgeConditionFolder(const Loop *L, Value *BECond, |
0 |
4952 |
explicit SCEVBackedgeConditionFolder(const Loop *L, Value *BECond, |
0 |
| 4953 |
bool IsPosBECond, ScalarEvolution &SE) |
--- |
4953 |
bool IsPosBECond, ScalarEvolution &SE) |
--- |
| 4954 |
: SCEVRewriteVisitor(SE), L(L), BackedgeCond(BECond), |
0 |
4954 |
: SCEVRewriteVisitor(SE), L(L), BackedgeCond(BECond), |
0 |
| 4955 |
IsPositiveBECond(IsPosBECond) {} |
0 |
4955 |
IsPositiveBECond(IsPosBECond) {} |
0 |
| 4956 |
|
--- |
4956 |
|
--- |
| 4957 |
std::optional compareWithBackedgeCondition(Value *IC); |
--- |
4957 |
std::optional compareWithBackedgeCondition(Value *IC); |
--- |
| 4958 |
|
--- |
4958 |
|
--- |
| 4959 |
const Loop *L; |
--- |
4959 |
const Loop *L; |
--- |
| 4960 |
/// Loop back condition. |
--- |
4960 |
/// Loop back condition. |
--- |
| 4961 |
Value *BackedgeCond = nullptr; |
--- |
4961 |
Value *BackedgeCond = nullptr; |
--- |
| 4962 |
/// Set to true if loop back is on positive branch condition. |
--- |
4962 |
/// Set to true if loop back is on positive branch condition. |
--- |
| 4963 |
bool IsPositiveBECond; |
--- |
4963 |
bool IsPositiveBECond; |
--- |
| 4964 |
}; |
--- |
4964 |
}; |
--- |
| 4965 |
|
--- |
4965 |
|
--- |
| 4966 |
std::optional |
--- |
4966 |
std::optional |
--- |
| 4967 |
SCEVBackedgeConditionFolder::compareWithBackedgeCondition(Value *IC) { |
0 |
4967 |
SCEVBackedgeConditionFolder::compareWithBackedgeCondition(Value *IC) { |
0 |
| 4968 |
|
--- |
4968 |
|
--- |
| 4969 |
// If value matches the backedge condition for loop latch, |
--- |
4969 |
// If value matches the backedge condition for loop latch, |
--- |
| 4970 |
// then return a constant evolution node based on loopback |
--- |
4970 |
// then return a constant evolution node based on loopback |
--- |
| 4971 |
// branch taken. |
--- |
4971 |
// branch taken. |
--- |
| 4972 |
if (BackedgeCond == IC) |
0 |
4972 |
if (BackedgeCond == IC) |
0 |
| 4973 |
return IsPositiveBECond ? SE.getOne(Type::getInt1Ty(SE.getContext())) |
0 |
4973 |
return IsPositiveBECond ? SE.getOne(Type::getInt1Ty(SE.getContext())) |
0 |
| 4974 |
: SE.getZero(Type::getInt1Ty(SE.getContext())); |
0 |
4974 |
: SE.getZero(Type::getInt1Ty(SE.getContext())); |
0 |
| 4975 |
return std::nullopt; |
0 |
4975 |
return std::nullopt; |
0 |
| 4976 |
} |
--- |
4976 |
} |
--- |
| 4977 |
|
--- |
4977 |
|
--- |
| 4978 |
class SCEVShiftRewriter : public SCEVRewriteVisitor { |
--- |
4978 |
class SCEVShiftRewriter : public SCEVRewriteVisitor { |
--- |
| 4979 |
public: |
--- |
4979 |
public: |
--- |
| 4980 |
static const SCEV *rewrite(const SCEV *S, const Loop *L, |
0 |
4980 |
static const SCEV *rewrite(const SCEV *S, const Loop *L, |
0 |
| 4981 |
ScalarEvolution &SE) { |
--- |
4981 |
ScalarEvolution &SE) { |
--- |
| 4982 |
SCEVShiftRewriter Rewriter(L, SE); |
0 |
4982 |
SCEVShiftRewriter Rewriter(L, SE); |
0 |
| 4983 |
const SCEV *Result = Rewriter.visit(S); |
0 |
4983 |
const SCEV *Result = Rewriter.visit(S); |
0 |
| 4984 |
return Rewriter.isValid() ? Result : SE.getCouldNotCompute(); |
0 |
4984 |
return Rewriter.isValid() ? Result : SE.getCouldNotCompute(); |
0 |
| 4985 |
} |
0 |
4985 |
} |
0 |
| 4986 |
|
--- |
4986 |
|
--- |
| 4987 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
4987 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
| 4988 |
// Only allow AddRecExprs for this loop. |
--- |
4988 |
// Only allow AddRecExprs for this loop. |
--- |
| 4989 |
if (!SE.isLoopInvariant(Expr, L)) |
0 |
4989 |
if (!SE.isLoopInvariant(Expr, L)) |
0 |
| 4990 |
Valid = false; |
0 |
4990 |
Valid = false; |
0 |
| 4991 |
return Expr; |
0 |
4991 |
return Expr; |
0 |
| 4992 |
} |
--- |
4992 |
} |
--- |
| 4993 |
|
--- |
4993 |
|
--- |
| 4994 |
const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { |
0 |
4994 |
const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { |
0 |
| 4995 |
if (Expr->getLoop() == L && Expr->isAffine()) |
0 |
4995 |
if (Expr->getLoop() == L && Expr->isAffine()) |
0 |
| 4996 |
return SE.getMinusSCEV(Expr, Expr->getStepRecurrence(SE)); |
0 |
4996 |
return SE.getMinusSCEV(Expr, Expr->getStepRecurrence(SE)); |
0 |
| 4997 |
Valid = false; |
0 |
4997 |
Valid = false; |
0 |
| 4998 |
return Expr; |
0 |
4998 |
return Expr; |
0 |
| 4999 |
} |
--- |
4999 |
} |
--- |
| 5000 |
|
--- |
5000 |
|
--- |
| 5001 |
bool isValid() { return Valid; } |
0 |
5001 |
bool isValid() { return Valid; } |
0 |
| 5002 |
|
--- |
5002 |
|
--- |
| 5003 |
private: |
--- |
5003 |
private: |
--- |
| 5004 |
explicit SCEVShiftRewriter(const Loop *L, ScalarEvolution &SE) |
0 |
5004 |
explicit SCEVShiftRewriter(const Loop *L, ScalarEvolution &SE) |
0 |
| 5005 |
: SCEVRewriteVisitor(SE), L(L) {} |
0 |
5005 |
: SCEVRewriteVisitor(SE), L(L) {} |
0 |
| 5006 |
|
--- |
5006 |
|
--- |
| 5007 |
const Loop *L; |
--- |
5007 |
const Loop *L; |
--- |
| 5008 |
bool Valid = true; |
--- |
5008 |
bool Valid = true; |
--- |
| 5009 |
}; |
--- |
5009 |
}; |
--- |
| 5010 |
|
--- |
5010 |
|
--- |
| 5011 |
} // end anonymous namespace |
--- |
5011 |
} // end anonymous namespace |
--- |
| 5012 |
|
--- |
5012 |
|
--- |
| 5013 |
SCEV::NoWrapFlags |
--- |
5013 |
SCEV::NoWrapFlags |
--- |
| 5014 |
ScalarEvolution::proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR) { |
0 |
5014 |
ScalarEvolution::proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR) { |
0 |
| 5015 |
if (!AR->isAffine()) |
0 |
5015 |
if (!AR->isAffine()) |
0 |
| 5016 |
return SCEV::FlagAnyWrap; |
0 |
5016 |
return SCEV::FlagAnyWrap; |
0 |
| 5017 |
|
--- |
5017 |
|
--- |
| 5018 |
using OBO = OverflowingBinaryOperator; |
--- |
5018 |
using OBO = OverflowingBinaryOperator; |
--- |
| 5019 |
|
--- |
5019 |
|
--- |
| 5020 |
SCEV::NoWrapFlags Result = SCEV::FlagAnyWrap; |
0 |
5020 |
SCEV::NoWrapFlags Result = SCEV::FlagAnyWrap; |
0 |
| 5021 |
|
--- |
5021 |
|
--- |
| 5022 |
if (!AR->hasNoSelfWrap()) { |
0 |
5022 |
if (!AR->hasNoSelfWrap()) { |
0 |
| 5023 |
const SCEV *BECount = getConstantMaxBackedgeTakenCount(AR->getLoop()); |
0 |
5023 |
const SCEV *BECount = getConstantMaxBackedgeTakenCount(AR->getLoop()); |
0 |
| 5024 |
if (const SCEVConstant *BECountMax = dyn_cast(BECount)) { |
0 |
5024 |
if (const SCEVConstant *BECountMax = dyn_cast(BECount)) { |
0 |
| 5025 |
ConstantRange StepCR = getSignedRange(AR->getStepRecurrence(*this)); |
0 |
5025 |
ConstantRange StepCR = getSignedRange(AR->getStepRecurrence(*this)); |
0 |
| 5026 |
const APInt &BECountAP = BECountMax->getAPInt(); |
0 |
5026 |
const APInt &BECountAP = BECountMax->getAPInt(); |
0 |
| 5027 |
unsigned NoOverflowBitWidth = |
--- |
5027 |
unsigned NoOverflowBitWidth = |
--- |
| 5028 |
BECountAP.getActiveBits() + StepCR.getMinSignedBits(); |
0 |
5028 |
BECountAP.getActiveBits() + StepCR.getMinSignedBits(); |
0 |
| 5029 |
if (NoOverflowBitWidth <= getTypeSizeInBits(AR->getType())) |
0 |
5029 |
if (NoOverflowBitWidth <= getTypeSizeInBits(AR->getType())) |
0 |
| 5030 |
Result = ScalarEvolution::setFlags(Result, SCEV::FlagNW); |
0 |
5030 |
Result = ScalarEvolution::setFlags(Result, SCEV::FlagNW); |
0 |
| 5031 |
} |
0 |
5031 |
} |
0 |
| 5032 |
} |
--- |
5032 |
} |
--- |
| 5033 |
|
--- |
5033 |
|
--- |
| 5034 |
if (!AR->hasNoSignedWrap()) { |
0 |
5034 |
if (!AR->hasNoSignedWrap()) { |
0 |
| 5035 |
ConstantRange AddRecRange = getSignedRange(AR); |
0 |
5035 |
ConstantRange AddRecRange = getSignedRange(AR); |
0 |
| 5036 |
ConstantRange IncRange = getSignedRange(AR->getStepRecurrence(*this)); |
0 |
5036 |
ConstantRange IncRange = getSignedRange(AR->getStepRecurrence(*this)); |
0 |
| 5037 |
|
--- |
5037 |
|
--- |
| 5038 |
auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion( |
--- |
5038 |
auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion( |
--- |
| 5039 |
Instruction::Add, IncRange, OBO::NoSignedWrap); |
0 |
5039 |
Instruction::Add, IncRange, OBO::NoSignedWrap); |
0 |
| 5040 |
if (NSWRegion.contains(AddRecRange)) |
0 |
5040 |
if (NSWRegion.contains(AddRecRange)) |
0 |
| 5041 |
Result = ScalarEvolution::setFlags(Result, SCEV::FlagNSW); |
0 |
5041 |
Result = ScalarEvolution::setFlags(Result, SCEV::FlagNSW); |
0 |
| 5042 |
} |
0 |
5042 |
} |
0 |
| 5043 |
|
--- |
5043 |
|
--- |
| 5044 |
if (!AR->hasNoUnsignedWrap()) { |
0 |
5044 |
if (!AR->hasNoUnsignedWrap()) { |
0 |
| 5045 |
ConstantRange AddRecRange = getUnsignedRange(AR); |
0 |
5045 |
ConstantRange AddRecRange = getUnsignedRange(AR); |
0 |
| 5046 |
ConstantRange IncRange = getUnsignedRange(AR->getStepRecurrence(*this)); |
0 |
5046 |
ConstantRange IncRange = getUnsignedRange(AR->getStepRecurrence(*this)); |
0 |
| 5047 |
|
--- |
5047 |
|
--- |
| 5048 |
auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion( |
--- |
5048 |
auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion( |
--- |
| 5049 |
Instruction::Add, IncRange, OBO::NoUnsignedWrap); |
0 |
5049 |
Instruction::Add, IncRange, OBO::NoUnsignedWrap); |
0 |
| 5050 |
if (NUWRegion.contains(AddRecRange)) |
0 |
5050 |
if (NUWRegion.contains(AddRecRange)) |
0 |
| 5051 |
Result = ScalarEvolution::setFlags(Result, SCEV::FlagNUW); |
0 |
5051 |
Result = ScalarEvolution::setFlags(Result, SCEV::FlagNUW); |
0 |
| 5052 |
} |
0 |
5052 |
} |
0 |
| 5053 |
|
--- |
5053 |
|
--- |
| 5054 |
return Result; |
0 |
5054 |
return Result; |
0 |
| 5055 |
} |
--- |
5055 |
} |
--- |
| 5056 |
|
--- |
5056 |
|
--- |
| 5057 |
SCEV::NoWrapFlags |
--- |
5057 |
SCEV::NoWrapFlags |
--- |
| 5058 |
ScalarEvolution::proveNoSignedWrapViaInduction(const SCEVAddRecExpr *AR) { |
0 |
5058 |
ScalarEvolution::proveNoSignedWrapViaInduction(const SCEVAddRecExpr *AR) { |
0 |
| 5059 |
SCEV::NoWrapFlags Result = AR->getNoWrapFlags(); |
0 |
5059 |
SCEV::NoWrapFlags Result = AR->getNoWrapFlags(); |
0 |
| 5060 |
|
--- |
5060 |
|
--- |
| 5061 |
if (AR->hasNoSignedWrap()) |
0 |
5061 |
if (AR->hasNoSignedWrap()) |
0 |
| 5062 |
return Result; |
0 |
5062 |
return Result; |
0 |
| 5063 |
|
--- |
5063 |
|
--- |
| 5064 |
if (!AR->isAffine()) |
0 |
5064 |
if (!AR->isAffine()) |
0 |
| 5065 |
return Result; |
0 |
5065 |
return Result; |
0 |
| 5066 |
|
--- |
5066 |
|
--- |
| 5067 |
// This function can be expensive, only try to prove NSW once per AddRec. |
--- |
5067 |
// This function can be expensive, only try to prove NSW once per AddRec. |
--- |
| 5068 |
if (!SignedWrapViaInductionTried.insert(AR).second) |
0 |
5068 |
if (!SignedWrapViaInductionTried.insert(AR).second) |
0 |
| 5069 |
return Result; |
0 |
5069 |
return Result; |
0 |
| 5070 |
|
--- |
5070 |
|
--- |
| 5071 |
const SCEV *Step = AR->getStepRecurrence(*this); |
0 |
5071 |
const SCEV *Step = AR->getStepRecurrence(*this); |
0 |
| 5072 |
const Loop *L = AR->getLoop(); |
0 |
5072 |
const Loop *L = AR->getLoop(); |
0 |
| 5073 |
|
--- |
5073 |
|
--- |
| 5074 |
// Check whether the backedge-taken count is SCEVCouldNotCompute. |
--- |
5074 |
// Check whether the backedge-taken count is SCEVCouldNotCompute. |
--- |
| 5075 |
// Note that this serves two purposes: It filters out loops that are |
--- |
5075 |
// Note that this serves two purposes: It filters out loops that are |
--- |
| 5076 |
// simply not analyzable, and it covers the case where this code is |
--- |
5076 |
// simply not analyzable, and it covers the case where this code is |
--- |
| 5077 |
// being called from within backedge-taken count analysis, such that |
--- |
5077 |
// being called from within backedge-taken count analysis, such that |
--- |
| 5078 |
// attempting to ask for the backedge-taken count would likely result |
--- |
5078 |
// attempting to ask for the backedge-taken count would likely result |
--- |
| 5079 |
// in infinite recursion. In the later case, the analysis code will |
--- |
5079 |
// in infinite recursion. In the later case, the analysis code will |
--- |
| 5080 |
// cope with a conservative value, and it will take care to purge |
--- |
5080 |
// cope with a conservative value, and it will take care to purge |
--- |
| 5081 |
// that value once it has finished. |
--- |
5081 |
// that value once it has finished. |
--- |
| 5082 |
const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L); |
0 |
5082 |
const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L); |
0 |
| 5083 |
|
--- |
5083 |
|
--- |
| 5084 |
// Normally, in the cases we can prove no-overflow via a |
--- |
5084 |
// Normally, in the cases we can prove no-overflow via a |
--- |
| 5085 |
// backedge guarding condition, we can also compute a backedge |
--- |
5085 |
// backedge guarding condition, we can also compute a backedge |
--- |
| 5086 |
// taken count for the loop. The exceptions are assumptions and |
--- |
5086 |
// taken count for the loop. The exceptions are assumptions and |
--- |
| 5087 |
// guards present in the loop -- SCEV is not great at exploiting |
--- |
5087 |
// guards present in the loop -- SCEV is not great at exploiting |
--- |
| 5088 |
// these to compute max backedge taken counts, but can still use |
--- |
5088 |
// these to compute max backedge taken counts, but can still use |
--- |
| 5089 |
// these to prove lack of overflow. Use this fact to avoid |
--- |
5089 |
// these to prove lack of overflow. Use this fact to avoid |
--- |
| 5090 |
// doing extra work that may not pay off. |
--- |
5090 |
// doing extra work that may not pay off. |
--- |
| 5091 |
|
--- |
5091 |
|
--- |
| 5092 |
if (isa(MaxBECount) && !HasGuards && |
0 |
5092 |
if (isa(MaxBECount) && !HasGuards && |
0 |
| 5093 |
AC.assumptions().empty()) |
0 |
5093 |
AC.assumptions().empty()) |
0 |
| 5094 |
return Result; |
0 |
5094 |
return Result; |
0 |
| 5095 |
|
--- |
5095 |
|
--- |
| 5096 |
// If the backedge is guarded by a comparison with the pre-inc value the |
--- |
5096 |
// If the backedge is guarded by a comparison with the pre-inc value the |
--- |
| 5097 |
// addrec is safe. Also, if the entry is guarded by a comparison with the |
--- |
5097 |
// addrec is safe. Also, if the entry is guarded by a comparison with the |
--- |
| 5098 |
// start value and the backedge is guarded by a comparison with the post-inc |
--- |
5098 |
// start value and the backedge is guarded by a comparison with the post-inc |
--- |
| 5099 |
// value, the addrec is safe. |
--- |
5099 |
// value, the addrec is safe. |
--- |
| 5100 |
ICmpInst::Predicate Pred; |
--- |
5100 |
ICmpInst::Predicate Pred; |
--- |
| 5101 |
const SCEV *OverflowLimit = |
--- |
5101 |
const SCEV *OverflowLimit = |
--- |
| 5102 |
getSignedOverflowLimitForStep(Step, &Pred, this); |
0 |
5102 |
getSignedOverflowLimitForStep(Step, &Pred, this); |
0 |
| 5103 |
if (OverflowLimit && |
0 |
5103 |
if (OverflowLimit && |
0 |
| 5104 |
(isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) || |
0 |
5104 |
(isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) || |
0 |
| 5105 |
isKnownOnEveryIteration(Pred, AR, OverflowLimit))) { |
0 |
5105 |
isKnownOnEveryIteration(Pred, AR, OverflowLimit))) { |
0 |
| 5106 |
Result = setFlags(Result, SCEV::FlagNSW); |
0 |
5106 |
Result = setFlags(Result, SCEV::FlagNSW); |
0 |
| 5107 |
} |
--- |
5107 |
} |
--- |
| 5108 |
return Result; |
0 |
5108 |
return Result; |
0 |
| 5109 |
} |
--- |
5109 |
} |
--- |
| 5110 |
SCEV::NoWrapFlags |
--- |
5110 |
SCEV::NoWrapFlags |
--- |
| 5111 |
ScalarEvolution::proveNoUnsignedWrapViaInduction(const SCEVAddRecExpr *AR) { |
0 |
5111 |
ScalarEvolution::proveNoUnsignedWrapViaInduction(const SCEVAddRecExpr *AR) { |
0 |
| 5112 |
SCEV::NoWrapFlags Result = AR->getNoWrapFlags(); |
0 |
5112 |
SCEV::NoWrapFlags Result = AR->getNoWrapFlags(); |
0 |
| 5113 |
|
--- |
5113 |
|
--- |
| 5114 |
if (AR->hasNoUnsignedWrap()) |
0 |
5114 |
if (AR->hasNoUnsignedWrap()) |
0 |
| 5115 |
return Result; |
0 |
5115 |
return Result; |
0 |
| 5116 |
|
--- |
5116 |
|
--- |
| 5117 |
if (!AR->isAffine()) |
0 |
5117 |
if (!AR->isAffine()) |
0 |
| 5118 |
return Result; |
0 |
5118 |
return Result; |
0 |
| 5119 |
|
--- |
5119 |
|
--- |
| 5120 |
// This function can be expensive, only try to prove NUW once per AddRec. |
--- |
5120 |
// This function can be expensive, only try to prove NUW once per AddRec. |
--- |
| 5121 |
if (!UnsignedWrapViaInductionTried.insert(AR).second) |
0 |
5121 |
if (!UnsignedWrapViaInductionTried.insert(AR).second) |
0 |
| 5122 |
return Result; |
0 |
5122 |
return Result; |
0 |
| 5123 |
|
--- |
5123 |
|
--- |
| 5124 |
const SCEV *Step = AR->getStepRecurrence(*this); |
0 |
5124 |
const SCEV *Step = AR->getStepRecurrence(*this); |
0 |
| 5125 |
unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
0 |
5125 |
unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
0 |
| 5126 |
const Loop *L = AR->getLoop(); |
0 |
5126 |
const Loop *L = AR->getLoop(); |
0 |
| 5127 |
|
--- |
5127 |
|
--- |
| 5128 |
// Check whether the backedge-taken count is SCEVCouldNotCompute. |
--- |
5128 |
// Check whether the backedge-taken count is SCEVCouldNotCompute. |
--- |
| 5129 |
// Note that this serves two purposes: It filters out loops that are |
--- |
5129 |
// Note that this serves two purposes: It filters out loops that are |
--- |
| 5130 |
// simply not analyzable, and it covers the case where this code is |
--- |
5130 |
// simply not analyzable, and it covers the case where this code is |
--- |
| 5131 |
// being called from within backedge-taken count analysis, such that |
--- |
5131 |
// being called from within backedge-taken count analysis, such that |
--- |
| 5132 |
// attempting to ask for the backedge-taken count would likely result |
--- |
5132 |
// attempting to ask for the backedge-taken count would likely result |
--- |
| 5133 |
// in infinite recursion. In the later case, the analysis code will |
--- |
5133 |
// in infinite recursion. In the later case, the analysis code will |
--- |
| 5134 |
// cope with a conservative value, and it will take care to purge |
--- |
5134 |
// cope with a conservative value, and it will take care to purge |
--- |
| 5135 |
// that value once it has finished. |
--- |
5135 |
// that value once it has finished. |
--- |
| 5136 |
const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L); |
0 |
5136 |
const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L); |
0 |
| 5137 |
|
--- |
5137 |
|
--- |
| 5138 |
// Normally, in the cases we can prove no-overflow via a |
--- |
5138 |
// Normally, in the cases we can prove no-overflow via a |
--- |
| 5139 |
// backedge guarding condition, we can also compute a backedge |
--- |
5139 |
// backedge guarding condition, we can also compute a backedge |
--- |
| 5140 |
// taken count for the loop. The exceptions are assumptions and |
--- |
5140 |
// taken count for the loop. The exceptions are assumptions and |
--- |
| 5141 |
// guards present in the loop -- SCEV is not great at exploiting |
--- |
5141 |
// guards present in the loop -- SCEV is not great at exploiting |
--- |
| 5142 |
// these to compute max backedge taken counts, but can still use |
--- |
5142 |
// these to compute max backedge taken counts, but can still use |
--- |
| 5143 |
// these to prove lack of overflow. Use this fact to avoid |
--- |
5143 |
// these to prove lack of overflow. Use this fact to avoid |
--- |
| 5144 |
// doing extra work that may not pay off. |
--- |
5144 |
// doing extra work that may not pay off. |
--- |
| 5145 |
|
--- |
5145 |
|
--- |
| 5146 |
if (isa(MaxBECount) && !HasGuards && |
0 |
5146 |
if (isa(MaxBECount) && !HasGuards && |
0 |
| 5147 |
AC.assumptions().empty()) |
0 |
5147 |
AC.assumptions().empty()) |
0 |
| 5148 |
return Result; |
0 |
5148 |
return Result; |
0 |
| 5149 |
|
--- |
5149 |
|
--- |
| 5150 |
// If the backedge is guarded by a comparison with the pre-inc value the |
--- |
5150 |
// If the backedge is guarded by a comparison with the pre-inc value the |
--- |
| 5151 |
// addrec is safe. Also, if the entry is guarded by a comparison with the |
--- |
5151 |
// addrec is safe. Also, if the entry is guarded by a comparison with the |
--- |
| 5152 |
// start value and the backedge is guarded by a comparison with the post-inc |
--- |
5152 |
// start value and the backedge is guarded by a comparison with the post-inc |
--- |
| 5153 |
// value, the addrec is safe. |
--- |
5153 |
// value, the addrec is safe. |
--- |
| 5154 |
if (isKnownPositive(Step)) { |
0 |
5154 |
if (isKnownPositive(Step)) { |
0 |
| 5155 |
const SCEV *N = getConstant(APInt::getMinValue(BitWidth) - |
0 |
5155 |
const SCEV *N = getConstant(APInt::getMinValue(BitWidth) - |
0 |
| 5156 |
getUnsignedRangeMax(Step)); |
0 |
5156 |
getUnsignedRangeMax(Step)); |
0 |
| 5157 |
if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) || |
0 |
5157 |
if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) || |
0 |
| 5158 |
isKnownOnEveryIteration(ICmpInst::ICMP_ULT, AR, N)) { |
0 |
5158 |
isKnownOnEveryIteration(ICmpInst::ICMP_ULT, AR, N)) { |
0 |
| 5159 |
Result = setFlags(Result, SCEV::FlagNUW); |
0 |
5159 |
Result = setFlags(Result, SCEV::FlagNUW); |
0 |
| 5160 |
} |
--- |
5160 |
} |
--- |
| 5161 |
} |
--- |
5161 |
} |
--- |
| 5162 |
|
--- |
5162 |
|
--- |
| 5163 |
return Result; |
0 |
5163 |
return Result; |
0 |
| 5164 |
} |
--- |
5164 |
} |
--- |
| 5165 |
|
--- |
5165 |
|
--- |
| 5166 |
namespace { |
--- |
5166 |
namespace { |
--- |
| 5167 |
|
--- |
5167 |
|
--- |
| 5168 |
/// Represents an abstract binary operation. This may exist as a |
--- |
5168 |
/// Represents an abstract binary operation. This may exist as a |
--- |
| 5169 |
/// normal instruction or constant expression, or may have been |
--- |
5169 |
/// normal instruction or constant expression, or may have been |
--- |
| 5170 |
/// derived from an expression tree. |
--- |
5170 |
/// derived from an expression tree. |
--- |
| 5171 |
struct BinaryOp { |
--- |
5171 |
struct BinaryOp { |
--- |
| 5172 |
unsigned Opcode; |
--- |
5172 |
unsigned Opcode; |
--- |
| 5173 |
Value *LHS; |
--- |
5173 |
Value *LHS; |
--- |
| 5174 |
Value *RHS; |
--- |
5174 |
Value *RHS; |
--- |
| 5175 |
bool IsNSW = false; |
--- |
5175 |
bool IsNSW = false; |
--- |
| 5176 |
bool IsNUW = false; |
--- |
5176 |
bool IsNUW = false; |
--- |
| 5177 |
|
--- |
5177 |
|
--- |
| 5178 |
/// Op is set if this BinaryOp corresponds to a concrete LLVM instruction or |
--- |
5178 |
/// Op is set if this BinaryOp corresponds to a concrete LLVM instruction or |
--- |
| 5179 |
/// constant expression. |
--- |
5179 |
/// constant expression. |
--- |
| 5180 |
Operator *Op = nullptr; |
--- |
5180 |
Operator *Op = nullptr; |
--- |
| 5181 |
|
--- |
5181 |
|
--- |
| 5182 |
explicit BinaryOp(Operator *Op) |
0 |
5182 |
explicit BinaryOp(Operator *Op) |
0 |
| 5183 |
: Opcode(Op->getOpcode()), LHS(Op->getOperand(0)), RHS(Op->getOperand(1)), |
0 |
5183 |
: Opcode(Op->getOpcode()), LHS(Op->getOperand(0)), RHS(Op->getOperand(1)), |
0 |
| 5184 |
Op(Op) { |
0 |
5184 |
Op(Op) { |
0 |
| 5185 |
if (auto *OBO = dyn_cast(Op)) { |
0 |
5185 |
if (auto *OBO = dyn_cast(Op)) { |
0 |
| 5186 |
IsNSW = OBO->hasNoSignedWrap(); |
0 |
5186 |
IsNSW = OBO->hasNoSignedWrap(); |
0 |
| 5187 |
IsNUW = OBO->hasNoUnsignedWrap(); |
0 |
5187 |
IsNUW = OBO->hasNoUnsignedWrap(); |
0 |
| 5188 |
} |
--- |
5188 |
} |
--- |
| 5189 |
} |
0 |
5189 |
} |
0 |
| 5190 |
|
--- |
5190 |
|
--- |
| 5191 |
explicit BinaryOp(unsigned Opcode, Value *LHS, Value *RHS, bool IsNSW = false, |
0 |
5191 |
explicit BinaryOp(unsigned Opcode, Value *LHS, Value *RHS, bool IsNSW = false, |
0 |
| 5192 |
bool IsNUW = false) |
--- |
5192 |
bool IsNUW = false) |
--- |
| 5193 |
: Opcode(Opcode), LHS(LHS), RHS(RHS), IsNSW(IsNSW), IsNUW(IsNUW) {} |
0 |
5193 |
: Opcode(Opcode), LHS(LHS), RHS(RHS), IsNSW(IsNSW), IsNUW(IsNUW) {} |
0 |
| 5194 |
}; |
--- |
5194 |
}; |
--- |
| 5195 |
|
--- |
5195 |
|
--- |
| 5196 |
} // end anonymous namespace |
--- |
5196 |
} // end anonymous namespace |
--- |
| 5197 |
|
--- |
5197 |
|
--- |
| 5198 |
/// Try to map \p V into a BinaryOp, and return \c std::nullopt on failure. |
--- |
5198 |
/// Try to map \p V into a BinaryOp, and return \c std::nullopt on failure. |
--- |
| 5199 |
static std::optional MatchBinaryOp(Value *V, const DataLayout &DL, |
0 |
5199 |
static std::optional MatchBinaryOp(Value *V, const DataLayout &DL, |
0 |
| 5200 |
AssumptionCache &AC, |
--- |
5200 |
AssumptionCache &AC, |
--- |
| 5201 |
const DominatorTree &DT, |
--- |
5201 |
const DominatorTree &DT, |
--- |
| 5202 |
const Instruction *CxtI) { |
--- |
5202 |
const Instruction *CxtI) { |
--- |
| 5203 |
auto *Op = dyn_cast(V); |
0 |
5203 |
auto *Op = dyn_cast(V); |
0 |
| 5204 |
if (!Op) |
0 |
5204 |
if (!Op) |
0 |
| 5205 |
return std::nullopt; |
0 |
5205 |
return std::nullopt; |
0 |
| 5206 |
|
--- |
5206 |
|
--- |
| 5207 |
// Implementation detail: all the cleverness here should happen without |
--- |
5207 |
// Implementation detail: all the cleverness here should happen without |
--- |
| 5208 |
// creating new SCEV expressions -- our caller knowns tricks to avoid creating |
--- |
5208 |
// creating new SCEV expressions -- our caller knowns tricks to avoid creating |
--- |
| 5209 |
// SCEV expressions when possible, and we should not break that. |
--- |
5209 |
// SCEV expressions when possible, and we should not break that. |
--- |
| 5210 |
|
--- |
5210 |
|
--- |
| 5211 |
switch (Op->getOpcode()) { |
0 |
5211 |
switch (Op->getOpcode()) { |
0 |
| 5212 |
case Instruction::Add: |
0 |
5212 |
case Instruction::Add: |
0 |
| 5213 |
case Instruction::Sub: |
--- |
5213 |
case Instruction::Sub: |
--- |
| 5214 |
case Instruction::Mul: |
--- |
5214 |
case Instruction::Mul: |
--- |
| 5215 |
case Instruction::UDiv: |
--- |
5215 |
case Instruction::UDiv: |
--- |
| 5216 |
case Instruction::URem: |
--- |
5216 |
case Instruction::URem: |
--- |
| 5217 |
case Instruction::And: |
--- |
5217 |
case Instruction::And: |
--- |
| 5218 |
case Instruction::AShr: |
--- |
5218 |
case Instruction::AShr: |
--- |
| 5219 |
case Instruction::Shl: |
--- |
5219 |
case Instruction::Shl: |
--- |
| 5220 |
return BinaryOp(Op); |
0 |
5220 |
return BinaryOp(Op); |
0 |
| 5221 |
|
--- |
5221 |
|
--- |
| 5222 |
case Instruction::Or: { |
0 |
5222 |
case Instruction::Or: { |
0 |
| 5223 |
// LLVM loves to convert `add` of operands with no common bits |
--- |
5223 |
// LLVM loves to convert `add` of operands with no common bits |
--- |
| 5224 |
// into an `or`. But SCEV really doesn't deal with `or` that well, |
--- |
5224 |
// into an `or`. But SCEV really doesn't deal with `or` that well, |
--- |
| 5225 |
// so try extra hard to recognize this `or` as an `add`. |
--- |
5225 |
// so try extra hard to recognize this `or` as an `add`. |
--- |
| 5226 |
if (haveNoCommonBitsSet(Op->getOperand(0), Op->getOperand(1), DL, &AC, CxtI, |
0 |
5226 |
if (haveNoCommonBitsSet(Op->getOperand(0), Op->getOperand(1), DL, &AC, CxtI, |
0 |
| 5227 |
&DT, /*UseInstrInfo=*/true)) |
--- |
5227 |
&DT, /*UseInstrInfo=*/true)) |
--- |
| 5228 |
return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1), |
0 |
5228 |
return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1), |
0 |
| 5229 |
/*IsNSW=*/true, /*IsNUW=*/true); |
0 |
5229 |
/*IsNSW=*/true, /*IsNUW=*/true); |
0 |
| 5230 |
return BinaryOp(Op); |
0 |
5230 |
return BinaryOp(Op); |
0 |
| 5231 |
} |
--- |
5231 |
} |
--- |
| 5232 |
|
--- |
5232 |
|
--- |
| 5233 |
case Instruction::Xor: |
0 |
5233 |
case Instruction::Xor: |
0 |
| 5234 |
if (auto *RHSC = dyn_cast(Op->getOperand(1))) |
0 |
5234 |
if (auto *RHSC = dyn_cast(Op->getOperand(1))) |
0 |
| 5235 |
// If the RHS of the xor is a signmask, then this is just an add. |
--- |
5235 |
// If the RHS of the xor is a signmask, then this is just an add. |
--- |
| 5236 |
// Instcombine turns add of signmask into xor as a strength reduction step. |
--- |
5236 |
// Instcombine turns add of signmask into xor as a strength reduction step. |
--- |
| 5237 |
if (RHSC->getValue().isSignMask()) |
0 |
5237 |
if (RHSC->getValue().isSignMask()) |
0 |
| 5238 |
return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1)); |
0 |
5238 |
return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1)); |
0 |
| 5239 |
// Binary `xor` is a bit-wise `add`. |
--- |
5239 |
// Binary `xor` is a bit-wise `add`. |
--- |
| 5240 |
if (V->getType()->isIntegerTy(1)) |
0 |
5240 |
if (V->getType()->isIntegerTy(1)) |
0 |
| 5241 |
return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1)); |
0 |
5241 |
return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1)); |
0 |
| 5242 |
return BinaryOp(Op); |
0 |
5242 |
return BinaryOp(Op); |
0 |
| 5243 |
|
--- |
5243 |
|
--- |
| 5244 |
case Instruction::LShr: |
0 |
5244 |
case Instruction::LShr: |
0 |
| 5245 |
// Turn logical shift right of a constant into a unsigned divide. |
--- |
5245 |
// Turn logical shift right of a constant into a unsigned divide. |
--- |
| 5246 |
if (ConstantInt *SA = dyn_cast(Op->getOperand(1))) { |
0 |
5246 |
if (ConstantInt *SA = dyn_cast(Op->getOperand(1))) { |
0 |
| 5247 |
uint32_t BitWidth = cast(Op->getType())->getBitWidth(); |
0 |
5247 |
uint32_t BitWidth = cast(Op->getType())->getBitWidth(); |
0 |
| 5248 |
|
--- |
5248 |
|
--- |
| 5249 |
// If the shift count is not less than the bitwidth, the result of |
--- |
5249 |
// If the shift count is not less than the bitwidth, the result of |
--- |
| 5250 |
// the shift is undefined. Don't try to analyze it, because the |
--- |
5250 |
// the shift is undefined. Don't try to analyze it, because the |
--- |
| 5251 |
// resolution chosen here may differ from the resolution chosen in |
--- |
5251 |
// resolution chosen here may differ from the resolution chosen in |
--- |
| 5252 |
// other parts of the compiler. |
--- |
5252 |
// other parts of the compiler. |
--- |
| 5253 |
if (SA->getValue().ult(BitWidth)) { |
0 |
5253 |
if (SA->getValue().ult(BitWidth)) { |
0 |
| 5254 |
Constant *X = |
--- |
5254 |
Constant *X = |
--- |
| 5255 |
ConstantInt::get(SA->getContext(), |
0 |
5255 |
ConstantInt::get(SA->getContext(), |
0 |
| 5256 |
APInt::getOneBitSet(BitWidth, SA->getZExtValue())); |
0 |
5256 |
APInt::getOneBitSet(BitWidth, SA->getZExtValue())); |
0 |
| 5257 |
return BinaryOp(Instruction::UDiv, Op->getOperand(0), X); |
0 |
5257 |
return BinaryOp(Instruction::UDiv, Op->getOperand(0), X); |
0 |
| 5258 |
} |
--- |
5258 |
} |
--- |
| 5259 |
} |
--- |
5259 |
} |
--- |
| 5260 |
return BinaryOp(Op); |
0 |
5260 |
return BinaryOp(Op); |
0 |
| 5261 |
|
--- |
5261 |
|
--- |
| 5262 |
case Instruction::ExtractValue: { |
0 |
5262 |
case Instruction::ExtractValue: { |
0 |
| 5263 |
auto *EVI = cast(Op); |
0 |
5263 |
auto *EVI = cast(Op); |
0 |
| 5264 |
if (EVI->getNumIndices() != 1 || EVI->getIndices()[0] != 0) |
0 |
5264 |
if (EVI->getNumIndices() != 1 || EVI->getIndices()[0] != 0) |
0 |
| 5265 |
break; |
0 |
5265 |
break; |
0 |
| 5266 |
|
--- |
5266 |
|
--- |
| 5267 |
auto *WO = dyn_cast(EVI->getAggregateOperand()); |
0 |
5267 |
auto *WO = dyn_cast(EVI->getAggregateOperand()); |
0 |
| 5268 |
if (!WO) |
0 |
5268 |
if (!WO) |
0 |
| 5269 |
break; |
0 |
5269 |
break; |
0 |
| 5270 |
|
--- |
5270 |
|
--- |
| 5271 |
Instruction::BinaryOps BinOp = WO->getBinaryOp(); |
0 |
5271 |
Instruction::BinaryOps BinOp = WO->getBinaryOp(); |
0 |
| 5272 |
bool Signed = WO->isSigned(); |
0 |
5272 |
bool Signed = WO->isSigned(); |
0 |
| 5273 |
// TODO: Should add nuw/nsw flags for mul as well. |
--- |
5273 |
// TODO: Should add nuw/nsw flags for mul as well. |
--- |
| 5274 |
if (BinOp == Instruction::Mul || !isOverflowIntrinsicNoWrap(WO, DT)) |
0 |
5274 |
if (BinOp == Instruction::Mul || !isOverflowIntrinsicNoWrap(WO, DT)) |
0 |
| 5275 |
return BinaryOp(BinOp, WO->getLHS(), WO->getRHS()); |
0 |
5275 |
return BinaryOp(BinOp, WO->getLHS(), WO->getRHS()); |
0 |
| 5276 |
|
--- |
5276 |
|
--- |
| 5277 |
// Now that we know that all uses of the arithmetic-result component of |
--- |
5277 |
// Now that we know that all uses of the arithmetic-result component of |
--- |
| 5278 |
// CI are guarded by the overflow check, we can go ahead and pretend |
--- |
5278 |
// CI are guarded by the overflow check, we can go ahead and pretend |
--- |
| 5279 |
// that the arithmetic is non-overflowing. |
--- |
5279 |
// that the arithmetic is non-overflowing. |
--- |
| 5280 |
return BinaryOp(BinOp, WO->getLHS(), WO->getRHS(), |
0 |
5280 |
return BinaryOp(BinOp, WO->getLHS(), WO->getRHS(), |
0 |
| 5281 |
/* IsNSW = */ Signed, /* IsNUW = */ !Signed); |
0 |
5281 |
/* IsNSW = */ Signed, /* IsNUW = */ !Signed); |
0 |
| 5282 |
} |
--- |
5282 |
} |
--- |
| 5283 |
|
--- |
5283 |
|
--- |
| 5284 |
default: |
0 |
5284 |
default: |
0 |
| 5285 |
break; |
0 |
5285 |
break; |
0 |
| 5286 |
} |
--- |
5286 |
} |
--- |
| 5287 |
|
--- |
5287 |
|
--- |
| 5288 |
// Recognise intrinsic loop.decrement.reg, and as this has exactly the same |
--- |
5288 |
// Recognise intrinsic loop.decrement.reg, and as this has exactly the same |
--- |
| 5289 |
// semantics as a Sub, return a binary sub expression. |
--- |
5289 |
// semantics as a Sub, return a binary sub expression. |
--- |
| 5290 |
if (auto *II = dyn_cast(V)) |
0 |
5290 |
if (auto *II = dyn_cast(V)) |
0 |
| 5291 |
if (II->getIntrinsicID() == Intrinsic::loop_decrement_reg) |
0 |
5291 |
if (II->getIntrinsicID() == Intrinsic::loop_decrement_reg) |
0 |
| 5292 |
return BinaryOp(Instruction::Sub, II->getOperand(0), II->getOperand(1)); |
0 |
5292 |
return BinaryOp(Instruction::Sub, II->getOperand(0), II->getOperand(1)); |
0 |
| 5293 |
|
--- |
5293 |
|
--- |
| 5294 |
return std::nullopt; |
0 |
5294 |
return std::nullopt; |
0 |
| 5295 |
} |
--- |
5295 |
} |
--- |
| 5296 |
|
--- |
5296 |
|
--- |
| 5297 |
/// Helper function to createAddRecFromPHIWithCasts. We have a phi |
--- |
5297 |
/// Helper function to createAddRecFromPHIWithCasts. We have a phi |
--- |
| 5298 |
/// node whose symbolic (unknown) SCEV is \p SymbolicPHI, which is updated via |
--- |
5298 |
/// node whose symbolic (unknown) SCEV is \p SymbolicPHI, which is updated via |
--- |
| 5299 |
/// the loop backedge by a SCEVAddExpr, possibly also with a few casts on the |
--- |
5299 |
/// the loop backedge by a SCEVAddExpr, possibly also with a few casts on the |
--- |
| 5300 |
/// way. This function checks if \p Op, an operand of this SCEVAddExpr, |
--- |
5300 |
/// way. This function checks if \p Op, an operand of this SCEVAddExpr, |
--- |
| 5301 |
/// follows one of the following patterns: |
--- |
5301 |
/// follows one of the following patterns: |
--- |
| 5302 |
/// Op == (SExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) |
--- |
5302 |
/// Op == (SExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) |
--- |
| 5303 |
/// Op == (ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) |
--- |
5303 |
/// Op == (ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) |
--- |
| 5304 |
/// If the SCEV expression of \p Op conforms with one of the expected patterns |
--- |
5304 |
/// If the SCEV expression of \p Op conforms with one of the expected patterns |
--- |
| 5305 |
/// we return the type of the truncation operation, and indicate whether the |
--- |
5305 |
/// we return the type of the truncation operation, and indicate whether the |
--- |
| 5306 |
/// truncated type should be treated as signed/unsigned by setting |
--- |
5306 |
/// truncated type should be treated as signed/unsigned by setting |
--- |
| 5307 |
/// \p Signed to true/false, respectively. |
--- |
5307 |
/// \p Signed to true/false, respectively. |
--- |
| 5308 |
static Type *isSimpleCastedPHI(const SCEV *Op, const SCEVUnknown *SymbolicPHI, |
0 |
5308 |
static Type *isSimpleCastedPHI(const SCEV *Op, const SCEVUnknown *SymbolicPHI, |
0 |
| 5309 |
bool &Signed, ScalarEvolution &SE) { |
--- |
5309 |
bool &Signed, ScalarEvolution &SE) { |
--- |
| 5310 |
// The case where Op == SymbolicPHI (that is, with no type conversions on |
--- |
5310 |
// The case where Op == SymbolicPHI (that is, with no type conversions on |
--- |
| 5311 |
// the way) is handled by the regular add recurrence creating logic and |
--- |
5311 |
// the way) is handled by the regular add recurrence creating logic and |
--- |
| 5312 |
// would have already been triggered in createAddRecForPHI. Reaching it here |
--- |
5312 |
// would have already been triggered in createAddRecForPHI. Reaching it here |
--- |
| 5313 |
// means that createAddRecFromPHI had failed for this PHI before (e.g., |
--- |
5313 |
// means that createAddRecFromPHI had failed for this PHI before (e.g., |
--- |
| 5314 |
// because one of the other operands of the SCEVAddExpr updating this PHI is |
--- |
5314 |
// because one of the other operands of the SCEVAddExpr updating this PHI is |
--- |
| 5315 |
// not invariant). |
--- |
5315 |
// not invariant). |
--- |
| 5316 |
// |
--- |
5316 |
// |
--- |
| 5317 |
// Here we look for the case where Op = (ext(trunc(SymbolicPHI))), and in |
--- |
5317 |
// Here we look for the case where Op = (ext(trunc(SymbolicPHI))), and in |
--- |
| 5318 |
// this case predicates that allow us to prove that Op == SymbolicPHI will |
--- |
5318 |
// this case predicates that allow us to prove that Op == SymbolicPHI will |
--- |
| 5319 |
// be added. |
--- |
5319 |
// be added. |
--- |
| 5320 |
if (Op == SymbolicPHI) |
0 |
5320 |
if (Op == SymbolicPHI) |
0 |
| 5321 |
return nullptr; |
0 |
5321 |
return nullptr; |
0 |
| 5322 |
|
--- |
5322 |
|
--- |
| 5323 |
unsigned SourceBits = SE.getTypeSizeInBits(SymbolicPHI->getType()); |
0 |
5323 |
unsigned SourceBits = SE.getTypeSizeInBits(SymbolicPHI->getType()); |
0 |
| 5324 |
unsigned NewBits = SE.getTypeSizeInBits(Op->getType()); |
0 |
5324 |
unsigned NewBits = SE.getTypeSizeInBits(Op->getType()); |
0 |
| 5325 |
if (SourceBits != NewBits) |
0 |
5325 |
if (SourceBits != NewBits) |
0 |
| 5326 |
return nullptr; |
0 |
5326 |
return nullptr; |
0 |
| 5327 |
|
--- |
5327 |
|
--- |
| 5328 |
const SCEVSignExtendExpr *SExt = dyn_cast(Op); |
0 |
5328 |
const SCEVSignExtendExpr *SExt = dyn_cast(Op); |
0 |
| 5329 |
const SCEVZeroExtendExpr *ZExt = dyn_cast(Op); |
0 |
5329 |
const SCEVZeroExtendExpr *ZExt = dyn_cast(Op); |
0 |
| 5330 |
if (!SExt && !ZExt) |
0 |
5330 |
if (!SExt && !ZExt) |
0 |
| 5331 |
return nullptr; |
0 |
5331 |
return nullptr; |
0 |
| 5332 |
const SCEVTruncateExpr *Trunc = |
--- |
5332 |
const SCEVTruncateExpr *Trunc = |
--- |
| 5333 |
SExt ? dyn_cast(SExt->getOperand()) |
0 |
5333 |
SExt ? dyn_cast(SExt->getOperand()) |
0 |
| 5334 |
: dyn_cast(ZExt->getOperand()); |
0 |
5334 |
: dyn_cast(ZExt->getOperand()); |
0 |
| 5335 |
if (!Trunc) |
0 |
5335 |
if (!Trunc) |
0 |
| 5336 |
return nullptr; |
0 |
5336 |
return nullptr; |
0 |
| 5337 |
const SCEV *X = Trunc->getOperand(); |
0 |
5337 |
const SCEV *X = Trunc->getOperand(); |
0 |
| 5338 |
if (X != SymbolicPHI) |
0 |
5338 |
if (X != SymbolicPHI) |
0 |
| 5339 |
return nullptr; |
0 |
5339 |
return nullptr; |
0 |
| 5340 |
Signed = SExt != nullptr; |
0 |
5340 |
Signed = SExt != nullptr; |
0 |
| 5341 |
return Trunc->getType(); |
0 |
5341 |
return Trunc->getType(); |
0 |
| 5342 |
} |
--- |
5342 |
} |
--- |
| 5343 |
|
--- |
5343 |
|
--- |
| 5344 |
static const Loop *isIntegerLoopHeaderPHI(const PHINode *PN, LoopInfo &LI) { |
0 |
5344 |
static const Loop *isIntegerLoopHeaderPHI(const PHINode *PN, LoopInfo &LI) { |
0 |
| 5345 |
if (!PN->getType()->isIntegerTy()) |
0 |
5345 |
if (!PN->getType()->isIntegerTy()) |
0 |
| 5346 |
return nullptr; |
0 |
5346 |
return nullptr; |
0 |
| 5347 |
const Loop *L = LI.getLoopFor(PN->getParent()); |
0 |
5347 |
const Loop *L = LI.getLoopFor(PN->getParent()); |
0 |
| 5348 |
if (!L || L->getHeader() != PN->getParent()) |
0 |
5348 |
if (!L || L->getHeader() != PN->getParent()) |
0 |
| 5349 |
return nullptr; |
0 |
5349 |
return nullptr; |
0 |
| 5350 |
return L; |
0 |
5350 |
return L; |
0 |
| 5351 |
} |
--- |
5351 |
} |
--- |
| 5352 |
|
--- |
5352 |
|
--- |
| 5353 |
// Analyze \p SymbolicPHI, a SCEV expression of a phi node, and check if the |
--- |
5353 |
// Analyze \p SymbolicPHI, a SCEV expression of a phi node, and check if the |
--- |
| 5354 |
// computation that updates the phi follows the following pattern: |
--- |
5354 |
// computation that updates the phi follows the following pattern: |
--- |
| 5355 |
// (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum |
--- |
5355 |
// (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum |
--- |
| 5356 |
// which correspond to a phi->trunc->sext/zext->add->phi update chain. |
--- |
5356 |
// which correspond to a phi->trunc->sext/zext->add->phi update chain. |
--- |
| 5357 |
// If so, try to see if it can be rewritten as an AddRecExpr under some |
--- |
5357 |
// If so, try to see if it can be rewritten as an AddRecExpr under some |
--- |
| 5358 |
// Predicates. If successful, return them as a pair. Also cache the results |
--- |
5358 |
// Predicates. If successful, return them as a pair. Also cache the results |
--- |
| 5359 |
// of the analysis. |
--- |
5359 |
// of the analysis. |
--- |
| 5360 |
// |
--- |
5360 |
// |
--- |
| 5361 |
// Example usage scenario: |
--- |
5361 |
// Example usage scenario: |
--- |
| 5362 |
// Say the Rewriter is called for the following SCEV: |
--- |
5362 |
// Say the Rewriter is called for the following SCEV: |
--- |
| 5363 |
// 8 * ((sext i32 (trunc i64 %X to i32) to i64) + %Step) |
--- |
5363 |
// 8 * ((sext i32 (trunc i64 %X to i32) to i64) + %Step) |
--- |
| 5364 |
// where: |
--- |
5364 |
// where: |
--- |
| 5365 |
// %X = phi i64 (%Start, %BEValue) |
--- |
5365 |
// %X = phi i64 (%Start, %BEValue) |
--- |
| 5366 |
// It will visitMul->visitAdd->visitSExt->visitTrunc->visitUnknown(%X), |
--- |
5366 |
// It will visitMul->visitAdd->visitSExt->visitTrunc->visitUnknown(%X), |
--- |
| 5367 |
// and call this function with %SymbolicPHI = %X. |
--- |
5367 |
// and call this function with %SymbolicPHI = %X. |
--- |
| 5368 |
// |
--- |
5368 |
// |
--- |
| 5369 |
// The analysis will find that the value coming around the backedge has |
--- |
5369 |
// The analysis will find that the value coming around the backedge has |
--- |
| 5370 |
// the following SCEV: |
--- |
5370 |
// the following SCEV: |
--- |
| 5371 |
// BEValue = ((sext i32 (trunc i64 %X to i32) to i64) + %Step) |
--- |
5371 |
// BEValue = ((sext i32 (trunc i64 %X to i32) to i64) + %Step) |
--- |
| 5372 |
// Upon concluding that this matches the desired pattern, the function |
--- |
5372 |
// Upon concluding that this matches the desired pattern, the function |
--- |
| 5373 |
// will return the pair {NewAddRec, SmallPredsVec} where: |
--- |
5373 |
// will return the pair {NewAddRec, SmallPredsVec} where: |
--- |
| 5374 |
// NewAddRec = {%Start,+,%Step} |
--- |
5374 |
// NewAddRec = {%Start,+,%Step} |
--- |
| 5375 |
// SmallPredsVec = {P1, P2, P3} as follows: |
--- |
5375 |
// SmallPredsVec = {P1, P2, P3} as follows: |
--- |
| 5376 |
// P1(WrapPred): AR: {trunc(%Start),+,(trunc %Step)} Flags: |
--- |
5376 |
// P1(WrapPred): AR: {trunc(%Start),+,(trunc %Step)} Flags: |
--- |
| 5377 |
// P2(EqualPred): %Start == (sext i32 (trunc i64 %Start to i32) to i64) |
--- |
5377 |
// P2(EqualPred): %Start == (sext i32 (trunc i64 %Start to i32) to i64) |
--- |
| 5378 |
// P3(EqualPred): %Step == (sext i32 (trunc i64 %Step to i32) to i64) |
--- |
5378 |
// P3(EqualPred): %Step == (sext i32 (trunc i64 %Step to i32) to i64) |
--- |
| 5379 |
// The returned pair means that SymbolicPHI can be rewritten into NewAddRec |
--- |
5379 |
// The returned pair means that SymbolicPHI can be rewritten into NewAddRec |
--- |
| 5380 |
// under the predicates {P1,P2,P3}. |
--- |
5380 |
// under the predicates {P1,P2,P3}. |
--- |
| 5381 |
// This predicated rewrite will be cached in PredicatedSCEVRewrites: |
--- |
5381 |
// This predicated rewrite will be cached in PredicatedSCEVRewrites: |
--- |
| 5382 |
// PredicatedSCEVRewrites[{%X,L}] = {NewAddRec, {P1,P2,P3)} |
--- |
5382 |
// PredicatedSCEVRewrites[{%X,L}] = {NewAddRec, {P1,P2,P3)} |
--- |
| 5383 |
// |
--- |
5383 |
// |
--- |
| 5384 |
// TODO's: |
--- |
5384 |
// TODO's: |
--- |
| 5385 |
// |
--- |
5385 |
// |
--- |
| 5386 |
// 1) Extend the Induction descriptor to also support inductions that involve |
--- |
5386 |
// 1) Extend the Induction descriptor to also support inductions that involve |
--- |
| 5387 |
// casts: When needed (namely, when we are called in the context of the |
--- |
5387 |
// casts: When needed (namely, when we are called in the context of the |
--- |
| 5388 |
// vectorizer induction analysis), a Set of cast instructions will be |
--- |
5388 |
// vectorizer induction analysis), a Set of cast instructions will be |
--- |
| 5389 |
// populated by this method, and provided back to isInductionPHI. This is |
--- |
5389 |
// populated by this method, and provided back to isInductionPHI. This is |
--- |
| 5390 |
// needed to allow the vectorizer to properly record them to be ignored by |
--- |
5390 |
// needed to allow the vectorizer to properly record them to be ignored by |
--- |
| 5391 |
// the cost model and to avoid vectorizing them (otherwise these casts, |
--- |
5391 |
// the cost model and to avoid vectorizing them (otherwise these casts, |
--- |
| 5392 |
// which are redundant under the runtime overflow checks, will be |
--- |
5392 |
// which are redundant under the runtime overflow checks, will be |
--- |
| 5393 |
// vectorized, which can be costly). |
--- |
5393 |
// vectorized, which can be costly). |
--- |
| 5394 |
// |
--- |
5394 |
// |
--- |
| 5395 |
// 2) Support additional induction/PHISCEV patterns: We also want to support |
--- |
5395 |
// 2) Support additional induction/PHISCEV patterns: We also want to support |
--- |
| 5396 |
// inductions where the sext-trunc / zext-trunc operations (partly) occur |
--- |
5396 |
// inductions where the sext-trunc / zext-trunc operations (partly) occur |
--- |
| 5397 |
// after the induction update operation (the induction increment): |
--- |
5397 |
// after the induction update operation (the induction increment): |
--- |
| 5398 |
// |
--- |
5398 |
// |
--- |
| 5399 |
// (Trunc iy (SExt/ZExt ix (%SymbolicPHI + InvariantAccum) to iy) to ix) |
--- |
5399 |
// (Trunc iy (SExt/ZExt ix (%SymbolicPHI + InvariantAccum) to iy) to ix) |
--- |
| 5400 |
// which correspond to a phi->add->trunc->sext/zext->phi update chain. |
--- |
5400 |
// which correspond to a phi->add->trunc->sext/zext->phi update chain. |
--- |
| 5401 |
// |
--- |
5401 |
// |
--- |
| 5402 |
// (Trunc iy ((SExt/ZExt ix (%SymbolicPhi) to iy) + InvariantAccum) to ix) |
--- |
5402 |
// (Trunc iy ((SExt/ZExt ix (%SymbolicPhi) to iy) + InvariantAccum) to ix) |
--- |
| 5403 |
// which correspond to a phi->trunc->add->sext/zext->phi update chain. |
--- |
5403 |
// which correspond to a phi->trunc->add->sext/zext->phi update chain. |
--- |
| 5404 |
// |
--- |
5404 |
// |
--- |
| 5405 |
// 3) Outline common code with createAddRecFromPHI to avoid duplication. |
--- |
5405 |
// 3) Outline common code with createAddRecFromPHI to avoid duplication. |
--- |
| 5406 |
std::optional>> |
--- |
5406 |
std::optional>> |
--- |
| 5407 |
ScalarEvolution::createAddRecFromPHIWithCastsImpl(const SCEVUnknown *SymbolicPHI) { |
0 |
5407 |
ScalarEvolution::createAddRecFromPHIWithCastsImpl(const SCEVUnknown *SymbolicPHI) { |
0 |
| 5408 |
SmallVector Predicates; |
0 |
5408 |
SmallVector Predicates; |
0 |
| 5409 |
|
--- |
5409 |
|
--- |
| 5410 |
// *** Part1: Analyze if we have a phi-with-cast pattern for which we can |
--- |
5410 |
// *** Part1: Analyze if we have a phi-with-cast pattern for which we can |
--- |
| 5411 |
// return an AddRec expression under some predicate. |
--- |
5411 |
// return an AddRec expression under some predicate. |
--- |
| 5412 |
|
--- |
5412 |
|
--- |
| 5413 |
auto *PN = cast(SymbolicPHI->getValue()); |
0 |
5413 |
auto *PN = cast(SymbolicPHI->getValue()); |
0 |
| 5414 |
const Loop *L = isIntegerLoopHeaderPHI(PN, LI); |
0 |
5414 |
const Loop *L = isIntegerLoopHeaderPHI(PN, LI); |
0 |
| 5415 |
assert(L && "Expecting an integer loop header phi"); |
0 |
5415 |
assert(L && "Expecting an integer loop header phi"); |
0 |
| 5416 |
|
--- |
5416 |
|
--- |
| 5417 |
// The loop may have multiple entrances or multiple exits; we can analyze |
--- |
5417 |
// The loop may have multiple entrances or multiple exits; we can analyze |
--- |
| 5418 |
// this phi as an addrec if it has a unique entry value and a unique |
--- |
5418 |
// this phi as an addrec if it has a unique entry value and a unique |
--- |
| 5419 |
// backedge value. |
--- |
5419 |
// backedge value. |
--- |
| 5420 |
Value *BEValueV = nullptr, *StartValueV = nullptr; |
0 |
5420 |
Value *BEValueV = nullptr, *StartValueV = nullptr; |
0 |
| 5421 |
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
0 |
5421 |
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
0 |
| 5422 |
Value *V = PN->getIncomingValue(i); |
0 |
5422 |
Value *V = PN->getIncomingValue(i); |
0 |
| 5423 |
if (L->contains(PN->getIncomingBlock(i))) { |
0 |
5423 |
if (L->contains(PN->getIncomingBlock(i))) { |
0 |
| 5424 |
if (!BEValueV) { |
0 |
5424 |
if (!BEValueV) { |
0 |
| 5425 |
BEValueV = V; |
0 |
5425 |
BEValueV = V; |
0 |
| 5426 |
} else if (BEValueV != V) { |
0 |
5426 |
} else if (BEValueV != V) { |
0 |
| 5427 |
BEValueV = nullptr; |
0 |
5427 |
BEValueV = nullptr; |
0 |
| 5428 |
break; |
0 |
5428 |
break; |
0 |
| 5429 |
} |
--- |
5429 |
} |
--- |
| 5430 |
} else if (!StartValueV) { |
0 |
5430 |
} else if (!StartValueV) { |
0 |
| 5431 |
StartValueV = V; |
0 |
5431 |
StartValueV = V; |
0 |
| 5432 |
} else if (StartValueV != V) { |
0 |
5432 |
} else if (StartValueV != V) { |
0 |
| 5433 |
StartValueV = nullptr; |
0 |
5433 |
StartValueV = nullptr; |
0 |
| 5434 |
break; |
0 |
5434 |
break; |
0 |
| 5435 |
} |
--- |
5435 |
} |
--- |
| 5436 |
} |
--- |
5436 |
} |
--- |
| 5437 |
if (!BEValueV || !StartValueV) |
0 |
5437 |
if (!BEValueV || !StartValueV) |
0 |
| 5438 |
return std::nullopt; |
0 |
5438 |
return std::nullopt; |
0 |
| 5439 |
|
--- |
5439 |
|
--- |
| 5440 |
const SCEV *BEValue = getSCEV(BEValueV); |
0 |
5440 |
const SCEV *BEValue = getSCEV(BEValueV); |
0 |
| 5441 |
|
--- |
5441 |
|
--- |
| 5442 |
// If the value coming around the backedge is an add with the symbolic |
--- |
5442 |
// If the value coming around the backedge is an add with the symbolic |
--- |
| 5443 |
// value we just inserted, possibly with casts that we can ignore under |
--- |
5443 |
// value we just inserted, possibly with casts that we can ignore under |
--- |
| 5444 |
// an appropriate runtime guard, then we found a simple induction variable! |
--- |
5444 |
// an appropriate runtime guard, then we found a simple induction variable! |
--- |
| 5445 |
const auto *Add = dyn_cast(BEValue); |
0 |
5445 |
const auto *Add = dyn_cast(BEValue); |
0 |
| 5446 |
if (!Add) |
0 |
5446 |
if (!Add) |
0 |
| 5447 |
return std::nullopt; |
0 |
5447 |
return std::nullopt; |
0 |
| 5448 |
|
--- |
5448 |
|
--- |
| 5449 |
// If there is a single occurrence of the symbolic value, possibly |
--- |
5449 |
// If there is a single occurrence of the symbolic value, possibly |
--- |
| 5450 |
// casted, replace it with a recurrence. |
--- |
5450 |
// casted, replace it with a recurrence. |
--- |
| 5451 |
unsigned FoundIndex = Add->getNumOperands(); |
0 |
5451 |
unsigned FoundIndex = Add->getNumOperands(); |
0 |
| 5452 |
Type *TruncTy = nullptr; |
0 |
5452 |
Type *TruncTy = nullptr; |
0 |
| 5453 |
bool Signed; |
--- |
5453 |
bool Signed; |
--- |
| 5454 |
for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
0 |
5454 |
for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
0 |
| 5455 |
if ((TruncTy = |
0 |
5455 |
if ((TruncTy = |
0 |
| 5456 |
isSimpleCastedPHI(Add->getOperand(i), SymbolicPHI, Signed, *this))) |
0 |
5456 |
isSimpleCastedPHI(Add->getOperand(i), SymbolicPHI, Signed, *this))) |
0 |
| 5457 |
if (FoundIndex == e) { |
0 |
5457 |
if (FoundIndex == e) { |
0 |
| 5458 |
FoundIndex = i; |
0 |
5458 |
FoundIndex = i; |
0 |
| 5459 |
break; |
0 |
5459 |
break; |
0 |
| 5460 |
} |
--- |
5460 |
} |
--- |
| 5461 |
|
--- |
5461 |
|
--- |
| 5462 |
if (FoundIndex == Add->getNumOperands()) |
0 |
5462 |
if (FoundIndex == Add->getNumOperands()) |
0 |
| 5463 |
return std::nullopt; |
0 |
5463 |
return std::nullopt; |
0 |
| 5464 |
|
--- |
5464 |
|
--- |
| 5465 |
// Create an add with everything but the specified operand. |
--- |
5465 |
// Create an add with everything but the specified operand. |
--- |
| 5466 |
SmallVector Ops; |
0 |
5466 |
SmallVector Ops; |
0 |
| 5467 |
for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
0 |
5467 |
for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
0 |
| 5468 |
if (i != FoundIndex) |
0 |
5468 |
if (i != FoundIndex) |
0 |
| 5469 |
Ops.push_back(Add->getOperand(i)); |
0 |
5469 |
Ops.push_back(Add->getOperand(i)); |
0 |
| 5470 |
const SCEV *Accum = getAddExpr(Ops); |
0 |
5470 |
const SCEV *Accum = getAddExpr(Ops); |
0 |
| 5471 |
|
--- |
5471 |
|
--- |
| 5472 |
// The runtime checks will not be valid if the step amount is |
--- |
5472 |
// The runtime checks will not be valid if the step amount is |
--- |
| 5473 |
// varying inside the loop. |
--- |
5473 |
// varying inside the loop. |
--- |
| 5474 |
if (!isLoopInvariant(Accum, L)) |
0 |
5474 |
if (!isLoopInvariant(Accum, L)) |
0 |
| 5475 |
return std::nullopt; |
0 |
5475 |
return std::nullopt; |
0 |
| 5476 |
|
--- |
5476 |
|
--- |
| 5477 |
// *** Part2: Create the predicates |
--- |
5477 |
// *** Part2: Create the predicates |
--- |
| 5478 |
|
--- |
5478 |
|
--- |
| 5479 |
// Analysis was successful: we have a phi-with-cast pattern for which we |
--- |
5479 |
// Analysis was successful: we have a phi-with-cast pattern for which we |
--- |
| 5480 |
// can return an AddRec expression under the following predicates: |
--- |
5480 |
// can return an AddRec expression under the following predicates: |
--- |
| 5481 |
// |
--- |
5481 |
// |
--- |
| 5482 |
// P1: A Wrap predicate that guarantees that Trunc(Start) + i*Trunc(Accum) |
--- |
5482 |
// P1: A Wrap predicate that guarantees that Trunc(Start) + i*Trunc(Accum) |
--- |
| 5483 |
// fits within the truncated type (does not overflow) for i = 0 to n-1. |
--- |
5483 |
// fits within the truncated type (does not overflow) for i = 0 to n-1. |
--- |
| 5484 |
// P2: An Equal predicate that guarantees that |
--- |
5484 |
// P2: An Equal predicate that guarantees that |
--- |
| 5485 |
// Start = (Ext ix (Trunc iy (Start) to ix) to iy) |
--- |
5485 |
// Start = (Ext ix (Trunc iy (Start) to ix) to iy) |
--- |
| 5486 |
// P3: An Equal predicate that guarantees that |
--- |
5486 |
// P3: An Equal predicate that guarantees that |
--- |
| 5487 |
// Accum = (Ext ix (Trunc iy (Accum) to ix) to iy) |
--- |
5487 |
// Accum = (Ext ix (Trunc iy (Accum) to ix) to iy) |
--- |
| 5488 |
// |
--- |
5488 |
// |
--- |
| 5489 |
// As we next prove, the above predicates guarantee that: |
--- |
5489 |
// As we next prove, the above predicates guarantee that: |
--- |
| 5490 |
// Start + i*Accum = (Ext ix (Trunc iy ( Start + i*Accum ) to ix) to iy) |
--- |
5490 |
// Start + i*Accum = (Ext ix (Trunc iy ( Start + i*Accum ) to ix) to iy) |
--- |
| 5491 |
// |
--- |
5491 |
// |
--- |
| 5492 |
// |
--- |
5492 |
// |
--- |
| 5493 |
// More formally, we want to prove that: |
--- |
5493 |
// More formally, we want to prove that: |
--- |
| 5494 |
// Expr(i+1) = Start + (i+1) * Accum |
--- |
5494 |
// Expr(i+1) = Start + (i+1) * Accum |
--- |
| 5495 |
// = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum |
--- |
5495 |
// = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum |
--- |
| 5496 |
// |
--- |
5496 |
// |
--- |
| 5497 |
// Given that: |
--- |
5497 |
// Given that: |
--- |
| 5498 |
// 1) Expr(0) = Start |
--- |
5498 |
// 1) Expr(0) = Start |
--- |
| 5499 |
// 2) Expr(1) = Start + Accum |
--- |
5499 |
// 2) Expr(1) = Start + Accum |
--- |
| 5500 |
// = (Ext ix (Trunc iy (Start) to ix) to iy) + Accum :: from P2 |
--- |
5500 |
// = (Ext ix (Trunc iy (Start) to ix) to iy) + Accum :: from P2 |
--- |
| 5501 |
// 3) Induction hypothesis (step i): |
--- |
5501 |
// 3) Induction hypothesis (step i): |
--- |
| 5502 |
// Expr(i) = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum |
--- |
5502 |
// Expr(i) = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum |
--- |
| 5503 |
// |
--- |
5503 |
// |
--- |
| 5504 |
// Proof: |
--- |
5504 |
// Proof: |
--- |
| 5505 |
// Expr(i+1) = |
--- |
5505 |
// Expr(i+1) = |
--- |
| 5506 |
// = Start + (i+1)*Accum |
--- |
5506 |
// = Start + (i+1)*Accum |
--- |
| 5507 |
// = (Start + i*Accum) + Accum |
--- |
5507 |
// = (Start + i*Accum) + Accum |
--- |
| 5508 |
// = Expr(i) + Accum |
--- |
5508 |
// = Expr(i) + Accum |
--- |
| 5509 |
// = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum + Accum |
--- |
5509 |
// = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum + Accum |
--- |
| 5510 |
// :: from step i |
--- |
5510 |
// :: from step i |
--- |
| 5511 |
// |
--- |
5511 |
// |
--- |
| 5512 |
// = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy) + Accum + Accum |
--- |
5512 |
// = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy) + Accum + Accum |
--- |
| 5513 |
// |
--- |
5513 |
// |
--- |
| 5514 |
// = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy) |
--- |
5514 |
// = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy) |
--- |
| 5515 |
// + (Ext ix (Trunc iy (Accum) to ix) to iy) |
--- |
5515 |
// + (Ext ix (Trunc iy (Accum) to ix) to iy) |
--- |
| 5516 |
// + Accum :: from P3 |
--- |
5516 |
// + Accum :: from P3 |
--- |
| 5517 |
// |
--- |
5517 |
// |
--- |
| 5518 |
// = (Ext ix (Trunc iy ((Start + (i-1)*Accum) + Accum) to ix) to iy) |
--- |
5518 |
// = (Ext ix (Trunc iy ((Start + (i-1)*Accum) + Accum) to ix) to iy) |
--- |
| 5519 |
// + Accum :: from P1: Ext(x)+Ext(y)=>Ext(x+y) |
--- |
5519 |
// + Accum :: from P1: Ext(x)+Ext(y)=>Ext(x+y) |
--- |
| 5520 |
// |
--- |
5520 |
// |
--- |
| 5521 |
// = (Ext ix (Trunc iy (Start + i*Accum) to ix) to iy) + Accum |
--- |
5521 |
// = (Ext ix (Trunc iy (Start + i*Accum) to ix) to iy) + Accum |
--- |
| 5522 |
// = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum |
--- |
5522 |
// = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum |
--- |
| 5523 |
// |
--- |
5523 |
// |
--- |
| 5524 |
// By induction, the same applies to all iterations 1<=i
| --- |
5524 |
// By induction, the same applies to all iterations 1<=i
| --- |
| |
| 5525 |
// |
--- |
5525 |
// |
--- |
| 5526 |
|
--- |
5526 |
|
--- |
| 5527 |
// Create a truncated addrec for which we will add a no overflow check (P1). |
--- |
5527 |
// Create a truncated addrec for which we will add a no overflow check (P1). |
--- |
| 5528 |
const SCEV *StartVal = getSCEV(StartValueV); |
0 |
5528 |
const SCEV *StartVal = getSCEV(StartValueV); |
0 |
| 5529 |
const SCEV *PHISCEV = |
--- |
5529 |
const SCEV *PHISCEV = |
--- |
| 5530 |
getAddRecExpr(getTruncateExpr(StartVal, TruncTy), |
0 |
5530 |
getAddRecExpr(getTruncateExpr(StartVal, TruncTy), |
0 |
| 5531 |
getTruncateExpr(Accum, TruncTy), L, SCEV::FlagAnyWrap); |
--- |
5531 |
getTruncateExpr(Accum, TruncTy), L, SCEV::FlagAnyWrap); |
--- |
| 5532 |
|
--- |
5532 |
|
--- |
| 5533 |
// PHISCEV can be either a SCEVConstant or a SCEVAddRecExpr. |
--- |
5533 |
// PHISCEV can be either a SCEVConstant or a SCEVAddRecExpr. |
--- |
| 5534 |
// ex: If truncated Accum is 0 and StartVal is a constant, then PHISCEV |
--- |
5534 |
// ex: If truncated Accum is 0 and StartVal is a constant, then PHISCEV |
--- |
| 5535 |
// will be constant. |
--- |
5535 |
// will be constant. |
--- |
| 5536 |
// |
--- |
5536 |
// |
--- |
| 5537 |
// If PHISCEV is a constant, then P1 degenerates into P2 or P3, so we don't |
--- |
5537 |
// If PHISCEV is a constant, then P1 degenerates into P2 or P3, so we don't |
--- |
| 5538 |
// add P1. |
--- |
5538 |
// add P1. |
--- |
| 5539 |
if (const auto *AR = dyn_cast(PHISCEV)) { |
0 |
5539 |
if (const auto *AR = dyn_cast(PHISCEV)) { |
0 |
| 5540 |
SCEVWrapPredicate::IncrementWrapFlags AddedFlags = |
0 |
5540 |
SCEVWrapPredicate::IncrementWrapFlags AddedFlags = |
0 |
| 5541 |
Signed ? SCEVWrapPredicate::IncrementNSSW |
0 |
5541 |
Signed ? SCEVWrapPredicate::IncrementNSSW |
0 |
| 5542 |
: SCEVWrapPredicate::IncrementNUSW; |
--- |
5542 |
: SCEVWrapPredicate::IncrementNUSW; |
--- |
| 5543 |
const SCEVPredicate *AddRecPred = getWrapPredicate(AR, AddedFlags); |
0 |
5543 |
const SCEVPredicate *AddRecPred = getWrapPredicate(AR, AddedFlags); |
0 |
| 5544 |
Predicates.push_back(AddRecPred); |
0 |
5544 |
Predicates.push_back(AddRecPred); |
0 |
| 5545 |
} |
--- |
5545 |
} |
--- |
| 5546 |
|
--- |
5546 |
|
--- |
| 5547 |
// Create the Equal Predicates P2,P3: |
--- |
5547 |
// Create the Equal Predicates P2,P3: |
--- |
| 5548 |
|
--- |
5548 |
|
--- |
| 5549 |
// It is possible that the predicates P2 and/or P3 are computable at |
--- |
5549 |
// It is possible that the predicates P2 and/or P3 are computable at |
--- |
| 5550 |
// compile time due to StartVal and/or Accum being constants. |
--- |
5550 |
// compile time due to StartVal and/or Accum being constants. |
--- |
| 5551 |
// If either one is, then we can check that now and escape if either P2 |
--- |
5551 |
// If either one is, then we can check that now and escape if either P2 |
--- |
| 5552 |
// or P3 is false. |
--- |
5552 |
// or P3 is false. |
--- |
| 5553 |
|
--- |
5553 |
|
--- |
| 5554 |
// Construct the extended SCEV: (Ext ix (Trunc iy (Expr) to ix) to iy) |
--- |
5554 |
// Construct the extended SCEV: (Ext ix (Trunc iy (Expr) to ix) to iy) |
--- |
| 5555 |
// for each of StartVal and Accum |
--- |
5555 |
// for each of StartVal and Accum |
--- |
| 5556 |
auto getExtendedExpr = [&](const SCEV *Expr, |
0 |
5556 |
auto getExtendedExpr = [&](const SCEV *Expr, |
0 |
| 5557 |
bool CreateSignExtend) -> const SCEV * { |
--- |
5557 |
bool CreateSignExtend) -> const SCEV * { |
--- |
| 5558 |
assert(isLoopInvariant(Expr, L) && "Expr is expected to be invariant"); |
0 |
5558 |
assert(isLoopInvariant(Expr, L) && "Expr is expected to be invariant"); |
0 |
| 5559 |
const SCEV *TruncatedExpr = getTruncateExpr(Expr, TruncTy); |
0 |
5559 |
const SCEV *TruncatedExpr = getTruncateExpr(Expr, TruncTy); |
0 |
| 5560 |
const SCEV *ExtendedExpr = |
--- |
5560 |
const SCEV *ExtendedExpr = |
--- |
| 5561 |
CreateSignExtend ? getSignExtendExpr(TruncatedExpr, Expr->getType()) |
0 |
5561 |
CreateSignExtend ? getSignExtendExpr(TruncatedExpr, Expr->getType()) |
0 |
| 5562 |
: getZeroExtendExpr(TruncatedExpr, Expr->getType()); |
0 |
5562 |
: getZeroExtendExpr(TruncatedExpr, Expr->getType()); |
0 |
| 5563 |
return ExtendedExpr; |
0 |
5563 |
return ExtendedExpr; |
0 |
| 5564 |
}; |
0 |
5564 |
}; |
0 |
| 5565 |
|
--- |
5565 |
|
--- |
| 5566 |
// Given: |
--- |
5566 |
// Given: |
--- |
| 5567 |
// ExtendedExpr = (Ext ix (Trunc iy (Expr) to ix) to iy |
--- |
5567 |
// ExtendedExpr = (Ext ix (Trunc iy (Expr) to ix) to iy |
--- |
| 5568 |
// = getExtendedExpr(Expr) |
--- |
5568 |
// = getExtendedExpr(Expr) |
--- |
| 5569 |
// Determine whether the predicate P: Expr == ExtendedExpr |
--- |
5569 |
// Determine whether the predicate P: Expr == ExtendedExpr |
--- |
| 5570 |
// is known to be false at compile time |
--- |
5570 |
// is known to be false at compile time |
--- |
| 5571 |
auto PredIsKnownFalse = [&](const SCEV *Expr, |
0 |
5571 |
auto PredIsKnownFalse = [&](const SCEV *Expr, |
0 |
| 5572 |
const SCEV *ExtendedExpr) -> bool { |
--- |
5572 |
const SCEV *ExtendedExpr) -> bool { |
--- |
| 5573 |
return Expr != ExtendedExpr && |
0 |
5573 |
return Expr != ExtendedExpr && |
0 |
| 5574 |
isKnownPredicate(ICmpInst::ICMP_NE, Expr, ExtendedExpr); |
0 |
5574 |
isKnownPredicate(ICmpInst::ICMP_NE, Expr, ExtendedExpr); |
0 |
| 5575 |
}; |
0 |
5575 |
}; |
0 |
| 5576 |
|
--- |
5576 |
|
--- |
| 5577 |
const SCEV *StartExtended = getExtendedExpr(StartVal, Signed); |
0 |
5577 |
const SCEV *StartExtended = getExtendedExpr(StartVal, Signed); |
0 |
| 5578 |
if (PredIsKnownFalse(StartVal, StartExtended)) { |
0 |
5578 |
if (PredIsKnownFalse(StartVal, StartExtended)) { |
0 |
| 5579 |
LLVM_DEBUG(dbgs() << "P2 is compile-time false\n";); |
0 |
5579 |
LLVM_DEBUG(dbgs() << "P2 is compile-time false\n";); |
0 |
| 5580 |
return std::nullopt; |
0 |
5580 |
return std::nullopt; |
0 |
| 5581 |
} |
--- |
5581 |
} |
--- |
| 5582 |
|
--- |
5582 |
|
--- |
| 5583 |
// The Step is always Signed (because the overflow checks are either |
--- |
5583 |
// The Step is always Signed (because the overflow checks are either |
--- |
| 5584 |
// NSSW or NUSW) |
--- |
5584 |
// NSSW or NUSW) |
--- |
| 5585 |
const SCEV *AccumExtended = getExtendedExpr(Accum, /*CreateSignExtend=*/true); |
0 |
5585 |
const SCEV *AccumExtended = getExtendedExpr(Accum, /*CreateSignExtend=*/true); |
0 |
| 5586 |
if (PredIsKnownFalse(Accum, AccumExtended)) { |
0 |
5586 |
if (PredIsKnownFalse(Accum, AccumExtended)) { |
0 |
| 5587 |
LLVM_DEBUG(dbgs() << "P3 is compile-time false\n";); |
0 |
5587 |
LLVM_DEBUG(dbgs() << "P3 is compile-time false\n";); |
0 |
| 5588 |
return std::nullopt; |
0 |
5588 |
return std::nullopt; |
0 |
| 5589 |
} |
--- |
5589 |
} |
--- |
| 5590 |
|
--- |
5590 |
|
--- |
| 5591 |
auto AppendPredicate = [&](const SCEV *Expr, |
0 |
5591 |
auto AppendPredicate = [&](const SCEV *Expr, |
0 |
| 5592 |
const SCEV *ExtendedExpr) -> void { |
--- |
5592 |
const SCEV *ExtendedExpr) -> void { |
--- |
| 5593 |
if (Expr != ExtendedExpr && |
0 |
5593 |
if (Expr != ExtendedExpr && |
0 |
| 5594 |
!isKnownPredicate(ICmpInst::ICMP_EQ, Expr, ExtendedExpr)) { |
0 |
5594 |
!isKnownPredicate(ICmpInst::ICMP_EQ, Expr, ExtendedExpr)) { |
0 |
| 5595 |
const SCEVPredicate *Pred = getEqualPredicate(Expr, ExtendedExpr); |
0 |
5595 |
const SCEVPredicate *Pred = getEqualPredicate(Expr, ExtendedExpr); |
0 |
| 5596 |
LLVM_DEBUG(dbgs() << "Added Predicate: " << *Pred); |
0 |
5596 |
LLVM_DEBUG(dbgs() << "Added Predicate: " << *Pred); |
0 |
| 5597 |
Predicates.push_back(Pred); |
0 |
5597 |
Predicates.push_back(Pred); |
0 |
| 5598 |
} |
--- |
5598 |
} |
--- |
| 5599 |
}; |
0 |
5599 |
}; |
0 |
| 5600 |
|
--- |
5600 |
|
--- |
| 5601 |
AppendPredicate(StartVal, StartExtended); |
0 |
5601 |
AppendPredicate(StartVal, StartExtended); |
0 |
| 5602 |
AppendPredicate(Accum, AccumExtended); |
0 |
5602 |
AppendPredicate(Accum, AccumExtended); |
0 |
| 5603 |
|
--- |
5603 |
|
--- |
| 5604 |
// *** Part3: Predicates are ready. Now go ahead and create the new addrec in |
--- |
5604 |
// *** Part3: Predicates are ready. Now go ahead and create the new addrec in |
--- |
| 5605 |
// which the casts had been folded away. The caller can rewrite SymbolicPHI |
--- |
5605 |
// which the casts had been folded away. The caller can rewrite SymbolicPHI |
--- |
| 5606 |
// into NewAR if it will also add the runtime overflow checks specified in |
--- |
5606 |
// into NewAR if it will also add the runtime overflow checks specified in |
--- |
| 5607 |
// Predicates. |
--- |
5607 |
// Predicates. |
--- |
| 5608 |
auto *NewAR = getAddRecExpr(StartVal, Accum, L, SCEV::FlagAnyWrap); |
0 |
5608 |
auto *NewAR = getAddRecExpr(StartVal, Accum, L, SCEV::FlagAnyWrap); |
0 |
| 5609 |
|
--- |
5609 |
|
--- |
| 5610 |
std::pair> PredRewrite = |
--- |
5610 |
std::pair> PredRewrite = |
--- |
| 5611 |
std::make_pair(NewAR, Predicates); |
0 |
5611 |
std::make_pair(NewAR, Predicates); |
0 |
| 5612 |
// Remember the result of the analysis for this SCEV at this locayyytion. |
--- |
5612 |
// Remember the result of the analysis for this SCEV at this locayyytion. |
--- |
| 5613 |
PredicatedSCEVRewrites[{SymbolicPHI, L}] = PredRewrite; |
0 |
5613 |
PredicatedSCEVRewrites[{SymbolicPHI, L}] = PredRewrite; |
0 |
| 5614 |
return PredRewrite; |
0 |
5614 |
return PredRewrite; |
0 |
| 5615 |
} |
0 |
5615 |
} |
0 |
| 5616 |
|
--- |
5616 |
|
--- |
| 5617 |
std::optional>> |
--- |
5617 |
std::optional>> |
--- |
| 5618 |
ScalarEvolution::createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI) { |
0 |
5618 |
ScalarEvolution::createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI) { |
0 |
| 5619 |
auto *PN = cast(SymbolicPHI->getValue()); |
0 |
5619 |
auto *PN = cast(SymbolicPHI->getValue()); |
0 |
| 5620 |
const Loop *L = isIntegerLoopHeaderPHI(PN, LI); |
0 |
5620 |
const Loop *L = isIntegerLoopHeaderPHI(PN, LI); |
0 |
| 5621 |
if (!L) |
0 |
5621 |
if (!L) |
0 |
| 5622 |
return std::nullopt; |
0 |
5622 |
return std::nullopt; |
0 |
| 5623 |
|
--- |
5623 |
|
--- |
| 5624 |
// Check to see if we already analyzed this PHI. |
--- |
5624 |
// Check to see if we already analyzed this PHI. |
--- |
| 5625 |
auto I = PredicatedSCEVRewrites.find({SymbolicPHI, L}); |
0 |
5625 |
auto I = PredicatedSCEVRewrites.find({SymbolicPHI, L}); |
0 |
| 5626 |
if (I != PredicatedSCEVRewrites.end()) { |
0 |
5626 |
if (I != PredicatedSCEVRewrites.end()) { |
0 |
| 5627 |
std::pair> Rewrite = |
--- |
5627 |
std::pair> Rewrite = |
--- |
| 5628 |
I->second; |
0 |
5628 |
I->second; |
0 |
| 5629 |
// Analysis was done before and failed to create an AddRec: |
--- |
5629 |
// Analysis was done before and failed to create an AddRec: |
--- |
| 5630 |
if (Rewrite.first == SymbolicPHI) |
0 |
5630 |
if (Rewrite.first == SymbolicPHI) |
0 |
| 5631 |
return std::nullopt; |
0 |
5631 |
return std::nullopt; |
0 |
| 5632 |
// Analysis was done before and succeeded to create an AddRec under |
--- |
5632 |
// Analysis was done before and succeeded to create an AddRec under |
--- |
| 5633 |
// a predicate: |
--- |
5633 |
// a predicate: |
--- |
| 5634 |
assert(isa(Rewrite.first) && "Expected an AddRec"); |
0 |
5634 |
assert(isa(Rewrite.first) && "Expected an AddRec"); |
0 |
| 5635 |
assert(!(Rewrite.second).empty() && "Expected to find Predicates"); |
0 |
5635 |
assert(!(Rewrite.second).empty() && "Expected to find Predicates"); |
0 |
| 5636 |
return Rewrite; |
0 |
5636 |
return Rewrite; |
0 |
| 5637 |
} |
0 |
5637 |
} |
0 |
| 5638 |
|
--- |
5638 |
|
--- |
| 5639 |
std::optional>> |
--- |
5639 |
std::optional>> |
--- |
| 5640 |
Rewrite = createAddRecFromPHIWithCastsImpl(SymbolicPHI); |
0 |
5640 |
Rewrite = createAddRecFromPHIWithCastsImpl(SymbolicPHI); |
0 |
| 5641 |
|
--- |
5641 |
|
--- |
| 5642 |
// Record in the cache that the analysis failed |
--- |
5642 |
// Record in the cache that the analysis failed |
--- |
| 5643 |
if (!Rewrite) { |
0 |
5643 |
if (!Rewrite) { |
0 |
| 5644 |
SmallVector Predicates; |
0 |
5644 |
SmallVector Predicates; |
0 |
| 5645 |
PredicatedSCEVRewrites[{SymbolicPHI, L}] = {SymbolicPHI, Predicates}; |
0 |
5645 |
PredicatedSCEVRewrites[{SymbolicPHI, L}] = {SymbolicPHI, Predicates}; |
0 |
| 5646 |
return std::nullopt; |
0 |
5646 |
return std::nullopt; |
0 |
| 5647 |
} |
0 |
5647 |
} |
0 |
| 5648 |
|
--- |
5648 |
|
--- |
| 5649 |
return Rewrite; |
0 |
5649 |
return Rewrite; |
0 |
| 5650 |
} |
0 |
5650 |
} |
0 |
| 5651 |
|
--- |
5651 |
|
--- |
| 5652 |
// FIXME: This utility is currently required because the Rewriter currently |
--- |
5652 |
// FIXME: This utility is currently required because the Rewriter currently |
--- |
| 5653 |
// does not rewrite this expression: |
--- |
5653 |
// does not rewrite this expression: |
--- |
| 5654 |
// {0, +, (sext ix (trunc iy to ix) to iy)} |
--- |
5654 |
// {0, +, (sext ix (trunc iy to ix) to iy)} |
--- |
| 5655 |
// into {0, +, %step}, |
--- |
5655 |
// into {0, +, %step}, |
--- |
| 5656 |
// even when the following Equal predicate exists: |
--- |
5656 |
// even when the following Equal predicate exists: |
--- |
| 5657 |
// "%step == (sext ix (trunc iy to ix) to iy)". |
--- |
5657 |
// "%step == (sext ix (trunc iy to ix) to iy)". |
--- |
| 5658 |
bool PredicatedScalarEvolution::areAddRecsEqualWithPreds( |
0 |
5658 |
bool PredicatedScalarEvolution::areAddRecsEqualWithPreds( |
0 |
| 5659 |
const SCEVAddRecExpr *AR1, const SCEVAddRecExpr *AR2) const { |
--- |
5659 |
const SCEVAddRecExpr *AR1, const SCEVAddRecExpr *AR2) const { |
--- |
| 5660 |
if (AR1 == AR2) |
0 |
5660 |
if (AR1 == AR2) |
0 |
| 5661 |
return true; |
0 |
5661 |
return true; |
0 |
| 5662 |
|
--- |
5662 |
|
--- |
| 5663 |
auto areExprsEqual = [&](const SCEV *Expr1, const SCEV *Expr2) -> bool { |
0 |
5663 |
auto areExprsEqual = [&](const SCEV *Expr1, const SCEV *Expr2) -> bool { |
0 |
| 5664 |
if (Expr1 != Expr2 && !Preds->implies(SE.getEqualPredicate(Expr1, Expr2)) && |
0 |
5664 |
if (Expr1 != Expr2 && !Preds->implies(SE.getEqualPredicate(Expr1, Expr2)) && |
0 |
| 5665 |
!Preds->implies(SE.getEqualPredicate(Expr2, Expr1))) |
0 |
5665 |
!Preds->implies(SE.getEqualPredicate(Expr2, Expr1))) |
0 |
| 5666 |
return false; |
0 |
5666 |
return false; |
0 |
| 5667 |
return true; |
0 |
5667 |
return true; |
0 |
| 5668 |
}; |
0 |
5668 |
}; |
0 |
| 5669 |
|
--- |
5669 |
|
--- |
| 5670 |
if (!areExprsEqual(AR1->getStart(), AR2->getStart()) || |
0 |
5670 |
if (!areExprsEqual(AR1->getStart(), AR2->getStart()) || |
0 |
| 5671 |
!areExprsEqual(AR1->getStepRecurrence(SE), AR2->getStepRecurrence(SE))) |
0 |
5671 |
!areExprsEqual(AR1->getStepRecurrence(SE), AR2->getStepRecurrence(SE))) |
0 |
| 5672 |
return false; |
0 |
5672 |
return false; |
0 |
| 5673 |
return true; |
0 |
5673 |
return true; |
0 |
| 5674 |
} |
--- |
5674 |
} |
--- |
| 5675 |
|
--- |
5675 |
|
--- |
| 5676 |
/// A helper function for createAddRecFromPHI to handle simple cases. |
--- |
5676 |
/// A helper function for createAddRecFromPHI to handle simple cases. |
--- |
| 5677 |
/// |
--- |
5677 |
/// |
--- |
| 5678 |
/// This function tries to find an AddRec expression for the simplest (yet most |
--- |
5678 |
/// This function tries to find an AddRec expression for the simplest (yet most |
--- |
| 5679 |
/// common) cases: PN = PHI(Start, OP(Self, LoopInvariant)). |
--- |
5679 |
/// common) cases: PN = PHI(Start, OP(Self, LoopInvariant)). |
--- |
| 5680 |
/// If it fails, createAddRecFromPHI will use a more general, but slow, |
--- |
5680 |
/// If it fails, createAddRecFromPHI will use a more general, but slow, |
--- |
| 5681 |
/// technique for finding the AddRec expression. |
--- |
5681 |
/// technique for finding the AddRec expression. |
--- |
| 5682 |
const SCEV *ScalarEvolution::createSimpleAffineAddRec(PHINode *PN, |
0 |
5682 |
const SCEV *ScalarEvolution::createSimpleAffineAddRec(PHINode *PN, |
0 |
| 5683 |
Value *BEValueV, |
--- |
5683 |
Value *BEValueV, |
--- |
| 5684 |
Value *StartValueV) { |
--- |
5684 |
Value *StartValueV) { |
--- |
| 5685 |
const Loop *L = LI.getLoopFor(PN->getParent()); |
0 |
5685 |
const Loop *L = LI.getLoopFor(PN->getParent()); |
0 |
| 5686 |
assert(L && L->getHeader() == PN->getParent()); |
0 |
5686 |
assert(L && L->getHeader() == PN->getParent()); |
0 |
| 5687 |
assert(BEValueV && StartValueV); |
0 |
5687 |
assert(BEValueV && StartValueV); |
0 |
| 5688 |
|
--- |
5688 |
|
--- |
| 5689 |
auto BO = MatchBinaryOp(BEValueV, getDataLayout(), AC, DT, PN); |
0 |
5689 |
auto BO = MatchBinaryOp(BEValueV, getDataLayout(), AC, DT, PN); |
0 |
| 5690 |
if (!BO) |
0 |
5690 |
if (!BO) |
0 |
| 5691 |
return nullptr; |
0 |
5691 |
return nullptr; |
0 |
| 5692 |
|
--- |
5692 |
|
--- |
| 5693 |
if (BO->Opcode != Instruction::Add) |
0 |
5693 |
if (BO->Opcode != Instruction::Add) |
0 |
| 5694 |
return nullptr; |
0 |
5694 |
return nullptr; |
0 |
| 5695 |
|
--- |
5695 |
|
--- |
| 5696 |
const SCEV *Accum = nullptr; |
0 |
5696 |
const SCEV *Accum = nullptr; |
0 |
| 5697 |
if (BO->LHS == PN && L->isLoopInvariant(BO->RHS)) |
0 |
5697 |
if (BO->LHS == PN && L->isLoopInvariant(BO->RHS)) |
0 |
| 5698 |
Accum = getSCEV(BO->RHS); |
0 |
5698 |
Accum = getSCEV(BO->RHS); |
0 |
| 5699 |
else if (BO->RHS == PN && L->isLoopInvariant(BO->LHS)) |
0 |
5699 |
else if (BO->RHS == PN && L->isLoopInvariant(BO->LHS)) |
0 |
| 5700 |
Accum = getSCEV(BO->LHS); |
0 |
5700 |
Accum = getSCEV(BO->LHS); |
0 |
| 5701 |
|
--- |
5701 |
|
--- |
| 5702 |
if (!Accum) |
0 |
5702 |
if (!Accum) |
0 |
| 5703 |
return nullptr; |
0 |
5703 |
return nullptr; |
0 |
| 5704 |
|
--- |
5704 |
|
--- |
| 5705 |
SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; |
0 |
5705 |
SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; |
0 |
| 5706 |
if (BO->IsNUW) |
0 |
5706 |
if (BO->IsNUW) |
0 |
| 5707 |
Flags = setFlags(Flags, SCEV::FlagNUW); |
0 |
5707 |
Flags = setFlags(Flags, SCEV::FlagNUW); |
0 |
| 5708 |
if (BO->IsNSW) |
0 |
5708 |
if (BO->IsNSW) |
0 |
| 5709 |
Flags = setFlags(Flags, SCEV::FlagNSW); |
0 |
5709 |
Flags = setFlags(Flags, SCEV::FlagNSW); |
0 |
| 5710 |
|
--- |
5710 |
|
--- |
| 5711 |
const SCEV *StartVal = getSCEV(StartValueV); |
0 |
5711 |
const SCEV *StartVal = getSCEV(StartValueV); |
0 |
| 5712 |
const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags); |
0 |
5712 |
const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags); |
0 |
| 5713 |
insertValueToMap(PN, PHISCEV); |
0 |
5713 |
insertValueToMap(PN, PHISCEV); |
0 |
| 5714 |
|
--- |
5714 |
|
--- |
| 5715 |
if (auto *AR = dyn_cast(PHISCEV)) { |
0 |
5715 |
if (auto *AR = dyn_cast(PHISCEV)) { |
0 |
| 5716 |
setNoWrapFlags(const_cast(AR), |
0 |
5716 |
setNoWrapFlags(const_cast(AR), |
0 |
| 5717 |
(SCEV::NoWrapFlags)(AR->getNoWrapFlags() | |
0 |
5717 |
(SCEV::NoWrapFlags)(AR->getNoWrapFlags() | |
0 |
| 5718 |
proveNoWrapViaConstantRanges(AR))); |
0 |
5718 |
proveNoWrapViaConstantRanges(AR))); |
0 |
| 5719 |
} |
--- |
5719 |
} |
--- |
| 5720 |
|
--- |
5720 |
|
--- |
| 5721 |
// We can add Flags to the post-inc expression only if we |
--- |
5721 |
// We can add Flags to the post-inc expression only if we |
--- |
| 5722 |
// know that it is *undefined behavior* for BEValueV to |
--- |
5722 |
// know that it is *undefined behavior* for BEValueV to |
--- |
| 5723 |
// overflow. |
--- |
5723 |
// overflow. |
--- |
| 5724 |
if (auto *BEInst = dyn_cast(BEValueV)) { |
0 |
5724 |
if (auto *BEInst = dyn_cast(BEValueV)) { |
0 |
| 5725 |
assert(isLoopInvariant(Accum, L) && |
0 |
5725 |
assert(isLoopInvariant(Accum, L) && |
0 |
| 5726 |
"Accum is defined outside L, but is not invariant?"); |
--- |
5726 |
"Accum is defined outside L, but is not invariant?"); |
--- |
| 5727 |
if (isAddRecNeverPoison(BEInst, L)) |
0 |
5727 |
if (isAddRecNeverPoison(BEInst, L)) |
0 |
| 5728 |
(void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags); |
0 |
5728 |
(void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags); |
0 |
| 5729 |
} |
--- |
5729 |
} |
--- |
| 5730 |
|
--- |
5730 |
|
--- |
| 5731 |
return PHISCEV; |
0 |
5731 |
return PHISCEV; |
0 |
| 5732 |
} |
--- |
5732 |
} |
--- |
| 5733 |
|
--- |
5733 |
|
--- |
| 5734 |
const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) { |
0 |
5734 |
const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) { |
0 |
| 5735 |
const Loop *L = LI.getLoopFor(PN->getParent()); |
0 |
5735 |
const Loop *L = LI.getLoopFor(PN->getParent()); |
0 |
| 5736 |
if (!L || L->getHeader() != PN->getParent()) |
0 |
5736 |
if (!L || L->getHeader() != PN->getParent()) |
0 |
| 5737 |
return nullptr; |
0 |
5737 |
return nullptr; |
0 |
| 5738 |
|
--- |
5738 |
|
--- |
| 5739 |
// The loop may have multiple entrances or multiple exits; we can analyze |
--- |
5739 |
// The loop may have multiple entrances or multiple exits; we can analyze |
--- |
| 5740 |
// this phi as an addrec if it has a unique entry value and a unique |
--- |
5740 |
// this phi as an addrec if it has a unique entry value and a unique |
--- |
| 5741 |
// backedge value. |
--- |
5741 |
// backedge value. |
--- |
| 5742 |
Value *BEValueV = nullptr, *StartValueV = nullptr; |
0 |
5742 |
Value *BEValueV = nullptr, *StartValueV = nullptr; |
0 |
| 5743 |
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
0 |
5743 |
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
0 |
| 5744 |
Value *V = PN->getIncomingValue(i); |
0 |
5744 |
Value *V = PN->getIncomingValue(i); |
0 |
| 5745 |
if (L->contains(PN->getIncomingBlock(i))) { |
0 |
5745 |
if (L->contains(PN->getIncomingBlock(i))) { |
0 |
| 5746 |
if (!BEValueV) { |
0 |
5746 |
if (!BEValueV) { |
0 |
| 5747 |
BEValueV = V; |
0 |
5747 |
BEValueV = V; |
0 |
| 5748 |
} else if (BEValueV != V) { |
0 |
5748 |
} else if (BEValueV != V) { |
0 |
| 5749 |
BEValueV = nullptr; |
0 |
5749 |
BEValueV = nullptr; |
0 |
| 5750 |
break; |
0 |
5750 |
break; |
0 |
| 5751 |
} |
--- |
5751 |
} |
--- |
| 5752 |
} else if (!StartValueV) { |
0 |
5752 |
} else if (!StartValueV) { |
0 |
| 5753 |
StartValueV = V; |
0 |
5753 |
StartValueV = V; |
0 |
| 5754 |
} else if (StartValueV != V) { |
0 |
5754 |
} else if (StartValueV != V) { |
0 |
| 5755 |
StartValueV = nullptr; |
0 |
5755 |
StartValueV = nullptr; |
0 |
| 5756 |
break; |
0 |
5756 |
break; |
0 |
| 5757 |
} |
--- |
5757 |
} |
--- |
| 5758 |
} |
--- |
5758 |
} |
--- |
| 5759 |
if (!BEValueV || !StartValueV) |
0 |
5759 |
if (!BEValueV || !StartValueV) |
0 |
| 5760 |
return nullptr; |
0 |
5760 |
return nullptr; |
0 |
| 5761 |
|
--- |
5761 |
|
--- |
| 5762 |
assert(ValueExprMap.find_as(PN) == ValueExprMap.end() && |
0 |
5762 |
assert(ValueExprMap.find_as(PN) == ValueExprMap.end() && |
0 |
| 5763 |
"PHI node already processed?"); |
--- |
5763 |
"PHI node already processed?"); |
--- |
| 5764 |
|
--- |
5764 |
|
--- |
| 5765 |
// First, try to find AddRec expression without creating a fictituos symbolic |
--- |
5765 |
// First, try to find AddRec expression without creating a fictituos symbolic |
--- |
| 5766 |
// value for PN. |
--- |
5766 |
// value for PN. |
--- |
| 5767 |
if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV)) |
0 |
5767 |
if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV)) |
0 |
| 5768 |
return S; |
0 |
5768 |
return S; |
0 |
| 5769 |
|
--- |
5769 |
|
--- |
| 5770 |
// Handle PHI node value symbolically. |
--- |
5770 |
// Handle PHI node value symbolically. |
--- |
| 5771 |
const SCEV *SymbolicName = getUnknown(PN); |
0 |
5771 |
const SCEV *SymbolicName = getUnknown(PN); |
0 |
| 5772 |
insertValueToMap(PN, SymbolicName); |
0 |
5772 |
insertValueToMap(PN, SymbolicName); |
0 |
| 5773 |
|
--- |
5773 |
|
--- |
| 5774 |
// Using this symbolic name for the PHI, analyze the value coming around |
--- |
5774 |
// Using this symbolic name for the PHI, analyze the value coming around |
--- |
| 5775 |
// the back-edge. |
--- |
5775 |
// the back-edge. |
--- |
| 5776 |
const SCEV *BEValue = getSCEV(BEValueV); |
0 |
5776 |
const SCEV *BEValue = getSCEV(BEValueV); |
0 |
| 5777 |
|
--- |
5777 |
|
--- |
| 5778 |
// NOTE: If BEValue is loop invariant, we know that the PHI node just |
--- |
5778 |
// NOTE: If BEValue is loop invariant, we know that the PHI node just |
--- |
| 5779 |
// has a special value for the first iteration of the loop. |
--- |
5779 |
// has a special value for the first iteration of the loop. |
--- |
| 5780 |
|
--- |
5780 |
|
--- |
| 5781 |
// If the value coming around the backedge is an add with the symbolic |
--- |
5781 |
// If the value coming around the backedge is an add with the symbolic |
--- |
| 5782 |
// value we just inserted, then we found a simple induction variable! |
--- |
5782 |
// value we just inserted, then we found a simple induction variable! |
--- |
| 5783 |
if (const SCEVAddExpr *Add = dyn_cast(BEValue)) { |
0 |
5783 |
if (const SCEVAddExpr *Add = dyn_cast(BEValue)) { |
0 |
| 5784 |
// If there is a single occurrence of the symbolic value, replace it |
--- |
5784 |
// If there is a single occurrence of the symbolic value, replace it |
--- |
| 5785 |
// with a recurrence. |
--- |
5785 |
// with a recurrence. |
--- |
| 5786 |
unsigned FoundIndex = Add->getNumOperands(); |
0 |
5786 |
unsigned FoundIndex = Add->getNumOperands(); |
0 |
| 5787 |
for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
0 |
5787 |
for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
0 |
| 5788 |
if (Add->getOperand(i) == SymbolicName) |
0 |
5788 |
if (Add->getOperand(i) == SymbolicName) |
0 |
| 5789 |
if (FoundIndex == e) { |
0 |
5789 |
if (FoundIndex == e) { |
0 |
| 5790 |
FoundIndex = i; |
0 |
5790 |
FoundIndex = i; |
0 |
| 5791 |
break; |
0 |
5791 |
break; |
0 |
| 5792 |
} |
--- |
5792 |
} |
--- |
| 5793 |
|
--- |
5793 |
|
--- |
| 5794 |
if (FoundIndex != Add->getNumOperands()) { |
0 |
5794 |
if (FoundIndex != Add->getNumOperands()) { |
0 |
| 5795 |
// Create an add with everything but the specified operand. |
--- |
5795 |
// Create an add with everything but the specified operand. |
--- |
| 5796 |
SmallVector Ops; |
0 |
5796 |
SmallVector Ops; |
0 |
| 5797 |
for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
0 |
5797 |
for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
0 |
| 5798 |
if (i != FoundIndex) |
0 |
5798 |
if (i != FoundIndex) |
0 |
| 5799 |
Ops.push_back(SCEVBackedgeConditionFolder::rewrite(Add->getOperand(i), |
0 |
5799 |
Ops.push_back(SCEVBackedgeConditionFolder::rewrite(Add->getOperand(i), |
0 |
| 5800 |
L, *this)); |
--- |
5800 |
L, *this)); |
--- |
| 5801 |
const SCEV *Accum = getAddExpr(Ops); |
0 |
5801 |
const SCEV *Accum = getAddExpr(Ops); |
0 |
| 5802 |
|
--- |
5802 |
|
--- |
| 5803 |
// This is not a valid addrec if the step amount is varying each |
--- |
5803 |
// This is not a valid addrec if the step amount is varying each |
--- |
| 5804 |
// loop iteration, but is not itself an addrec in this loop. |
--- |
5804 |
// loop iteration, but is not itself an addrec in this loop. |
--- |
| 5805 |
if (isLoopInvariant(Accum, L) || |
0 |
5805 |
if (isLoopInvariant(Accum, L) || |
0 |
| 5806 |
(isa(Accum) && |
0 |
5806 |
(isa(Accum) && |
0 |
| 5807 |
cast(Accum)->getLoop() == L)) { |
0 |
5807 |
cast(Accum)->getLoop() == L)) { |
0 |
| 5808 |
SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; |
0 |
5808 |
SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; |
0 |
| 5809 |
|
--- |
5809 |
|
--- |
| 5810 |
if (auto BO = MatchBinaryOp(BEValueV, getDataLayout(), AC, DT, PN)) { |
0 |
5810 |
if (auto BO = MatchBinaryOp(BEValueV, getDataLayout(), AC, DT, PN)) { |
0 |
| 5811 |
if (BO->Opcode == Instruction::Add && BO->LHS == PN) { |
0 |
5811 |
if (BO->Opcode == Instruction::Add && BO->LHS == PN) { |
0 |
| 5812 |
if (BO->IsNUW) |
0 |
5812 |
if (BO->IsNUW) |
0 |
| 5813 |
Flags = setFlags(Flags, SCEV::FlagNUW); |
0 |
5813 |
Flags = setFlags(Flags, SCEV::FlagNUW); |
0 |
| 5814 |
if (BO->IsNSW) |
0 |
5814 |
if (BO->IsNSW) |
0 |
| 5815 |
Flags = setFlags(Flags, SCEV::FlagNSW); |
0 |
5815 |
Flags = setFlags(Flags, SCEV::FlagNSW); |
0 |
| 5816 |
} |
--- |
5816 |
} |
--- |
| 5817 |
} else if (GEPOperator *GEP = dyn_cast(BEValueV)) { |
0 |
5817 |
} else if (GEPOperator *GEP = dyn_cast(BEValueV)) { |
0 |
| 5818 |
// If the increment is an inbounds GEP, then we know the address |
--- |
5818 |
// If the increment is an inbounds GEP, then we know the address |
--- |
| 5819 |
// space cannot be wrapped around. We cannot make any guarantee |
--- |
5819 |
// space cannot be wrapped around. We cannot make any guarantee |
--- |
| 5820 |
// about signed or unsigned overflow because pointers are |
--- |
5820 |
// about signed or unsigned overflow because pointers are |
--- |
| 5821 |
// unsigned but we may have a negative index from the base |
--- |
5821 |
// unsigned but we may have a negative index from the base |
--- |
| 5822 |
// pointer. We can guarantee that no unsigned wrap occurs if the |
--- |
5822 |
// pointer. We can guarantee that no unsigned wrap occurs if the |
--- |
| 5823 |
// indices form a positive value. |
--- |
5823 |
// indices form a positive value. |
--- |
| 5824 |
if (GEP->isInBounds() && GEP->getOperand(0) == PN) { |
0 |
5824 |
if (GEP->isInBounds() && GEP->getOperand(0) == PN) { |
0 |
| 5825 |
Flags = setFlags(Flags, SCEV::FlagNW); |
0 |
5825 |
Flags = setFlags(Flags, SCEV::FlagNW); |
0 |
| 5826 |
if (isKnownPositive(Accum)) |
0 |
5826 |
if (isKnownPositive(Accum)) |
0 |
| 5827 |
Flags = setFlags(Flags, SCEV::FlagNUW); |
0 |
5827 |
Flags = setFlags(Flags, SCEV::FlagNUW); |
0 |
| 5828 |
} |
--- |
5828 |
} |
--- |
| 5829 |
|
--- |
5829 |
|
--- |
| 5830 |
// We cannot transfer nuw and nsw flags from subtraction |
--- |
5830 |
// We cannot transfer nuw and nsw flags from subtraction |
--- |
| 5831 |
// operations -- sub nuw X, Y is not the same as add nuw X, -Y |
--- |
5831 |
// operations -- sub nuw X, Y is not the same as add nuw X, -Y |
--- |
| 5832 |
// for instance. |
--- |
5832 |
// for instance. |
--- |
| 5833 |
} |
--- |
5833 |
} |
--- |
| 5834 |
|
--- |
5834 |
|
--- |
| 5835 |
const SCEV *StartVal = getSCEV(StartValueV); |
0 |
5835 |
const SCEV *StartVal = getSCEV(StartValueV); |
0 |
| 5836 |
const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags); |
0 |
5836 |
const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags); |
0 |
| 5837 |
|
--- |
5837 |
|
--- |
| 5838 |
// Okay, for the entire analysis of this edge we assumed the PHI |
--- |
5838 |
// Okay, for the entire analysis of this edge we assumed the PHI |
--- |
| 5839 |
// to be symbolic. We now need to go back and purge all of the |
--- |
5839 |
// to be symbolic. We now need to go back and purge all of the |
--- |
| 5840 |
// entries for the scalars that use the symbolic expression. |
--- |
5840 |
// entries for the scalars that use the symbolic expression. |
--- |
| 5841 |
forgetMemoizedResults(SymbolicName); |
0 |
5841 |
forgetMemoizedResults(SymbolicName); |
0 |
| 5842 |
insertValueToMap(PN, PHISCEV); |
0 |
5842 |
insertValueToMap(PN, PHISCEV); |
0 |
| 5843 |
|
--- |
5843 |
|
--- |
| 5844 |
if (auto *AR = dyn_cast(PHISCEV)) { |
0 |
5844 |
if (auto *AR = dyn_cast(PHISCEV)) { |
0 |
| 5845 |
setNoWrapFlags(const_cast(AR), |
0 |
5845 |
setNoWrapFlags(const_cast(AR), |
0 |
| 5846 |
(SCEV::NoWrapFlags)(AR->getNoWrapFlags() | |
0 |
5846 |
(SCEV::NoWrapFlags)(AR->getNoWrapFlags() | |
0 |
| 5847 |
proveNoWrapViaConstantRanges(AR))); |
0 |
5847 |
proveNoWrapViaConstantRanges(AR))); |
0 |
| 5848 |
} |
--- |
5848 |
} |
--- |
| 5849 |
|
--- |
5849 |
|
--- |
| 5850 |
// We can add Flags to the post-inc expression only if we |
--- |
5850 |
// We can add Flags to the post-inc expression only if we |
--- |
| 5851 |
// know that it is *undefined behavior* for BEValueV to |
--- |
5851 |
// know that it is *undefined behavior* for BEValueV to |
--- |
| 5852 |
// overflow. |
--- |
5852 |
// overflow. |
--- |
| 5853 |
if (auto *BEInst = dyn_cast(BEValueV)) |
0 |
5853 |
if (auto *BEInst = dyn_cast(BEValueV)) |
0 |
| 5854 |
if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L)) |
0 |
5854 |
if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L)) |
0 |
| 5855 |
(void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags); |
0 |
5855 |
(void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags); |
0 |
| 5856 |
|
--- |
5856 |
|
--- |
| 5857 |
return PHISCEV; |
0 |
5857 |
return PHISCEV; |
0 |
| 5858 |
} |
--- |
5858 |
} |
--- |
| 5859 |
} |
0 |
5859 |
} |
0 |
| 5860 |
} else { |
--- |
5860 |
} else { |
--- |
| 5861 |
// Otherwise, this could be a loop like this: |
--- |
5861 |
// Otherwise, this could be a loop like this: |
--- |
| 5862 |
// i = 0; for (j = 1; ..; ++j) { .... i = j; } |
--- |
5862 |
// i = 0; for (j = 1; ..; ++j) { .... i = j; } |
--- |
| 5863 |
// In this case, j = {1,+,1} and BEValue is j. |
--- |
5863 |
// In this case, j = {1,+,1} and BEValue is j. |
--- |
| 5864 |
// Because the other in-value of i (0) fits the evolution of BEValue |
--- |
5864 |
// Because the other in-value of i (0) fits the evolution of BEValue |
--- |
| 5865 |
// i really is an addrec evolution. |
--- |
5865 |
// i really is an addrec evolution. |
--- |
| 5866 |
// |
--- |
5866 |
// |
--- |
| 5867 |
// We can generalize this saying that i is the shifted value of BEValue |
--- |
5867 |
// We can generalize this saying that i is the shifted value of BEValue |
--- |
| 5868 |
// by one iteration: |
--- |
5868 |
// by one iteration: |
--- |
| 5869 |
// PHI(f(0), f({1,+,1})) --> f({0,+,1}) |
--- |
5869 |
// PHI(f(0), f({1,+,1})) --> f({0,+,1}) |
--- |
| 5870 |
const SCEV *Shifted = SCEVShiftRewriter::rewrite(BEValue, L, *this); |
0 |
5870 |
const SCEV *Shifted = SCEVShiftRewriter::rewrite(BEValue, L, *this); |
0 |
| 5871 |
const SCEV *Start = SCEVInitRewriter::rewrite(Shifted, L, *this, false); |
0 |
5871 |
const SCEV *Start = SCEVInitRewriter::rewrite(Shifted, L, *this, false); |
0 |
| 5872 |
if (Shifted != getCouldNotCompute() && |
0 |
5872 |
if (Shifted != getCouldNotCompute() && |
0 |
| 5873 |
Start != getCouldNotCompute()) { |
0 |
5873 |
Start != getCouldNotCompute()) { |
0 |
| 5874 |
const SCEV *StartVal = getSCEV(StartValueV); |
0 |
5874 |
const SCEV *StartVal = getSCEV(StartValueV); |
0 |
| 5875 |
if (Start == StartVal) { |
0 |
5875 |
if (Start == StartVal) { |
0 |
| 5876 |
// Okay, for the entire analysis of this edge we assumed the PHI |
--- |
5876 |
// Okay, for the entire analysis of this edge we assumed the PHI |
--- |
| 5877 |
// to be symbolic. We now need to go back and purge all of the |
--- |
5877 |
// to be symbolic. We now need to go back and purge all of the |
--- |
| 5878 |
// entries for the scalars that use the symbolic expression. |
--- |
5878 |
// entries for the scalars that use the symbolic expression. |
--- |
| 5879 |
forgetMemoizedResults(SymbolicName); |
0 |
5879 |
forgetMemoizedResults(SymbolicName); |
0 |
| 5880 |
insertValueToMap(PN, Shifted); |
0 |
5880 |
insertValueToMap(PN, Shifted); |
0 |
| 5881 |
return Shifted; |
0 |
5881 |
return Shifted; |
0 |
| 5882 |
} |
--- |
5882 |
} |
--- |
| 5883 |
} |
--- |
5883 |
} |
--- |
| 5884 |
} |
--- |
5884 |
} |
--- |
| 5885 |
|
--- |
5885 |
|
--- |
| 5886 |
// Remove the temporary PHI node SCEV that has been inserted while intending |
--- |
5886 |
// Remove the temporary PHI node SCEV that has been inserted while intending |
--- |
| 5887 |
// to create an AddRecExpr for this PHI node. We can not keep this temporary |
--- |
5887 |
// to create an AddRecExpr for this PHI node. We can not keep this temporary |
--- |
| 5888 |
// as it will prevent later (possibly simpler) SCEV expressions to be added |
--- |
5888 |
// as it will prevent later (possibly simpler) SCEV expressions to be added |
--- |
| 5889 |
// to the ValueExprMap. |
--- |
5889 |
// to the ValueExprMap. |
--- |
| 5890 |
eraseValueFromMap(PN); |
0 |
5890 |
eraseValueFromMap(PN); |
0 |
| 5891 |
|
--- |
5891 |
|
--- |
| 5892 |
return nullptr; |
0 |
5892 |
return nullptr; |
0 |
| 5893 |
} |
--- |
5893 |
} |
--- |
| 5894 |
|
--- |
5894 |
|
--- |
| 5895 |
// Try to match a control flow sequence that branches out at BI and merges back |
--- |
5895 |
// Try to match a control flow sequence that branches out at BI and merges back |
--- |
| 5896 |
// at Merge into a "C ? LHS : RHS" select pattern. Return true on a successful |
--- |
5896 |
// at Merge into a "C ? LHS : RHS" select pattern. Return true on a successful |
--- |
| 5897 |
// match. |
--- |
5897 |
// match. |
--- |
| 5898 |
static bool BrPHIToSelect(DominatorTree &DT, BranchInst *BI, PHINode *Merge, |
0 |
5898 |
static bool BrPHIToSelect(DominatorTree &DT, BranchInst *BI, PHINode *Merge, |
0 |
| 5899 |
Value *&C, Value *&LHS, Value *&RHS) { |
--- |
5899 |
Value *&C, Value *&LHS, Value *&RHS) { |
--- |
| 5900 |
C = BI->getCondition(); |
0 |
5900 |
C = BI->getCondition(); |
0 |
| 5901 |
|
--- |
5901 |
|
--- |
| 5902 |
BasicBlockEdge LeftEdge(BI->getParent(), BI->getSuccessor(0)); |
0 |
5902 |
BasicBlockEdge LeftEdge(BI->getParent(), BI->getSuccessor(0)); |
0 |
| 5903 |
BasicBlockEdge RightEdge(BI->getParent(), BI->getSuccessor(1)); |
0 |
5903 |
BasicBlockEdge RightEdge(BI->getParent(), BI->getSuccessor(1)); |
0 |
| 5904 |
|
--- |
5904 |
|
--- |
| 5905 |
if (!LeftEdge.isSingleEdge()) |
0 |
5905 |
if (!LeftEdge.isSingleEdge()) |
0 |
| 5906 |
return false; |
0 |
5906 |
return false; |
0 |
| 5907 |
|
--- |
5907 |
|
--- |
| 5908 |
assert(RightEdge.isSingleEdge() && "Follows from LeftEdge.isSingleEdge()"); |
0 |
5908 |
assert(RightEdge.isSingleEdge() && "Follows from LeftEdge.isSingleEdge()"); |
0 |
| 5909 |
|
--- |
5909 |
|
--- |
| 5910 |
Use &LeftUse = Merge->getOperandUse(0); |
0 |
5910 |
Use &LeftUse = Merge->getOperandUse(0); |
0 |
| 5911 |
Use &RightUse = Merge->getOperandUse(1); |
0 |
5911 |
Use &RightUse = Merge->getOperandUse(1); |
0 |
| 5912 |
|
--- |
5912 |
|
--- |
| 5913 |
if (DT.dominates(LeftEdge, LeftUse) && DT.dominates(RightEdge, RightUse)) { |
0 |
5913 |
if (DT.dominates(LeftEdge, LeftUse) && DT.dominates(RightEdge, RightUse)) { |
0 |
| 5914 |
LHS = LeftUse; |
0 |
5914 |
LHS = LeftUse; |
0 |
| 5915 |
RHS = RightUse; |
0 |
5915 |
RHS = RightUse; |
0 |
| 5916 |
return true; |
0 |
5916 |
return true; |
0 |
| 5917 |
} |
--- |
5917 |
} |
--- |
| 5918 |
|
--- |
5918 |
|
--- |
| 5919 |
if (DT.dominates(LeftEdge, RightUse) && DT.dominates(RightEdge, LeftUse)) { |
0 |
5919 |
if (DT.dominates(LeftEdge, RightUse) && DT.dominates(RightEdge, LeftUse)) { |
0 |
| 5920 |
LHS = RightUse; |
0 |
5920 |
LHS = RightUse; |
0 |
| 5921 |
RHS = LeftUse; |
0 |
5921 |
RHS = LeftUse; |
0 |
| 5922 |
return true; |
0 |
5922 |
return true; |
0 |
| 5923 |
} |
--- |
5923 |
} |
--- |
| 5924 |
|
--- |
5924 |
|
--- |
| 5925 |
return false; |
0 |
5925 |
return false; |
0 |
| 5926 |
} |
--- |
5926 |
} |
--- |
| 5927 |
|
--- |
5927 |
|
--- |
| 5928 |
const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) { |
0 |
5928 |
const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) { |
0 |
| 5929 |
auto IsReachable = |
--- |
5929 |
auto IsReachable = |
--- |
| 5930 |
[&](BasicBlock *BB) { return DT.isReachableFromEntry(BB); }; |
0 |
5930 |
[&](BasicBlock *BB) { return DT.isReachableFromEntry(BB); }; |
0 |
| 5931 |
if (PN->getNumIncomingValues() == 2 && all_of(PN->blocks(), IsReachable)) { |
0 |
5931 |
if (PN->getNumIncomingValues() == 2 && all_of(PN->blocks(), IsReachable)) { |
0 |
| 5932 |
// Try to match |
--- |
5932 |
// Try to match |
--- |
| 5933 |
// |
--- |
5933 |
// |
--- |
| 5934 |
// br %cond, label %left, label %right |
--- |
5934 |
// br %cond, label %left, label %right |
--- |
| 5935 |
// left: |
--- |
5935 |
// left: |
--- |
| 5936 |
// br label %merge |
--- |
5936 |
// br label %merge |
--- |
| 5937 |
// right: |
--- |
5937 |
// right: |
--- |
| 5938 |
// br label %merge |
--- |
5938 |
// br label %merge |
--- |
| 5939 |
// merge: |
--- |
5939 |
// merge: |
--- |
| 5940 |
// V = phi [ %x, %left ], [ %y, %right ] |
--- |
5940 |
// V = phi [ %x, %left ], [ %y, %right ] |
--- |
| 5941 |
// |
--- |
5941 |
// |
--- |
| 5942 |
// as "select %cond, %x, %y" |
--- |
5942 |
// as "select %cond, %x, %y" |
--- |
| 5943 |
|
--- |
5943 |
|
--- |
| 5944 |
BasicBlock *IDom = DT[PN->getParent()]->getIDom()->getBlock(); |
0 |
5944 |
BasicBlock *IDom = DT[PN->getParent()]->getIDom()->getBlock(); |
0 |
| 5945 |
assert(IDom && "At least the entry block should dominate PN"); |
0 |
5945 |
assert(IDom && "At least the entry block should dominate PN"); |
0 |
| 5946 |
|
--- |
5946 |
|
--- |
| 5947 |
auto *BI = dyn_cast(IDom->getTerminator()); |
0 |
5947 |
auto *BI = dyn_cast(IDom->getTerminator()); |
0 |
| 5948 |
Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr; |
0 |
5948 |
Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr; |
0 |
| 5949 |
|
--- |
5949 |
|
--- |
| 5950 |
if (BI && BI->isConditional() && |
0 |
5950 |
if (BI && BI->isConditional() && |
0 |
| 5951 |
BrPHIToSelect(DT, BI, PN, Cond, LHS, RHS) && |
0 |
5951 |
BrPHIToSelect(DT, BI, PN, Cond, LHS, RHS) && |
0 |
| 5952 |
properlyDominates(getSCEV(LHS), PN->getParent()) && |
0 |
5952 |
properlyDominates(getSCEV(LHS), PN->getParent()) && |
0 |
| 5953 |
properlyDominates(getSCEV(RHS), PN->getParent())) |
0 |
5953 |
properlyDominates(getSCEV(RHS), PN->getParent())) |
0 |
| 5954 |
return createNodeForSelectOrPHI(PN, Cond, LHS, RHS); |
0 |
5954 |
return createNodeForSelectOrPHI(PN, Cond, LHS, RHS); |
0 |
| 5955 |
} |
--- |
5955 |
} |
--- |
| 5956 |
|
--- |
5956 |
|
--- |
| 5957 |
return nullptr; |
0 |
5957 |
return nullptr; |
0 |
| 5958 |
} |
--- |
5958 |
} |
--- |
| 5959 |
|
--- |
5959 |
|
--- |
| 5960 |
const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) { |
0 |
5960 |
const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) { |
0 |
| 5961 |
if (const SCEV *S = createAddRecFromPHI(PN)) |
0 |
5961 |
if (const SCEV *S = createAddRecFromPHI(PN)) |
0 |
| 5962 |
return S; |
0 |
5962 |
return S; |
0 |
| 5963 |
|
--- |
5963 |
|
--- |
| 5964 |
if (Value *V = simplifyInstruction(PN, {getDataLayout(), &TLI, &DT, &AC})) |
0 |
5964 |
if (Value *V = simplifyInstruction(PN, {getDataLayout(), &TLI, &DT, &AC})) |
0 |
| 5965 |
return getSCEV(V); |
0 |
5965 |
return getSCEV(V); |
0 |
| 5966 |
|
--- |
5966 |
|
--- |
| 5967 |
if (const SCEV *S = createNodeFromSelectLikePHI(PN)) |
0 |
5967 |
if (const SCEV *S = createNodeFromSelectLikePHI(PN)) |
0 |
| 5968 |
return S; |
0 |
5968 |
return S; |
0 |
| 5969 |
|
--- |
5969 |
|
--- |
| 5970 |
// If it's not a loop phi, we can't handle it yet. |
--- |
5970 |
// If it's not a loop phi, we can't handle it yet. |
--- |
| 5971 |
return getUnknown(PN); |
0 |
5971 |
return getUnknown(PN); |
0 |
| 5972 |
} |
--- |
5972 |
} |
--- |
| 5973 |
|
--- |
5973 |
|
--- |
| 5974 |
bool SCEVMinMaxExprContains(const SCEV *Root, const SCEV *OperandToFind, |
0 |
5974 |
bool SCEVMinMaxExprContains(const SCEV *Root, const SCEV *OperandToFind, |
0 |
| 5975 |
SCEVTypes RootKind) { |
--- |
5975 |
SCEVTypes RootKind) { |
--- |
| 5976 |
struct FindClosure { |
--- |
5976 |
struct FindClosure { |
--- |
| 5977 |
const SCEV *OperandToFind; |
--- |
5977 |
const SCEV *OperandToFind; |
--- |
| 5978 |
const SCEVTypes RootKind; // Must be a sequential min/max expression. |
--- |
5978 |
const SCEVTypes RootKind; // Must be a sequential min/max expression. |
--- |
| 5979 |
const SCEVTypes NonSequentialRootKind; // Non-seq variant of RootKind. |
--- |
5979 |
const SCEVTypes NonSequentialRootKind; // Non-seq variant of RootKind. |
--- |
| 5980 |
|
--- |
5980 |
|
--- |
| 5981 |
bool Found = false; |
--- |
5981 |
bool Found = false; |
--- |
| 5982 |
|
--- |
5982 |
|
--- |
| 5983 |
bool canRecurseInto(SCEVTypes Kind) const { |
0 |
5983 |
bool canRecurseInto(SCEVTypes Kind) const { |
0 |
| 5984 |
// We can only recurse into the SCEV expression of the same effective type |
--- |
5984 |
// We can only recurse into the SCEV expression of the same effective type |
--- |
| 5985 |
// as the type of our root SCEV expression, and into zero-extensions. |
--- |
5985 |
// as the type of our root SCEV expression, and into zero-extensions. |
--- |
| 5986 |
return RootKind == Kind || NonSequentialRootKind == Kind || |
0 |
5986 |
return RootKind == Kind || NonSequentialRootKind == Kind || |
0 |
| 5987 |
scZeroExtend == Kind; |
0 |
5987 |
scZeroExtend == Kind; |
0 |
| 5988 |
}; |
--- |
5988 |
}; |
--- |
| 5989 |
|
--- |
5989 |
|
--- |
| 5990 |
FindClosure(const SCEV *OperandToFind, SCEVTypes RootKind) |
0 |
5990 |
FindClosure(const SCEV *OperandToFind, SCEVTypes RootKind) |
0 |
| 5991 |
: OperandToFind(OperandToFind), RootKind(RootKind), |
0 |
5991 |
: OperandToFind(OperandToFind), RootKind(RootKind), |
0 |
| 5992 |
NonSequentialRootKind( |
0 |
5992 |
NonSequentialRootKind( |
0 |
| 5993 |
SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType( |
0 |
5993 |
SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType( |
0 |
| 5994 |
RootKind)) {} |
0 |
5994 |
RootKind)) {} |
0 |
| 5995 |
|
--- |
5995 |
|
--- |
| 5996 |
bool follow(const SCEV *S) { |
0 |
5996 |
bool follow(const SCEV *S) { |
0 |
| 5997 |
Found = S == OperandToFind; |
0 |
5997 |
Found = S == OperandToFind; |
0 |
| 5998 |
|
--- |
5998 |
|
--- |
| 5999 |
return !isDone() && canRecurseInto(S->getSCEVType()); |
0 |
5999 |
return !isDone() && canRecurseInto(S->getSCEVType()); |
0 |
| 6000 |
} |
--- |
6000 |
} |
--- |
| 6001 |
|
--- |
6001 |
|
--- |
| 6002 |
bool isDone() const { return Found; } |
0 |
6002 |
bool isDone() const { return Found; } |
0 |
| 6003 |
}; |
--- |
6003 |
}; |
--- |
| 6004 |
|
--- |
6004 |
|
--- |
| 6005 |
FindClosure FC(OperandToFind, RootKind); |
0 |
6005 |
FindClosure FC(OperandToFind, RootKind); |
0 |
| 6006 |
visitAll(Root, FC); |
0 |
6006 |
visitAll(Root, FC); |
0 |
| 6007 |
return FC.Found; |
0 |
6007 |
return FC.Found; |
0 |
| 6008 |
} |
--- |
6008 |
} |
--- |
| 6009 |
|
--- |
6009 |
|
--- |
| 6010 |
std::optional |
--- |
6010 |
std::optional |
--- |
| 6011 |
ScalarEvolution::createNodeForSelectOrPHIInstWithICmpInstCond(Type *Ty, |
0 |
6011 |
ScalarEvolution::createNodeForSelectOrPHIInstWithICmpInstCond(Type *Ty, |
0 |
| 6012 |
ICmpInst *Cond, |
--- |
6012 |
ICmpInst *Cond, |
--- |
| 6013 |
Value *TrueVal, |
--- |
6013 |
Value *TrueVal, |
--- |
| 6014 |
Value *FalseVal) { |
--- |
6014 |
Value *FalseVal) { |
--- |
| 6015 |
// Try to match some simple smax or umax patterns. |
--- |
6015 |
// Try to match some simple smax or umax patterns. |
--- |
| 6016 |
auto *ICI = Cond; |
0 |
6016 |
auto *ICI = Cond; |
0 |
| 6017 |
|
--- |
6017 |
|
--- |
| 6018 |
Value *LHS = ICI->getOperand(0); |
0 |
6018 |
Value *LHS = ICI->getOperand(0); |
0 |
| 6019 |
Value *RHS = ICI->getOperand(1); |
0 |
6019 |
Value *RHS = ICI->getOperand(1); |
0 |
| 6020 |
|
--- |
6020 |
|
--- |
| 6021 |
switch (ICI->getPredicate()) { |
0 |
6021 |
switch (ICI->getPredicate()) { |
0 |
| 6022 |
case ICmpInst::ICMP_SLT: |
0 |
6022 |
case ICmpInst::ICMP_SLT: |
0 |
| 6023 |
case ICmpInst::ICMP_SLE: |
--- |
6023 |
case ICmpInst::ICMP_SLE: |
--- |
| 6024 |
case ICmpInst::ICMP_ULT: |
--- |
6024 |
case ICmpInst::ICMP_ULT: |
--- |
| 6025 |
case ICmpInst::ICMP_ULE: |
--- |
6025 |
case ICmpInst::ICMP_ULE: |
--- |
| 6026 |
std::swap(LHS, RHS); |
0 |
6026 |
std::swap(LHS, RHS); |
0 |
| 6027 |
[[fallthrough]]; |
--- |
6027 |
[[fallthrough]]; |
--- |
| 6028 |
case ICmpInst::ICMP_SGT: |
0 |
6028 |
case ICmpInst::ICMP_SGT: |
0 |
| 6029 |
case ICmpInst::ICMP_SGE: |
--- |
6029 |
case ICmpInst::ICMP_SGE: |
--- |
| 6030 |
case ICmpInst::ICMP_UGT: |
--- |
6030 |
case ICmpInst::ICMP_UGT: |
--- |
| 6031 |
case ICmpInst::ICMP_UGE: |
--- |
6031 |
case ICmpInst::ICMP_UGE: |
--- |
| 6032 |
// a > b ? a+x : b+x -> max(a, b)+x |
--- |
6032 |
// a > b ? a+x : b+x -> max(a, b)+x |
--- |
| 6033 |
// a > b ? b+x : a+x -> min(a, b)+x |
--- |
6033 |
// a > b ? b+x : a+x -> min(a, b)+x |
--- |
| 6034 |
if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(Ty)) { |
0 |
6034 |
if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(Ty)) { |
0 |
| 6035 |
bool Signed = ICI->isSigned(); |
0 |
6035 |
bool Signed = ICI->isSigned(); |
0 |
| 6036 |
const SCEV *LA = getSCEV(TrueVal); |
0 |
6036 |
const SCEV *LA = getSCEV(TrueVal); |
0 |
| 6037 |
const SCEV *RA = getSCEV(FalseVal); |
0 |
6037 |
const SCEV *RA = getSCEV(FalseVal); |
0 |
| 6038 |
const SCEV *LS = getSCEV(LHS); |
0 |
6038 |
const SCEV *LS = getSCEV(LHS); |
0 |
| 6039 |
const SCEV *RS = getSCEV(RHS); |
0 |
6039 |
const SCEV *RS = getSCEV(RHS); |
0 |
| 6040 |
if (LA->getType()->isPointerTy()) { |
0 |
6040 |
if (LA->getType()->isPointerTy()) { |
0 |
| 6041 |
// FIXME: Handle cases where LS/RS are pointers not equal to LA/RA. |
--- |
6041 |
// FIXME: Handle cases where LS/RS are pointers not equal to LA/RA. |
--- |
| 6042 |
// Need to make sure we can't produce weird expressions involving |
--- |
6042 |
// Need to make sure we can't produce weird expressions involving |
--- |
| 6043 |
// negated pointers. |
--- |
6043 |
// negated pointers. |
--- |
| 6044 |
if (LA == LS && RA == RS) |
0 |
6044 |
if (LA == LS && RA == RS) |
0 |
| 6045 |
return Signed ? getSMaxExpr(LS, RS) : getUMaxExpr(LS, RS); |
0 |
6045 |
return Signed ? getSMaxExpr(LS, RS) : getUMaxExpr(LS, RS); |
0 |
| 6046 |
if (LA == RS && RA == LS) |
0 |
6046 |
if (LA == RS && RA == LS) |
0 |
| 6047 |
return Signed ? getSMinExpr(LS, RS) : getUMinExpr(LS, RS); |
0 |
6047 |
return Signed ? getSMinExpr(LS, RS) : getUMinExpr(LS, RS); |
0 |
| 6048 |
} |
--- |
6048 |
} |
--- |
| 6049 |
auto CoerceOperand = [&](const SCEV *Op) -> const SCEV * { |
0 |
6049 |
auto CoerceOperand = [&](const SCEV *Op) -> const SCEV * { |
0 |
| 6050 |
if (Op->getType()->isPointerTy()) { |
0 |
6050 |
if (Op->getType()->isPointerTy()) { |
0 |
| 6051 |
Op = getLosslessPtrToIntExpr(Op); |
0 |
6051 |
Op = getLosslessPtrToIntExpr(Op); |
0 |
| 6052 |
if (isa(Op)) |
0 |
6052 |
if (isa(Op)) |
0 |
| 6053 |
return Op; |
0 |
6053 |
return Op; |
0 |
| 6054 |
} |
--- |
6054 |
} |
--- |
| 6055 |
if (Signed) |
0 |
6055 |
if (Signed) |
0 |
| 6056 |
Op = getNoopOrSignExtend(Op, Ty); |
0 |
6056 |
Op = getNoopOrSignExtend(Op, Ty); |
0 |
| 6057 |
else |
--- |
6057 |
else |
--- |
| 6058 |
Op = getNoopOrZeroExtend(Op, Ty); |
0 |
6058 |
Op = getNoopOrZeroExtend(Op, Ty); |
0 |
| 6059 |
return Op; |
0 |
6059 |
return Op; |
0 |
| 6060 |
}; |
0 |
6060 |
}; |
0 |
| 6061 |
LS = CoerceOperand(LS); |
0 |
6061 |
LS = CoerceOperand(LS); |
0 |
| 6062 |
RS = CoerceOperand(RS); |
0 |
6062 |
RS = CoerceOperand(RS); |
0 |
| 6063 |
if (isa(LS) || isa(RS)) |
0 |
6063 |
if (isa(LS) || isa(RS)) |
0 |
| 6064 |
break; |
0 |
6064 |
break; |
0 |
| 6065 |
const SCEV *LDiff = getMinusSCEV(LA, LS); |
0 |
6065 |
const SCEV *LDiff = getMinusSCEV(LA, LS); |
0 |
| 6066 |
const SCEV *RDiff = getMinusSCEV(RA, RS); |
0 |
6066 |
const SCEV *RDiff = getMinusSCEV(RA, RS); |
0 |
| 6067 |
if (LDiff == RDiff) |
0 |
6067 |
if (LDiff == RDiff) |
0 |
| 6068 |
return getAddExpr(Signed ? getSMaxExpr(LS, RS) : getUMaxExpr(LS, RS), |
0 |
6068 |
return getAddExpr(Signed ? getSMaxExpr(LS, RS) : getUMaxExpr(LS, RS), |
0 |
| 6069 |
LDiff); |
0 |
6069 |
LDiff); |
0 |
| 6070 |
LDiff = getMinusSCEV(LA, RS); |
0 |
6070 |
LDiff = getMinusSCEV(LA, RS); |
0 |
| 6071 |
RDiff = getMinusSCEV(RA, LS); |
0 |
6071 |
RDiff = getMinusSCEV(RA, LS); |
0 |
| 6072 |
if (LDiff == RDiff) |
0 |
6072 |
if (LDiff == RDiff) |
0 |
| 6073 |
return getAddExpr(Signed ? getSMinExpr(LS, RS) : getUMinExpr(LS, RS), |
0 |
6073 |
return getAddExpr(Signed ? getSMinExpr(LS, RS) : getUMinExpr(LS, RS), |
0 |
| 6074 |
LDiff); |
0 |
6074 |
LDiff); |
0 |
| 6075 |
} |
--- |
6075 |
} |
--- |
| 6076 |
break; |
0 |
6076 |
break; |
0 |
| 6077 |
case ICmpInst::ICMP_NE: |
0 |
6077 |
case ICmpInst::ICMP_NE: |
0 |
| 6078 |
// x != 0 ? x+y : C+y -> x == 0 ? C+y : x+y |
--- |
6078 |
// x != 0 ? x+y : C+y -> x == 0 ? C+y : x+y |
--- |
| 6079 |
std::swap(TrueVal, FalseVal); |
0 |
6079 |
std::swap(TrueVal, FalseVal); |
0 |
| 6080 |
[[fallthrough]]; |
--- |
6080 |
[[fallthrough]]; |
--- |
| 6081 |
case ICmpInst::ICMP_EQ: |
0 |
6081 |
case ICmpInst::ICMP_EQ: |
0 |
| 6082 |
// x == 0 ? C+y : x+y -> umax(x, C)+y iff C u<= 1 |
--- |
6082 |
// x == 0 ? C+y : x+y -> umax(x, C)+y iff C u<= 1 |
--- |
| 6083 |
if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(Ty) && |
0 |
6083 |
if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(Ty) && |
0 |
| 6084 |
isa(RHS) && cast(RHS)->isZero()) { |
0 |
6084 |
isa(RHS) && cast(RHS)->isZero()) { |
0 |
| 6085 |
const SCEV *X = getNoopOrZeroExtend(getSCEV(LHS), Ty); |
0 |
6085 |
const SCEV *X = getNoopOrZeroExtend(getSCEV(LHS), Ty); |
0 |
| 6086 |
const SCEV *TrueValExpr = getSCEV(TrueVal); // C+y |
0 |
6086 |
const SCEV *TrueValExpr = getSCEV(TrueVal); // C+y |
0 |
| 6087 |
const SCEV *FalseValExpr = getSCEV(FalseVal); // x+y |
0 |
6087 |
const SCEV *FalseValExpr = getSCEV(FalseVal); // x+y |
0 |
| 6088 |
const SCEV *Y = getMinusSCEV(FalseValExpr, X); // y = (x+y)-x |
0 |
6088 |
const SCEV *Y = getMinusSCEV(FalseValExpr, X); // y = (x+y)-x |
0 |
| 6089 |
const SCEV *C = getMinusSCEV(TrueValExpr, Y); // C = (C+y)-y |
0 |
6089 |
const SCEV *C = getMinusSCEV(TrueValExpr, Y); // C = (C+y)-y |
0 |
| 6090 |
if (isa(C) && cast(C)->getAPInt().ule(1)) |
0 |
6090 |
if (isa(C) && cast(C)->getAPInt().ule(1)) |
0 |
| 6091 |
return getAddExpr(getUMaxExpr(X, C), Y); |
0 |
6091 |
return getAddExpr(getUMaxExpr(X, C), Y); |
0 |
| 6092 |
} |
--- |
6092 |
} |
--- |
| 6093 |
// x == 0 ? 0 : umin (..., x, ...) -> umin_seq(x, umin (...)) |
--- |
6093 |
// x == 0 ? 0 : umin (..., x, ...) -> umin_seq(x, umin (...)) |
--- |
| 6094 |
// x == 0 ? 0 : umin_seq(..., x, ...) -> umin_seq(x, umin_seq(...)) |
--- |
6094 |
// x == 0 ? 0 : umin_seq(..., x, ...) -> umin_seq(x, umin_seq(...)) |
--- |
| 6095 |
// x == 0 ? 0 : umin (..., umin_seq(..., x, ...), ...) |
--- |
6095 |
// x == 0 ? 0 : umin (..., umin_seq(..., x, ...), ...) |
--- |
| 6096 |
// -> umin_seq(x, umin (..., umin_seq(...), ...)) |
--- |
6096 |
// -> umin_seq(x, umin (..., umin_seq(...), ...)) |
--- |
| 6097 |
if (isa(RHS) && cast(RHS)->isZero() && |
0 |
6097 |
if (isa(RHS) && cast(RHS)->isZero() && |
0 |
| 6098 |
isa(TrueVal) && cast(TrueVal)->isZero()) { |
0 |
6098 |
isa(TrueVal) && cast(TrueVal)->isZero()) { |
0 |
| 6099 |
const SCEV *X = getSCEV(LHS); |
0 |
6099 |
const SCEV *X = getSCEV(LHS); |
0 |
| 6100 |
while (auto *ZExt = dyn_cast(X)) |
0 |
6100 |
while (auto *ZExt = dyn_cast(X)) |
0 |
| 6101 |
X = ZExt->getOperand(); |
0 |
6101 |
X = ZExt->getOperand(); |
0 |
| 6102 |
if (getTypeSizeInBits(X->getType()) <= getTypeSizeInBits(Ty)) { |
0 |
6102 |
if (getTypeSizeInBits(X->getType()) <= getTypeSizeInBits(Ty)) { |
0 |
| 6103 |
const SCEV *FalseValExpr = getSCEV(FalseVal); |
0 |
6103 |
const SCEV *FalseValExpr = getSCEV(FalseVal); |
0 |
| 6104 |
if (SCEVMinMaxExprContains(FalseValExpr, X, scSequentialUMinExpr)) |
0 |
6104 |
if (SCEVMinMaxExprContains(FalseValExpr, X, scSequentialUMinExpr)) |
0 |
| 6105 |
return getUMinExpr(getNoopOrZeroExtend(X, Ty), FalseValExpr, |
0 |
6105 |
return getUMinExpr(getNoopOrZeroExtend(X, Ty), FalseValExpr, |
0 |
| 6106 |
/*Sequential=*/true); |
0 |
6106 |
/*Sequential=*/true); |
0 |
| 6107 |
} |
--- |
6107 |
} |
--- |
| 6108 |
} |
--- |
6108 |
} |
--- |
| 6109 |
break; |
0 |
6109 |
break; |
0 |
| 6110 |
default: |
0 |
6110 |
default: |
0 |
| 6111 |
break; |
0 |
6111 |
break; |
0 |
| 6112 |
} |
--- |
6112 |
} |
--- |
| 6113 |
|
--- |
6113 |
|
--- |
| 6114 |
return std::nullopt; |
0 |
6114 |
return std::nullopt; |
0 |
| 6115 |
} |
--- |
6115 |
} |
--- |
| 6116 |
|
--- |
6116 |
|
--- |
| 6117 |
static std::optional |
--- |
6117 |
static std::optional |
--- |
| 6118 |
createNodeForSelectViaUMinSeq(ScalarEvolution *SE, const SCEV *CondExpr, |
0 |
6118 |
createNodeForSelectViaUMinSeq(ScalarEvolution *SE, const SCEV *CondExpr, |
0 |
| 6119 |
const SCEV *TrueExpr, const SCEV *FalseExpr) { |
--- |
6119 |
const SCEV *TrueExpr, const SCEV *FalseExpr) { |
--- |
| 6120 |
assert(CondExpr->getType()->isIntegerTy(1) && |
0 |
6120 |
assert(CondExpr->getType()->isIntegerTy(1) && |
0 |
| 6121 |
TrueExpr->getType() == FalseExpr->getType() && |
--- |
6121 |
TrueExpr->getType() == FalseExpr->getType() && |
--- |
| 6122 |
TrueExpr->getType()->isIntegerTy(1) && |
--- |
6122 |
TrueExpr->getType()->isIntegerTy(1) && |
--- |
| 6123 |
"Unexpected operands of a select."); |
--- |
6123 |
"Unexpected operands of a select."); |
--- |
| 6124 |
|
--- |
6124 |
|
--- |
| 6125 |
// i1 cond ? i1 x : i1 C --> C + (i1 cond ? (i1 x - i1 C) : i1 0) |
--- |
6125 |
// i1 cond ? i1 x : i1 C --> C + (i1 cond ? (i1 x - i1 C) : i1 0) |
--- |
| 6126 |
// --> C + (umin_seq cond, x - C) |
--- |
6126 |
// --> C + (umin_seq cond, x - C) |
--- |
| 6127 |
// |
--- |
6127 |
// |
--- |
| 6128 |
// i1 cond ? i1 C : i1 x --> C + (i1 cond ? i1 0 : (i1 x - i1 C)) |
--- |
6128 |
// i1 cond ? i1 C : i1 x --> C + (i1 cond ? i1 0 : (i1 x - i1 C)) |
--- |
| 6129 |
// --> C + (i1 ~cond ? (i1 x - i1 C) : i1 0) |
--- |
6129 |
// --> C + (i1 ~cond ? (i1 x - i1 C) : i1 0) |
--- |
| 6130 |
// --> C + (umin_seq ~cond, x - C) |
--- |
6130 |
// --> C + (umin_seq ~cond, x - C) |
--- |
| 6131 |
|
--- |
6131 |
|
--- |
| 6132 |
// FIXME: while we can't legally model the case where both of the hands |
--- |
6132 |
// FIXME: while we can't legally model the case where both of the hands |
--- |
| 6133 |
// are fully variable, we only require that the *difference* is constant. |
--- |
6133 |
// are fully variable, we only require that the *difference* is constant. |
--- |
| 6134 |
if (!isa(TrueExpr) && !isa(FalseExpr)) |
0 |
6134 |
if (!isa(TrueExpr) && !isa(FalseExpr)) |
0 |
| 6135 |
return std::nullopt; |
0 |
6135 |
return std::nullopt; |
0 |
| 6136 |
|
--- |
6136 |
|
--- |
| 6137 |
const SCEV *X, *C; |
--- |
6137 |
const SCEV *X, *C; |
--- |
| 6138 |
if (isa(TrueExpr)) { |
0 |
6138 |
if (isa(TrueExpr)) { |
0 |
| 6139 |
CondExpr = SE->getNotSCEV(CondExpr); |
0 |
6139 |
CondExpr = SE->getNotSCEV(CondExpr); |
0 |
| 6140 |
X = FalseExpr; |
0 |
6140 |
X = FalseExpr; |
0 |
| 6141 |
C = TrueExpr; |
0 |
6141 |
C = TrueExpr; |
0 |
| 6142 |
} else { |
--- |
6142 |
} else { |
--- |
| 6143 |
X = TrueExpr; |
0 |
6143 |
X = TrueExpr; |
0 |
| 6144 |
C = FalseExpr; |
0 |
6144 |
C = FalseExpr; |
0 |
| 6145 |
} |
--- |
6145 |
} |
--- |
| 6146 |
return SE->getAddExpr(C, SE->getUMinExpr(CondExpr, SE->getMinusSCEV(X, C), |
0 |
6146 |
return SE->getAddExpr(C, SE->getUMinExpr(CondExpr, SE->getMinusSCEV(X, C), |
0 |
| 6147 |
/*Sequential=*/true)); |
0 |
6147 |
/*Sequential=*/true)); |
0 |
| 6148 |
} |
--- |
6148 |
} |
--- |
| 6149 |
|
--- |
6149 |
|
--- |
| 6150 |
static std::optional |
--- |
6150 |
static std::optional |
--- |
| 6151 |
createNodeForSelectViaUMinSeq(ScalarEvolution *SE, Value *Cond, Value *TrueVal, |
0 |
6151 |
createNodeForSelectViaUMinSeq(ScalarEvolution *SE, Value *Cond, Value *TrueVal, |
0 |
| 6152 |
Value *FalseVal) { |
--- |
6152 |
Value *FalseVal) { |
--- |
| 6153 |
if (!isa(TrueVal) && !isa(FalseVal)) |
0 |
6153 |
if (!isa(TrueVal) && !isa(FalseVal)) |
0 |
| 6154 |
return std::nullopt; |
0 |
6154 |
return std::nullopt; |
0 |
| 6155 |
|
--- |
6155 |
|
--- |
| 6156 |
const auto *SECond = SE->getSCEV(Cond); |
0 |
6156 |
const auto *SECond = SE->getSCEV(Cond); |
0 |
| 6157 |
const auto *SETrue = SE->getSCEV(TrueVal); |
0 |
6157 |
const auto *SETrue = SE->getSCEV(TrueVal); |
0 |
| 6158 |
const auto *SEFalse = SE->getSCEV(FalseVal); |
0 |
6158 |
const auto *SEFalse = SE->getSCEV(FalseVal); |
0 |
| 6159 |
return createNodeForSelectViaUMinSeq(SE, SECond, SETrue, SEFalse); |
0 |
6159 |
return createNodeForSelectViaUMinSeq(SE, SECond, SETrue, SEFalse); |
0 |
| 6160 |
} |
--- |
6160 |
} |
--- |
| 6161 |
|
--- |
6161 |
|
--- |
| 6162 |
const SCEV *ScalarEvolution::createNodeForSelectOrPHIViaUMinSeq( |
0 |
6162 |
const SCEV *ScalarEvolution::createNodeForSelectOrPHIViaUMinSeq( |
0 |
| 6163 |
Value *V, Value *Cond, Value *TrueVal, Value *FalseVal) { |
--- |
6163 |
Value *V, Value *Cond, Value *TrueVal, Value *FalseVal) { |
--- |
| 6164 |
assert(Cond->getType()->isIntegerTy(1) && "Select condition is not an i1?"); |
0 |
6164 |
assert(Cond->getType()->isIntegerTy(1) && "Select condition is not an i1?"); |
0 |
| 6165 |
assert(TrueVal->getType() == FalseVal->getType() && |
0 |
6165 |
assert(TrueVal->getType() == FalseVal->getType() && |
0 |
| 6166 |
V->getType() == TrueVal->getType() && |
--- |
6166 |
V->getType() == TrueVal->getType() && |
--- |
| 6167 |
"Types of select hands and of the result must match."); |
--- |
6167 |
"Types of select hands and of the result must match."); |
--- |
| 6168 |
|
--- |
6168 |
|
--- |
| 6169 |
// For now, only deal with i1-typed `select`s. |
--- |
6169 |
// For now, only deal with i1-typed `select`s. |
--- |
| 6170 |
if (!V->getType()->isIntegerTy(1)) |
0 |
6170 |
if (!V->getType()->isIntegerTy(1)) |
0 |
| 6171 |
return getUnknown(V); |
0 |
6171 |
return getUnknown(V); |
0 |
| 6172 |
|
--- |
6172 |
|
--- |
| 6173 |
if (std::optional S = |
0 |
6173 |
if (std::optional S = |
0 |
| 6174 |
createNodeForSelectViaUMinSeq(this, Cond, TrueVal, FalseVal)) |
0 |
6174 |
createNodeForSelectViaUMinSeq(this, Cond, TrueVal, FalseVal)) |
0 |
| 6175 |
return *S; |
0 |
6175 |
return *S; |
0 |
| 6176 |
|
--- |
6176 |
|
--- |
| 6177 |
return getUnknown(V); |
0 |
6177 |
return getUnknown(V); |
0 |
| 6178 |
} |
--- |
6178 |
} |
--- |
| 6179 |
|
--- |
6179 |
|
--- |
| 6180 |
const SCEV *ScalarEvolution::createNodeForSelectOrPHI(Value *V, Value *Cond, |
0 |
6180 |
const SCEV *ScalarEvolution::createNodeForSelectOrPHI(Value *V, Value *Cond, |
0 |
| 6181 |
Value *TrueVal, |
--- |
6181 |
Value *TrueVal, |
--- |
| 6182 |
Value *FalseVal) { |
--- |
6182 |
Value *FalseVal) { |
--- |
| 6183 |
// Handle "constant" branch or select. This can occur for instance when a |
--- |
6183 |
// Handle "constant" branch or select. This can occur for instance when a |
--- |
| 6184 |
// loop pass transforms an inner loop and moves on to process the outer loop. |
--- |
6184 |
// loop pass transforms an inner loop and moves on to process the outer loop. |
--- |
| 6185 |
if (auto *CI = dyn_cast(Cond)) |
0 |
6185 |
if (auto *CI = dyn_cast(Cond)) |
0 |
| 6186 |
return getSCEV(CI->isOne() ? TrueVal : FalseVal); |
0 |
6186 |
return getSCEV(CI->isOne() ? TrueVal : FalseVal); |
0 |
| 6187 |
|
--- |
6187 |
|
--- |
| 6188 |
if (auto *I = dyn_cast(V)) { |
0 |
6188 |
if (auto *I = dyn_cast(V)) { |
0 |
| 6189 |
if (auto *ICI = dyn_cast(Cond)) { |
0 |
6189 |
if (auto *ICI = dyn_cast(Cond)) { |
0 |
| 6190 |
if (std::optional S = |
0 |
6190 |
if (std::optional S = |
0 |
| 6191 |
createNodeForSelectOrPHIInstWithICmpInstCond(I->getType(), ICI, |
0 |
6191 |
createNodeForSelectOrPHIInstWithICmpInstCond(I->getType(), ICI, |
0 |
| 6192 |
TrueVal, FalseVal)) |
0 |
6192 |
TrueVal, FalseVal)) |
0 |
| 6193 |
return *S; |
0 |
6193 |
return *S; |
0 |
| 6194 |
} |
--- |
6194 |
} |
--- |
| 6195 |
} |
--- |
6195 |
} |
--- |
| 6196 |
|
--- |
6196 |
|
--- |
| 6197 |
return createNodeForSelectOrPHIViaUMinSeq(V, Cond, TrueVal, FalseVal); |
0 |
6197 |
return createNodeForSelectOrPHIViaUMinSeq(V, Cond, TrueVal, FalseVal); |
0 |
| 6198 |
} |
--- |
6198 |
} |
--- |
| 6199 |
|
--- |
6199 |
|
--- |
| 6200 |
/// Expand GEP instructions into add and multiply operations. This allows them |
--- |
6200 |
/// Expand GEP instructions into add and multiply operations. This allows them |
--- |
| 6201 |
/// to be analyzed by regular SCEV code. |
--- |
6201 |
/// to be analyzed by regular SCEV code. |
--- |
| 6202 |
const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) { |
0 |
6202 |
const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) { |
0 |
| 6203 |
assert(GEP->getSourceElementType()->isSized() && |
0 |
6203 |
assert(GEP->getSourceElementType()->isSized() && |
0 |
| 6204 |
"GEP source element type must be sized"); |
--- |
6204 |
"GEP source element type must be sized"); |
--- |
| 6205 |
|
--- |
6205 |
|
--- |
| 6206 |
SmallVector IndexExprs; |
0 |
6206 |
SmallVector IndexExprs; |
0 |
| 6207 |
for (Value *Index : GEP->indices()) |
0 |
6207 |
for (Value *Index : GEP->indices()) |
0 |
| 6208 |
IndexExprs.push_back(getSCEV(Index)); |
0 |
6208 |
IndexExprs.push_back(getSCEV(Index)); |
0 |
| 6209 |
return getGEPExpr(GEP, IndexExprs); |
0 |
6209 |
return getGEPExpr(GEP, IndexExprs); |
0 |
| 6210 |
} |
0 |
6210 |
} |
0 |
| 6211 |
|
--- |
6211 |
|
--- |
| 6212 |
APInt ScalarEvolution::getConstantMultipleImpl(const SCEV *S) { |
0 |
6212 |
APInt ScalarEvolution::getConstantMultipleImpl(const SCEV *S) { |
0 |
| 6213 |
uint64_t BitWidth = getTypeSizeInBits(S->getType()); |
0 |
6213 |
uint64_t BitWidth = getTypeSizeInBits(S->getType()); |
0 |
| 6214 |
auto GetShiftedByZeros = [BitWidth](uint32_t TrailingZeros) { |
0 |
6214 |
auto GetShiftedByZeros = [BitWidth](uint32_t TrailingZeros) { |
0 |
| 6215 |
return TrailingZeros >= BitWidth |
0 |
6215 |
return TrailingZeros >= BitWidth |
0 |
| 6216 |
? APInt::getZero(BitWidth) |
--- |
6216 |
? APInt::getZero(BitWidth) |
--- |
| 6217 |
: APInt::getOneBitSet(BitWidth, TrailingZeros); |
0 |
6217 |
: APInt::getOneBitSet(BitWidth, TrailingZeros); |
0 |
| 6218 |
}; |
0 |
6218 |
}; |
0 |
| 6219 |
auto GetGCDMultiple = [this](const SCEVNAryExpr *N) { |
0 |
6219 |
auto GetGCDMultiple = [this](const SCEVNAryExpr *N) { |
0 |
| 6220 |
// The result is GCD of all operands results. |
--- |
6220 |
// The result is GCD of all operands results. |
--- |
| 6221 |
APInt Res = getConstantMultiple(N->getOperand(0)); |
0 |
6221 |
APInt Res = getConstantMultiple(N->getOperand(0)); |
0 |
| 6222 |
for (unsigned I = 1, E = N->getNumOperands(); I < E && Res != 1; ++I) |
0 |
6222 |
for (unsigned I = 1, E = N->getNumOperands(); I < E && Res != 1; ++I) |
0 |
| 6223 |
Res = APIntOps::GreatestCommonDivisor( |
0 |
6223 |
Res = APIntOps::GreatestCommonDivisor( |
0 |
| 6224 |
Res, getConstantMultiple(N->getOperand(I))); |
0 |
6224 |
Res, getConstantMultiple(N->getOperand(I))); |
0 |
| 6225 |
return Res; |
0 |
6225 |
return Res; |
0 |
| 6226 |
}; |
0 |
6226 |
}; |
0 |
| 6227 |
|
--- |
6227 |
|
--- |
| 6228 |
switch (S->getSCEVType()) { |
0 |
6228 |
switch (S->getSCEVType()) { |
0 |
| 6229 |
case scConstant: |
0 |
6229 |
case scConstant: |
0 |
| 6230 |
return cast(S)->getAPInt(); |
0 |
6230 |
return cast(S)->getAPInt(); |
0 |
| 6231 |
case scPtrToInt: |
0 |
6231 |
case scPtrToInt: |
0 |
| 6232 |
return getConstantMultiple(cast(S)->getOperand()); |
0 |
6232 |
return getConstantMultiple(cast(S)->getOperand()); |
0 |
| 6233 |
case scUDivExpr: |
0 |
6233 |
case scUDivExpr: |
0 |
| 6234 |
case scVScale: |
--- |
6234 |
case scVScale: |
--- |
| 6235 |
return APInt(BitWidth, 1); |
0 |
6235 |
return APInt(BitWidth, 1); |
0 |
| 6236 |
case scTruncate: { |
0 |
6236 |
case scTruncate: { |
0 |
| 6237 |
// Only multiples that are a power of 2 will hold after truncation. |
--- |
6237 |
// Only multiples that are a power of 2 will hold after truncation. |
--- |
| 6238 |
const SCEVTruncateExpr *T = cast(S); |
0 |
6238 |
const SCEVTruncateExpr *T = cast(S); |
0 |
| 6239 |
uint32_t TZ = getMinTrailingZeros(T->getOperand()); |
0 |
6239 |
uint32_t TZ = getMinTrailingZeros(T->getOperand()); |
0 |
| 6240 |
return GetShiftedByZeros(TZ); |
0 |
6240 |
return GetShiftedByZeros(TZ); |
0 |
| 6241 |
} |
--- |
6241 |
} |
--- |
| 6242 |
case scZeroExtend: { |
0 |
6242 |
case scZeroExtend: { |
0 |
| 6243 |
const SCEVZeroExtendExpr *Z = cast(S); |
0 |
6243 |
const SCEVZeroExtendExpr *Z = cast(S); |
0 |
| 6244 |
return getConstantMultiple(Z->getOperand()).zext(BitWidth); |
0 |
6244 |
return getConstantMultiple(Z->getOperand()).zext(BitWidth); |
0 |
| 6245 |
} |
--- |
6245 |
} |
--- |
| 6246 |
case scSignExtend: { |
0 |
6246 |
case scSignExtend: { |
0 |
| 6247 |
const SCEVSignExtendExpr *E = cast(S); |
0 |
6247 |
const SCEVSignExtendExpr *E = cast(S); |
0 |
| 6248 |
return getConstantMultiple(E->getOperand()).sext(BitWidth); |
0 |
6248 |
return getConstantMultiple(E->getOperand()).sext(BitWidth); |
0 |
| 6249 |
} |
--- |
6249 |
} |
--- |
| 6250 |
case scMulExpr: { |
0 |
6250 |
case scMulExpr: { |
0 |
| 6251 |
const SCEVMulExpr *M = cast(S); |
0 |
6251 |
const SCEVMulExpr *M = cast(S); |
0 |
| 6252 |
if (M->hasNoUnsignedWrap()) { |
0 |
6252 |
if (M->hasNoUnsignedWrap()) { |
0 |
| 6253 |
// The result is the product of all operand results. |
--- |
6253 |
// The result is the product of all operand results. |
--- |
| 6254 |
APInt Res = getConstantMultiple(M->getOperand(0)); |
0 |
6254 |
APInt Res = getConstantMultiple(M->getOperand(0)); |
0 |
| 6255 |
for (const SCEV *Operand : M->operands().drop_front()) |
0 |
6255 |
for (const SCEV *Operand : M->operands().drop_front()) |
0 |
| 6256 |
Res = Res * getConstantMultiple(Operand); |
0 |
6256 |
Res = Res * getConstantMultiple(Operand); |
0 |
| 6257 |
return Res; |
0 |
6257 |
return Res; |
0 |
| 6258 |
} |
0 |
6258 |
} |
0 |
| 6259 |
|
--- |
6259 |
|
--- |
| 6260 |
// If there are no wrap guarentees, find the trailing zeros, which is the |
--- |
6260 |
// If there are no wrap guarentees, find the trailing zeros, which is the |
--- |
| 6261 |
// sum of trailing zeros for all its operands. |
--- |
6261 |
// sum of trailing zeros for all its operands. |
--- |
| 6262 |
uint32_t TZ = 0; |
0 |
6262 |
uint32_t TZ = 0; |
0 |
| 6263 |
for (const SCEV *Operand : M->operands()) |
0 |
6263 |
for (const SCEV *Operand : M->operands()) |
0 |
| 6264 |
TZ += getMinTrailingZeros(Operand); |
0 |
6264 |
TZ += getMinTrailingZeros(Operand); |
0 |
| 6265 |
return GetShiftedByZeros(TZ); |
0 |
6265 |
return GetShiftedByZeros(TZ); |
0 |
| 6266 |
} |
--- |
6266 |
} |
--- |
| 6267 |
case scAddExpr: |
0 |
6267 |
case scAddExpr: |
0 |
| 6268 |
case scAddRecExpr: { |
--- |
6268 |
case scAddRecExpr: { |
--- |
| 6269 |
const SCEVNAryExpr *N = cast(S); |
0 |
6269 |
const SCEVNAryExpr *N = cast(S); |
0 |
| 6270 |
if (N->hasNoUnsignedWrap()) |
0 |
6270 |
if (N->hasNoUnsignedWrap()) |
0 |
| 6271 |
return GetGCDMultiple(N); |
0 |
6271 |
return GetGCDMultiple(N); |
0 |
| 6272 |
// Find the trailing bits, which is the minimum of its operands. |
--- |
6272 |
// Find the trailing bits, which is the minimum of its operands. |
--- |
| 6273 |
uint32_t TZ = getMinTrailingZeros(N->getOperand(0)); |
0 |
6273 |
uint32_t TZ = getMinTrailingZeros(N->getOperand(0)); |
0 |
| 6274 |
for (const SCEV *Operand : N->operands().drop_front()) |
0 |
6274 |
for (const SCEV *Operand : N->operands().drop_front()) |
0 |
| 6275 |
TZ = std::min(TZ, getMinTrailingZeros(Operand)); |
0 |
6275 |
TZ = std::min(TZ, getMinTrailingZeros(Operand)); |
0 |
| 6276 |
return GetShiftedByZeros(TZ); |
0 |
6276 |
return GetShiftedByZeros(TZ); |
0 |
| 6277 |
} |
--- |
6277 |
} |
--- |
| 6278 |
case scUMaxExpr: |
0 |
6278 |
case scUMaxExpr: |
0 |
| 6279 |
case scSMaxExpr: |
--- |
6279 |
case scSMaxExpr: |
--- |
| 6280 |
case scUMinExpr: |
--- |
6280 |
case scUMinExpr: |
--- |
| 6281 |
case scSMinExpr: |
--- |
6281 |
case scSMinExpr: |
--- |
| 6282 |
case scSequentialUMinExpr: |
--- |
6282 |
case scSequentialUMinExpr: |
--- |
| 6283 |
return GetGCDMultiple(cast(S)); |
0 |
6283 |
return GetGCDMultiple(cast(S)); |
0 |
| 6284 |
case scUnknown: { |
0 |
6284 |
case scUnknown: { |
0 |
| 6285 |
// ask ValueTracking for known bits |
--- |
6285 |
// ask ValueTracking for known bits |
--- |
| 6286 |
const SCEVUnknown *U = cast(S); |
0 |
6286 |
const SCEVUnknown *U = cast(S); |
0 |
| 6287 |
unsigned Known = |
--- |
6287 |
unsigned Known = |
--- |
| 6288 |
computeKnownBits(U->getValue(), getDataLayout(), 0, &AC, nullptr, &DT) |
0 |
6288 |
computeKnownBits(U->getValue(), getDataLayout(), 0, &AC, nullptr, &DT) |
0 |
| 6289 |
.countMinTrailingZeros(); |
0 |
6289 |
.countMinTrailingZeros(); |
0 |
| 6290 |
return GetShiftedByZeros(Known); |
0 |
6290 |
return GetShiftedByZeros(Known); |
0 |
| 6291 |
} |
--- |
6291 |
} |
--- |
| 6292 |
case scCouldNotCompute: |
0 |
6292 |
case scCouldNotCompute: |
0 |
| 6293 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
6293 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
| 6294 |
} |
--- |
6294 |
} |
--- |
| 6295 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
6295 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
| 6296 |
} |
--- |
6296 |
} |
--- |
| 6297 |
|
--- |
6297 |
|
--- |
| 6298 |
APInt ScalarEvolution::getConstantMultiple(const SCEV *S) { |
0 |
6298 |
APInt ScalarEvolution::getConstantMultiple(const SCEV *S) { |
0 |
| 6299 |
auto I = ConstantMultipleCache.find(S); |
0 |
6299 |
auto I = ConstantMultipleCache.find(S); |
0 |
| 6300 |
if (I != ConstantMultipleCache.end()) |
0 |
6300 |
if (I != ConstantMultipleCache.end()) |
0 |
| 6301 |
return I->second; |
0 |
6301 |
return I->second; |
0 |
| 6302 |
|
--- |
6302 |
|
--- |
| 6303 |
APInt Result = getConstantMultipleImpl(S); |
0 |
6303 |
APInt Result = getConstantMultipleImpl(S); |
0 |
| 6304 |
auto InsertPair = ConstantMultipleCache.insert({S, Result}); |
0 |
6304 |
auto InsertPair = ConstantMultipleCache.insert({S, Result}); |
0 |
| 6305 |
assert(InsertPair.second && "Should insert a new key"); |
0 |
6305 |
assert(InsertPair.second && "Should insert a new key"); |
0 |
| 6306 |
return InsertPair.first->second; |
0 |
6306 |
return InsertPair.first->second; |
0 |
| 6307 |
} |
0 |
6307 |
} |
0 |
| 6308 |
|
--- |
6308 |
|
--- |
| 6309 |
APInt ScalarEvolution::getNonZeroConstantMultiple(const SCEV *S) { |
0 |
6309 |
APInt ScalarEvolution::getNonZeroConstantMultiple(const SCEV *S) { |
0 |
| 6310 |
APInt Multiple = getConstantMultiple(S); |
0 |
6310 |
APInt Multiple = getConstantMultiple(S); |
0 |
| 6311 |
return Multiple == 0 ? APInt(Multiple.getBitWidth(), 1) : Multiple; |
0 |
6311 |
return Multiple == 0 ? APInt(Multiple.getBitWidth(), 1) : Multiple; |
0 |
| 6312 |
} |
0 |
6312 |
} |
0 |
| 6313 |
|
--- |
6313 |
|
--- |
| 6314 |
uint32_t ScalarEvolution::getMinTrailingZeros(const SCEV *S) { |
0 |
6314 |
uint32_t ScalarEvolution::getMinTrailingZeros(const SCEV *S) { |
0 |
| 6315 |
return std::min(getConstantMultiple(S).countTrailingZeros(), |
0 |
6315 |
return std::min(getConstantMultiple(S).countTrailingZeros(), |
0 |
| 6316 |
(unsigned)getTypeSizeInBits(S->getType())); |
0 |
6316 |
(unsigned)getTypeSizeInBits(S->getType())); |
0 |
| 6317 |
} |
--- |
6317 |
} |
--- |
| 6318 |
|
--- |
6318 |
|
--- |
| 6319 |
/// Helper method to assign a range to V from metadata present in the IR. |
--- |
6319 |
/// Helper method to assign a range to V from metadata present in the IR. |
--- |
| 6320 |
static std::optional GetRangeFromMetadata(Value *V) { |
0 |
6320 |
static std::optional GetRangeFromMetadata(Value *V) { |
0 |
| 6321 |
if (Instruction *I = dyn_cast(V)) |
0 |
6321 |
if (Instruction *I = dyn_cast(V)) |
0 |
| 6322 |
if (MDNode *MD = I->getMetadata(LLVMContext::MD_range)) |
0 |
6322 |
if (MDNode *MD = I->getMetadata(LLVMContext::MD_range)) |
0 |
| 6323 |
return getConstantRangeFromMetadata(*MD); |
0 |
6323 |
return getConstantRangeFromMetadata(*MD); |
0 |
| 6324 |
|
--- |
6324 |
|
--- |
| 6325 |
return std::nullopt; |
0 |
6325 |
return std::nullopt; |
0 |
| 6326 |
} |
--- |
6326 |
} |
--- |
| 6327 |
|
--- |
6327 |
|
--- |
| 6328 |
void ScalarEvolution::setNoWrapFlags(SCEVAddRecExpr *AddRec, |
0 |
6328 |
void ScalarEvolution::setNoWrapFlags(SCEVAddRecExpr *AddRec, |
0 |
| 6329 |
SCEV::NoWrapFlags Flags) { |
--- |
6329 |
SCEV::NoWrapFlags Flags) { |
--- |
| 6330 |
if (AddRec->getNoWrapFlags(Flags) != Flags) { |
0 |
6330 |
if (AddRec->getNoWrapFlags(Flags) != Flags) { |
0 |
| 6331 |
AddRec->setNoWrapFlags(Flags); |
0 |
6331 |
AddRec->setNoWrapFlags(Flags); |
0 |
| 6332 |
UnsignedRanges.erase(AddRec); |
0 |
6332 |
UnsignedRanges.erase(AddRec); |
0 |
| 6333 |
SignedRanges.erase(AddRec); |
0 |
6333 |
SignedRanges.erase(AddRec); |
0 |
| 6334 |
ConstantMultipleCache.erase(AddRec); |
0 |
6334 |
ConstantMultipleCache.erase(AddRec); |
0 |
| 6335 |
} |
--- |
6335 |
} |
--- |
| 6336 |
} |
0 |
6336 |
} |
0 |
| 6337 |
|
--- |
6337 |
|
--- |
| 6338 |
ConstantRange ScalarEvolution:: |
0 |
6338 |
ConstantRange ScalarEvolution:: |
0 |
| 6339 |
getRangeForUnknownRecurrence(const SCEVUnknown *U) { |
--- |
6339 |
getRangeForUnknownRecurrence(const SCEVUnknown *U) { |
--- |
| 6340 |
const DataLayout &DL = getDataLayout(); |
0 |
6340 |
const DataLayout &DL = getDataLayout(); |
0 |
| 6341 |
|
--- |
6341 |
|
--- |
| 6342 |
unsigned BitWidth = getTypeSizeInBits(U->getType()); |
0 |
6342 |
unsigned BitWidth = getTypeSizeInBits(U->getType()); |
0 |
| 6343 |
const ConstantRange FullSet(BitWidth, /*isFullSet=*/true); |
0 |
6343 |
const ConstantRange FullSet(BitWidth, /*isFullSet=*/true); |
0 |
| 6344 |
|
--- |
6344 |
|
--- |
| 6345 |
// Match a simple recurrence of the form: , and then |
--- |
6345 |
// Match a simple recurrence of the form: , and then |
--- |
| 6346 |
// use information about the trip count to improve our available range. Note |
--- |
6346 |
// use information about the trip count to improve our available range. Note |
--- |
| 6347 |
// that the trip count independent cases are already handled by known bits. |
--- |
6347 |
// that the trip count independent cases are already handled by known bits. |
--- |
| 6348 |
// WARNING: The definition of recurrence used here is subtly different than |
--- |
6348 |
// WARNING: The definition of recurrence used here is subtly different than |
--- |
| 6349 |
// the one used by AddRec (and thus most of this file). Step is allowed to |
--- |
6349 |
// the one used by AddRec (and thus most of this file). Step is allowed to |
--- |
| 6350 |
// be arbitrarily loop varying here, where AddRec allows only loop invariant |
--- |
6350 |
// be arbitrarily loop varying here, where AddRec allows only loop invariant |
--- |
| 6351 |
// and other addrecs in the same loop (for non-affine addrecs). The code |
--- |
6351 |
// and other addrecs in the same loop (for non-affine addrecs). The code |
--- |
| 6352 |
// below intentionally handles the case where step is not loop invariant. |
--- |
6352 |
// below intentionally handles the case where step is not loop invariant. |
--- |
| 6353 |
auto *P = dyn_cast(U->getValue()); |
0 |
6353 |
auto *P = dyn_cast(U->getValue()); |
0 |
| 6354 |
if (!P) |
0 |
6354 |
if (!P) |
0 |
| 6355 |
return FullSet; |
0 |
6355 |
return FullSet; |
0 |
| 6356 |
|
--- |
6356 |
|
--- |
| 6357 |
// Make sure that no Phi input comes from an unreachable block. Otherwise, |
--- |
6357 |
// Make sure that no Phi input comes from an unreachable block. Otherwise, |
--- |
| 6358 |
// even the values that are not available in these blocks may come from them, |
--- |
6358 |
// even the values that are not available in these blocks may come from them, |
--- |
| 6359 |
// and this leads to false-positive recurrence test. |
--- |
6359 |
// and this leads to false-positive recurrence test. |
--- |
| 6360 |
for (auto *Pred : predecessors(P->getParent())) |
0 |
6360 |
for (auto *Pred : predecessors(P->getParent())) |
0 |
| 6361 |
if (!DT.isReachableFromEntry(Pred)) |
0 |
6361 |
if (!DT.isReachableFromEntry(Pred)) |
0 |
| 6362 |
return FullSet; |
0 |
6362 |
return FullSet; |
0 |
| 6363 |
|
--- |
6363 |
|
--- |
| 6364 |
BinaryOperator *BO; |
--- |
6364 |
BinaryOperator *BO; |
--- |
| 6365 |
Value *Start, *Step; |
--- |
6365 |
Value *Start, *Step; |
--- |
| 6366 |
if (!matchSimpleRecurrence(P, BO, Start, Step)) |
0 |
6366 |
if (!matchSimpleRecurrence(P, BO, Start, Step)) |
0 |
| 6367 |
return FullSet; |
0 |
6367 |
return FullSet; |
0 |
| 6368 |
|
--- |
6368 |
|
--- |
| 6369 |
// If we found a recurrence in reachable code, we must be in a loop. Note |
--- |
6369 |
// If we found a recurrence in reachable code, we must be in a loop. Note |
--- |
| 6370 |
// that BO might be in some subloop of L, and that's completely okay. |
--- |
6370 |
// that BO might be in some subloop of L, and that's completely okay. |
--- |
| 6371 |
auto *L = LI.getLoopFor(P->getParent()); |
0 |
6371 |
auto *L = LI.getLoopFor(P->getParent()); |
0 |
| 6372 |
assert(L && L->getHeader() == P->getParent()); |
0 |
6372 |
assert(L && L->getHeader() == P->getParent()); |
0 |
| 6373 |
if (!L->contains(BO->getParent())) |
0 |
6373 |
if (!L->contains(BO->getParent())) |
0 |
| 6374 |
// NOTE: This bailout should be an assert instead. However, asserting |
--- |
6374 |
// NOTE: This bailout should be an assert instead. However, asserting |
--- |
| 6375 |
// the condition here exposes a case where LoopFusion is querying SCEV |
--- |
6375 |
// the condition here exposes a case where LoopFusion is querying SCEV |
--- |
| 6376 |
// with malformed loop information during the midst of the transform. |
--- |
6376 |
// with malformed loop information during the midst of the transform. |
--- |
| 6377 |
// There doesn't appear to be an obvious fix, so for the moment bailout |
--- |
6377 |
// There doesn't appear to be an obvious fix, so for the moment bailout |
--- |
| 6378 |
// until the caller issue can be fixed. PR49566 tracks the bug. |
--- |
6378 |
// until the caller issue can be fixed. PR49566 tracks the bug. |
--- |
| 6379 |
return FullSet; |
0 |
6379 |
return FullSet; |
0 |
| 6380 |
|
--- |
6380 |
|
--- |
| 6381 |
// TODO: Extend to other opcodes such as mul, and div |
--- |
6381 |
// TODO: Extend to other opcodes such as mul, and div |
--- |
| 6382 |
switch (BO->getOpcode()) { |
0 |
6382 |
switch (BO->getOpcode()) { |
0 |
| 6383 |
default: |
0 |
6383 |
default: |
0 |
| 6384 |
return FullSet; |
0 |
6384 |
return FullSet; |
0 |
| 6385 |
case Instruction::AShr: |
0 |
6385 |
case Instruction::AShr: |
0 |
| 6386 |
case Instruction::LShr: |
--- |
6386 |
case Instruction::LShr: |
--- |
| 6387 |
case Instruction::Shl: |
--- |
6387 |
case Instruction::Shl: |
--- |
| 6388 |
break; |
0 |
6388 |
break; |
0 |
| 6389 |
}; |
--- |
6389 |
}; |
--- |
| 6390 |
|
--- |
6390 |
|
--- |
| 6391 |
if (BO->getOperand(0) != P) |
0 |
6391 |
if (BO->getOperand(0) != P) |
0 |
| 6392 |
// TODO: Handle the power function forms some day. |
--- |
6392 |
// TODO: Handle the power function forms some day. |
--- |
| 6393 |
return FullSet; |
0 |
6393 |
return FullSet; |
0 |
| 6394 |
|
--- |
6394 |
|
--- |
| 6395 |
unsigned TC = getSmallConstantMaxTripCount(L); |
0 |
6395 |
unsigned TC = getSmallConstantMaxTripCount(L); |
0 |
| 6396 |
if (!TC || TC >= BitWidth) |
0 |
6396 |
if (!TC || TC >= BitWidth) |
0 |
| 6397 |
return FullSet; |
0 |
6397 |
return FullSet; |
0 |
| 6398 |
|
--- |
6398 |
|
--- |
| 6399 |
auto KnownStart = computeKnownBits(Start, DL, 0, &AC, nullptr, &DT); |
0 |
6399 |
auto KnownStart = computeKnownBits(Start, DL, 0, &AC, nullptr, &DT); |
0 |
| 6400 |
auto KnownStep = computeKnownBits(Step, DL, 0, &AC, nullptr, &DT); |
0 |
6400 |
auto KnownStep = computeKnownBits(Step, DL, 0, &AC, nullptr, &DT); |
0 |
| 6401 |
assert(KnownStart.getBitWidth() == BitWidth && |
0 |
6401 |
assert(KnownStart.getBitWidth() == BitWidth && |
0 |
| 6402 |
KnownStep.getBitWidth() == BitWidth); |
--- |
6402 |
KnownStep.getBitWidth() == BitWidth); |
--- |
| 6403 |
|
--- |
6403 |
|
--- |
| 6404 |
// Compute total shift amount, being careful of overflow and bitwidths. |
--- |
6404 |
// Compute total shift amount, being careful of overflow and bitwidths. |
--- |
| 6405 |
auto MaxShiftAmt = KnownStep.getMaxValue(); |
0 |
6405 |
auto MaxShiftAmt = KnownStep.getMaxValue(); |
0 |
| 6406 |
APInt TCAP(BitWidth, TC-1); |
0 |
6406 |
APInt TCAP(BitWidth, TC-1); |
0 |
| 6407 |
bool Overflow = false; |
0 |
6407 |
bool Overflow = false; |
0 |
| 6408 |
auto TotalShift = MaxShiftAmt.umul_ov(TCAP, Overflow); |
0 |
6408 |
auto TotalShift = MaxShiftAmt.umul_ov(TCAP, Overflow); |
0 |
| 6409 |
if (Overflow) |
0 |
6409 |
if (Overflow) |
0 |
| 6410 |
return FullSet; |
0 |
6410 |
return FullSet; |
0 |
| 6411 |
|
--- |
6411 |
|
--- |
| 6412 |
switch (BO->getOpcode()) { |
0 |
6412 |
switch (BO->getOpcode()) { |
0 |
| 6413 |
default: |
0 |
6413 |
default: |
0 |
| 6414 |
llvm_unreachable("filtered out above"); |
0 |
6414 |
llvm_unreachable("filtered out above"); |
0 |
| 6415 |
case Instruction::AShr: { |
0 |
6415 |
case Instruction::AShr: { |
0 |
| 6416 |
// For each ashr, three cases: |
--- |
6416 |
// For each ashr, three cases: |
--- |
| 6417 |
// shift = 0 => unchanged value |
--- |
6417 |
// shift = 0 => unchanged value |
--- |
| 6418 |
// saturation => 0 or -1 |
--- |
6418 |
// saturation => 0 or -1 |
--- |
| 6419 |
// other => a value closer to zero (of the same sign) |
--- |
6419 |
// other => a value closer to zero (of the same sign) |
--- |
| 6420 |
// Thus, the end value is closer to zero than the start. |
--- |
6420 |
// Thus, the end value is closer to zero than the start. |
--- |
| 6421 |
auto KnownEnd = KnownBits::ashr(KnownStart, |
--- |
6421 |
auto KnownEnd = KnownBits::ashr(KnownStart, |
--- |
| 6422 |
KnownBits::makeConstant(TotalShift)); |
0 |
6422 |
KnownBits::makeConstant(TotalShift)); |
0 |
| 6423 |
if (KnownStart.isNonNegative()) |
0 |
6423 |
if (KnownStart.isNonNegative()) |
0 |
| 6424 |
// Analogous to lshr (simply not yet canonicalized) |
--- |
6424 |
// Analogous to lshr (simply not yet canonicalized) |
--- |
| 6425 |
return ConstantRange::getNonEmpty(KnownEnd.getMinValue(), |
0 |
6425 |
return ConstantRange::getNonEmpty(KnownEnd.getMinValue(), |
0 |
| 6426 |
KnownStart.getMaxValue() + 1); |
0 |
6426 |
KnownStart.getMaxValue() + 1); |
0 |
| 6427 |
if (KnownStart.isNegative()) |
0 |
6427 |
if (KnownStart.isNegative()) |
0 |
| 6428 |
// End >=u Start && End <=s Start |
--- |
6428 |
// End >=u Start && End <=s Start |
--- |
| 6429 |
return ConstantRange::getNonEmpty(KnownStart.getMinValue(), |
0 |
6429 |
return ConstantRange::getNonEmpty(KnownStart.getMinValue(), |
0 |
| 6430 |
KnownEnd.getMaxValue() + 1); |
0 |
6430 |
KnownEnd.getMaxValue() + 1); |
0 |
| 6431 |
break; |
0 |
6431 |
break; |
0 |
| 6432 |
} |
0 |
6432 |
} |
0 |
| 6433 |
case Instruction::LShr: { |
0 |
6433 |
case Instruction::LShr: { |
0 |
| 6434 |
// For each lshr, three cases: |
--- |
6434 |
// For each lshr, three cases: |
--- |
| 6435 |
// shift = 0 => unchanged value |
--- |
6435 |
// shift = 0 => unchanged value |
--- |
| 6436 |
// saturation => 0 |
--- |
6436 |
// saturation => 0 |
--- |
| 6437 |
// other => a smaller positive number |
--- |
6437 |
// other => a smaller positive number |
--- |
| 6438 |
// Thus, the low end of the unsigned range is the last value produced. |
--- |
6438 |
// Thus, the low end of the unsigned range is the last value produced. |
--- |
| 6439 |
auto KnownEnd = KnownBits::lshr(KnownStart, |
--- |
6439 |
auto KnownEnd = KnownBits::lshr(KnownStart, |
--- |
| 6440 |
KnownBits::makeConstant(TotalShift)); |
0 |
6440 |
KnownBits::makeConstant(TotalShift)); |
0 |
| 6441 |
return ConstantRange::getNonEmpty(KnownEnd.getMinValue(), |
0 |
6441 |
return ConstantRange::getNonEmpty(KnownEnd.getMinValue(), |
0 |
| 6442 |
KnownStart.getMaxValue() + 1); |
0 |
6442 |
KnownStart.getMaxValue() + 1); |
0 |
| 6443 |
} |
0 |
6443 |
} |
0 |
| 6444 |
case Instruction::Shl: { |
0 |
6444 |
case Instruction::Shl: { |
0 |
| 6445 |
// Iff no bits are shifted out, value increases on every shift. |
--- |
6445 |
// Iff no bits are shifted out, value increases on every shift. |
--- |
| 6446 |
auto KnownEnd = KnownBits::shl(KnownStart, |
--- |
6446 |
auto KnownEnd = KnownBits::shl(KnownStart, |
--- |
| 6447 |
KnownBits::makeConstant(TotalShift)); |
0 |
6447 |
KnownBits::makeConstant(TotalShift)); |
0 |
| 6448 |
if (TotalShift.ult(KnownStart.countMinLeadingZeros())) |
0 |
6448 |
if (TotalShift.ult(KnownStart.countMinLeadingZeros())) |
0 |
| 6449 |
return ConstantRange(KnownStart.getMinValue(), |
0 |
6449 |
return ConstantRange(KnownStart.getMinValue(), |
0 |
| 6450 |
KnownEnd.getMaxValue() + 1); |
0 |
6450 |
KnownEnd.getMaxValue() + 1); |
0 |
| 6451 |
break; |
0 |
6451 |
break; |
0 |
| 6452 |
} |
0 |
6452 |
} |
0 |
| 6453 |
}; |
--- |
6453 |
}; |
--- |
| 6454 |
return FullSet; |
0 |
6454 |
return FullSet; |
0 |
| 6455 |
} |
0 |
6455 |
} |
0 |
| 6456 |
|
--- |
6456 |
|
--- |
| 6457 |
const ConstantRange & |
--- |
6457 |
const ConstantRange & |
--- |
| 6458 |
ScalarEvolution::getRangeRefIter(const SCEV *S, |
0 |
6458 |
ScalarEvolution::getRangeRefIter(const SCEV *S, |
0 |
| 6459 |
ScalarEvolution::RangeSignHint SignHint) { |
--- |
6459 |
ScalarEvolution::RangeSignHint SignHint) { |
--- |
| 6460 |
DenseMap &Cache = |
0 |
6460 |
DenseMap &Cache = |
0 |
| 6461 |
SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges |
--- |
6461 |
SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges |
--- |
| 6462 |
: SignedRanges; |
--- |
6462 |
: SignedRanges; |
--- |
| 6463 |
SmallVector WorkList; |
0 |
6463 |
SmallVector WorkList; |
0 |
| 6464 |
SmallPtrSet Seen; |
0 |
6464 |
SmallPtrSet Seen; |
0 |
| 6465 |
|
--- |
6465 |
|
--- |
| 6466 |
// Add Expr to the worklist, if Expr is either an N-ary expression or a |
--- |
6466 |
// Add Expr to the worklist, if Expr is either an N-ary expression or a |
--- |
| 6467 |
// SCEVUnknown PHI node. |
--- |
6467 |
// SCEVUnknown PHI node. |
--- |
| 6468 |
auto AddToWorklist = [&WorkList, &Seen, &Cache](const SCEV *Expr) { |
0 |
6468 |
auto AddToWorklist = [&WorkList, &Seen, &Cache](const SCEV *Expr) { |
0 |
| 6469 |
if (!Seen.insert(Expr).second) |
0 |
6469 |
if (!Seen.insert(Expr).second) |
0 |
| 6470 |
return; |
0 |
6470 |
return; |
0 |
| 6471 |
if (Cache.contains(Expr)) |
0 |
6471 |
if (Cache.contains(Expr)) |
0 |
| 6472 |
return; |
0 |
6472 |
return; |
0 |
| 6473 |
switch (Expr->getSCEVType()) { |
0 |
6473 |
switch (Expr->getSCEVType()) { |
0 |
| 6474 |
case scUnknown: |
0 |
6474 |
case scUnknown: |
0 |
| 6475 |
if (!isa(cast(Expr)->getValue())) |
0 |
6475 |
if (!isa(cast(Expr)->getValue())) |
0 |
| 6476 |
break; |
0 |
6476 |
break; |
0 |
| 6477 |
[[fallthrough]]; |
--- |
6477 |
[[fallthrough]]; |
--- |
| 6478 |
case scConstant: |
--- |
6478 |
case scConstant: |
--- |
| 6479 |
case scVScale: |
--- |
6479 |
case scVScale: |
--- |
| 6480 |
case scTruncate: |
--- |
6480 |
case scTruncate: |
--- |
| 6481 |
case scZeroExtend: |
--- |
6481 |
case scZeroExtend: |
--- |
| 6482 |
case scSignExtend: |
--- |
6482 |
case scSignExtend: |
--- |
| 6483 |
case scPtrToInt: |
--- |
6483 |
case scPtrToInt: |
--- |
| 6484 |
case scAddExpr: |
--- |
6484 |
case scAddExpr: |
--- |
| 6485 |
case scMulExpr: |
--- |
6485 |
case scMulExpr: |
--- |
| 6486 |
case scUDivExpr: |
--- |
6486 |
case scUDivExpr: |
--- |
| 6487 |
case scAddRecExpr: |
--- |
6487 |
case scAddRecExpr: |
--- |
| 6488 |
case scUMaxExpr: |
--- |
6488 |
case scUMaxExpr: |
--- |
| 6489 |
case scSMaxExpr: |
--- |
6489 |
case scSMaxExpr: |
--- |
| 6490 |
case scUMinExpr: |
--- |
6490 |
case scUMinExpr: |
--- |
| 6491 |
case scSMinExpr: |
--- |
6491 |
case scSMinExpr: |
--- |
| 6492 |
case scSequentialUMinExpr: |
--- |
6492 |
case scSequentialUMinExpr: |
--- |
| 6493 |
WorkList.push_back(Expr); |
0 |
6493 |
WorkList.push_back(Expr); |
0 |
| 6494 |
break; |
0 |
6494 |
break; |
0 |
| 6495 |
case scCouldNotCompute: |
0 |
6495 |
case scCouldNotCompute: |
0 |
| 6496 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
6496 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
| 6497 |
} |
--- |
6497 |
} |
--- |
| 6498 |
}; |
0 |
6498 |
}; |
0 |
| 6499 |
AddToWorklist(S); |
0 |
6499 |
AddToWorklist(S); |
0 |
| 6500 |
|
--- |
6500 |
|
--- |
| 6501 |
// Build worklist by queuing operands of N-ary expressions and phi nodes. |
--- |
6501 |
// Build worklist by queuing operands of N-ary expressions and phi nodes. |
--- |
| 6502 |
for (unsigned I = 0; I != WorkList.size(); ++I) { |
0 |
6502 |
for (unsigned I = 0; I != WorkList.size(); ++I) { |
0 |
| 6503 |
const SCEV *P = WorkList[I]; |
0 |
6503 |
const SCEV *P = WorkList[I]; |
0 |
| 6504 |
auto *UnknownS = dyn_cast(P); |
0 |
6504 |
auto *UnknownS = dyn_cast(P); |
0 |
| 6505 |
// If it is not a `SCEVUnknown`, just recurse into operands. |
--- |
6505 |
// If it is not a `SCEVUnknown`, just recurse into operands. |
--- |
| 6506 |
if (!UnknownS) { |
0 |
6506 |
if (!UnknownS) { |
0 |
| 6507 |
for (const SCEV *Op : P->operands()) |
0 |
6507 |
for (const SCEV *Op : P->operands()) |
0 |
| 6508 |
AddToWorklist(Op); |
0 |
6508 |
AddToWorklist(Op); |
0 |
| 6509 |
continue; |
0 |
6509 |
continue; |
0 |
| 6510 |
} |
0 |
6510 |
} |
0 |
| 6511 |
// `SCEVUnknown`'s require special treatment. |
--- |
6511 |
// `SCEVUnknown`'s require special treatment. |
--- |
| 6512 |
if (const PHINode *P = dyn_cast(UnknownS->getValue())) { |
0 |
6512 |
if (const PHINode *P = dyn_cast(UnknownS->getValue())) { |
0 |
| 6513 |
if (!PendingPhiRangesIter.insert(P).second) |
0 |
6513 |
if (!PendingPhiRangesIter.insert(P).second) |
0 |
| 6514 |
continue; |
0 |
6514 |
continue; |
0 |
| 6515 |
for (auto &Op : reverse(P->operands())) |
0 |
6515 |
for (auto &Op : reverse(P->operands())) |
0 |
| 6516 |
AddToWorklist(getSCEV(Op)); |
0 |
6516 |
AddToWorklist(getSCEV(Op)); |
0 |
| 6517 |
} |
--- |
6517 |
} |
--- |
| 6518 |
} |
--- |
6518 |
} |
--- |
| 6519 |
|
--- |
6519 |
|
--- |
| 6520 |
if (!WorkList.empty()) { |
0 |
6520 |
if (!WorkList.empty()) { |
0 |
| 6521 |
// Use getRangeRef to compute ranges for items in the worklist in reverse |
--- |
6521 |
// Use getRangeRef to compute ranges for items in the worklist in reverse |
--- |
| 6522 |
// order. This will force ranges for earlier operands to be computed before |
--- |
6522 |
// order. This will force ranges for earlier operands to be computed before |
--- |
| 6523 |
// their users in most cases. |
--- |
6523 |
// their users in most cases. |
--- |
| 6524 |
for (const SCEV *P : |
0 |
6524 |
for (const SCEV *P : |
0 |
| 6525 |
reverse(make_range(WorkList.begin() + 1, WorkList.end()))) { |
0 |
6525 |
reverse(make_range(WorkList.begin() + 1, WorkList.end()))) { |
0 |
| 6526 |
getRangeRef(P, SignHint); |
0 |
6526 |
getRangeRef(P, SignHint); |
0 |
| 6527 |
|
--- |
6527 |
|
--- |
| 6528 |
if (auto *UnknownS = dyn_cast(P)) |
0 |
6528 |
if (auto *UnknownS = dyn_cast(P)) |
0 |
| 6529 |
if (const PHINode *P = dyn_cast(UnknownS->getValue())) |
0 |
6529 |
if (const PHINode *P = dyn_cast(UnknownS->getValue())) |
0 |
| 6530 |
PendingPhiRangesIter.erase(P); |
0 |
6530 |
PendingPhiRangesIter.erase(P); |
0 |
| 6531 |
} |
--- |
6531 |
} |
--- |
| 6532 |
} |
--- |
6532 |
} |
--- |
| 6533 |
|
--- |
6533 |
|
--- |
| 6534 |
return getRangeRef(S, SignHint, 0); |
0 |
6534 |
return getRangeRef(S, SignHint, 0); |
0 |
| 6535 |
} |
0 |
6535 |
} |
0 |
| 6536 |
|
--- |
6536 |
|
--- |
| 6537 |
/// Determine the range for a particular SCEV. If SignHint is |
--- |
6537 |
/// Determine the range for a particular SCEV. If SignHint is |
--- |
| 6538 |
/// HINT_RANGE_UNSIGNED (resp. HINT_RANGE_SIGNED) then getRange prefers ranges |
--- |
6538 |
/// HINT_RANGE_UNSIGNED (resp. HINT_RANGE_SIGNED) then getRange prefers ranges |
--- |
| 6539 |
/// with a "cleaner" unsigned (resp. signed) representation. |
--- |
6539 |
/// with a "cleaner" unsigned (resp. signed) representation. |
--- |
| 6540 |
const ConstantRange &ScalarEvolution::getRangeRef( |
0 |
6540 |
const ConstantRange &ScalarEvolution::getRangeRef( |
0 |
| 6541 |
const SCEV *S, ScalarEvolution::RangeSignHint SignHint, unsigned Depth) { |
--- |
6541 |
const SCEV *S, ScalarEvolution::RangeSignHint SignHint, unsigned Depth) { |
--- |
| 6542 |
DenseMap &Cache = |
0 |
6542 |
DenseMap &Cache = |
0 |
| 6543 |
SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges |
--- |
6543 |
SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges |
--- |
| 6544 |
: SignedRanges; |
--- |
6544 |
: SignedRanges; |
--- |
| 6545 |
ConstantRange::PreferredRangeType RangeType = |
0 |
6545 |
ConstantRange::PreferredRangeType RangeType = |
0 |
| 6546 |
SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? ConstantRange::Unsigned |
0 |
6546 |
SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? ConstantRange::Unsigned |
0 |
| 6547 |
: ConstantRange::Signed; |
--- |
6547 |
: ConstantRange::Signed; |
--- |
| 6548 |
|
--- |
6548 |
|
--- |
| 6549 |
// See if we've computed this range already. |
--- |
6549 |
// See if we've computed this range already. |
--- |
| 6550 |
DenseMap::iterator I = Cache.find(S); |
0 |
6550 |
DenseMap::iterator I = Cache.find(S); |
0 |
| 6551 |
if (I != Cache.end()) |
0 |
6551 |
if (I != Cache.end()) |
0 |
| 6552 |
return I->second; |
0 |
6552 |
return I->second; |
0 |
| 6553 |
|
--- |
6553 |
|
--- |
| 6554 |
if (const SCEVConstant *C = dyn_cast(S)) |
0 |
6554 |
if (const SCEVConstant *C = dyn_cast(S)) |
0 |
| 6555 |
return setRange(C, SignHint, ConstantRange(C->getAPInt())); |
0 |
6555 |
return setRange(C, SignHint, ConstantRange(C->getAPInt())); |
0 |
| 6556 |
|
--- |
6556 |
|
--- |
| 6557 |
// Switch to iteratively computing the range for S, if it is part of a deeply |
--- |
6557 |
// Switch to iteratively computing the range for S, if it is part of a deeply |
--- |
| 6558 |
// nested expression. |
--- |
6558 |
// nested expression. |
--- |
| 6559 |
if (Depth > RangeIterThreshold) |
0 |
6559 |
if (Depth > RangeIterThreshold) |
0 |
| 6560 |
return getRangeRefIter(S, SignHint); |
0 |
6560 |
return getRangeRefIter(S, SignHint); |
0 |
| 6561 |
|
--- |
6561 |
|
--- |
| 6562 |
unsigned BitWidth = getTypeSizeInBits(S->getType()); |
0 |
6562 |
unsigned BitWidth = getTypeSizeInBits(S->getType()); |
0 |
| 6563 |
ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true); |
0 |
6563 |
ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true); |
0 |
| 6564 |
using OBO = OverflowingBinaryOperator; |
--- |
6564 |
using OBO = OverflowingBinaryOperator; |
--- |
| 6565 |
|
--- |
6565 |
|
--- |
| 6566 |
// If the value has known zeros, the maximum value will have those known zeros |
--- |
6566 |
// If the value has known zeros, the maximum value will have those known zeros |
--- |
| 6567 |
// as well. |
--- |
6567 |
// as well. |
--- |
| 6568 |
if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) { |
0 |
6568 |
if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) { |
0 |
| 6569 |
APInt Multiple = getNonZeroConstantMultiple(S); |
0 |
6569 |
APInt Multiple = getNonZeroConstantMultiple(S); |
0 |
| 6570 |
APInt Remainder = APInt::getMaxValue(BitWidth).urem(Multiple); |
0 |
6570 |
APInt Remainder = APInt::getMaxValue(BitWidth).urem(Multiple); |
0 |
| 6571 |
if (!Remainder.isZero()) |
0 |
6571 |
if (!Remainder.isZero()) |
0 |
| 6572 |
ConservativeResult = |
--- |
6572 |
ConservativeResult = |
--- |
| 6573 |
ConstantRange(APInt::getMinValue(BitWidth), |
0 |
6573 |
ConstantRange(APInt::getMinValue(BitWidth), |
0 |
| 6574 |
APInt::getMaxValue(BitWidth) - Remainder + 1); |
0 |
6574 |
APInt::getMaxValue(BitWidth) - Remainder + 1); |
0 |
| 6575 |
} |
0 |
6575 |
} |
0 |
| 6576 |
else { |
--- |
6576 |
else { |
--- |
| 6577 |
uint32_t TZ = getMinTrailingZeros(S); |
0 |
6577 |
uint32_t TZ = getMinTrailingZeros(S); |
0 |
| 6578 |
if (TZ != 0) { |
0 |
6578 |
if (TZ != 0) { |
0 |
| 6579 |
ConservativeResult = ConstantRange( |
0 |
6579 |
ConservativeResult = ConstantRange( |
0 |
| 6580 |
APInt::getSignedMinValue(BitWidth), |
0 |
6580 |
APInt::getSignedMinValue(BitWidth), |
0 |
| 6581 |
APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1); |
0 |
6581 |
APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1); |
0 |
| 6582 |
} |
--- |
6582 |
} |
--- |
| 6583 |
} |
--- |
6583 |
} |
--- |
| 6584 |
|
--- |
6584 |
|
--- |
| 6585 |
switch (S->getSCEVType()) { |
0 |
6585 |
switch (S->getSCEVType()) { |
0 |
| 6586 |
case scConstant: |
0 |
6586 |
case scConstant: |
0 |
| 6587 |
llvm_unreachable("Already handled above."); |
0 |
6587 |
llvm_unreachable("Already handled above."); |
0 |
| 6588 |
case scVScale: |
0 |
6588 |
case scVScale: |
0 |
| 6589 |
return setRange(S, SignHint, getVScaleRange(&F, BitWidth)); |
0 |
6589 |
return setRange(S, SignHint, getVScaleRange(&F, BitWidth)); |
0 |
| 6590 |
case scTruncate: { |
0 |
6590 |
case scTruncate: { |
0 |
| 6591 |
const SCEVTruncateExpr *Trunc = cast(S); |
0 |
6591 |
const SCEVTruncateExpr *Trunc = cast(S); |
0 |
| 6592 |
ConstantRange X = getRangeRef(Trunc->getOperand(), SignHint, Depth + 1); |
0 |
6592 |
ConstantRange X = getRangeRef(Trunc->getOperand(), SignHint, Depth + 1); |
0 |
| 6593 |
return setRange( |
0 |
6593 |
return setRange( |
0 |
| 6594 |
Trunc, SignHint, |
--- |
6594 |
Trunc, SignHint, |
--- |
| 6595 |
ConservativeResult.intersectWith(X.truncate(BitWidth), RangeType)); |
0 |
6595 |
ConservativeResult.intersectWith(X.truncate(BitWidth), RangeType)); |
0 |
| 6596 |
} |
0 |
6596 |
} |
0 |
| 6597 |
case scZeroExtend: { |
0 |
6597 |
case scZeroExtend: { |
0 |
| 6598 |
const SCEVZeroExtendExpr *ZExt = cast(S); |
0 |
6598 |
const SCEVZeroExtendExpr *ZExt = cast(S); |
0 |
| 6599 |
ConstantRange X = getRangeRef(ZExt->getOperand(), SignHint, Depth + 1); |
0 |
6599 |
ConstantRange X = getRangeRef(ZExt->getOperand(), SignHint, Depth + 1); |
0 |
| 6600 |
return setRange( |
0 |
6600 |
return setRange( |
0 |
| 6601 |
ZExt, SignHint, |
--- |
6601 |
ZExt, SignHint, |
--- |
| 6602 |
ConservativeResult.intersectWith(X.zeroExtend(BitWidth), RangeType)); |
0 |
6602 |
ConservativeResult.intersectWith(X.zeroExtend(BitWidth), RangeType)); |
0 |
| 6603 |
} |
0 |
6603 |
} |
0 |
| 6604 |
case scSignExtend: { |
0 |
6604 |
case scSignExtend: { |
0 |
| 6605 |
const SCEVSignExtendExpr *SExt = cast(S); |
0 |
6605 |
const SCEVSignExtendExpr *SExt = cast(S); |
0 |
| 6606 |
ConstantRange X = getRangeRef(SExt->getOperand(), SignHint, Depth + 1); |
0 |
6606 |
ConstantRange X = getRangeRef(SExt->getOperand(), SignHint, Depth + 1); |
0 |
| 6607 |
return setRange( |
0 |
6607 |
return setRange( |
0 |
| 6608 |
SExt, SignHint, |
--- |
6608 |
SExt, SignHint, |
--- |
| 6609 |
ConservativeResult.intersectWith(X.signExtend(BitWidth), RangeType)); |
0 |
6609 |
ConservativeResult.intersectWith(X.signExtend(BitWidth), RangeType)); |
0 |
| 6610 |
} |
0 |
6610 |
} |
0 |
| 6611 |
case scPtrToInt: { |
0 |
6611 |
case scPtrToInt: { |
0 |
| 6612 |
const SCEVPtrToIntExpr *PtrToInt = cast(S); |
0 |
6612 |
const SCEVPtrToIntExpr *PtrToInt = cast(S); |
0 |
| 6613 |
ConstantRange X = getRangeRef(PtrToInt->getOperand(), SignHint, Depth + 1); |
0 |
6613 |
ConstantRange X = getRangeRef(PtrToInt->getOperand(), SignHint, Depth + 1); |
0 |
| 6614 |
return setRange(PtrToInt, SignHint, X); |
0 |
6614 |
return setRange(PtrToInt, SignHint, X); |
0 |
| 6615 |
} |
0 |
6615 |
} |
0 |
| 6616 |
case scAddExpr: { |
0 |
6616 |
case scAddExpr: { |
0 |
| 6617 |
const SCEVAddExpr *Add = cast(S); |
0 |
6617 |
const SCEVAddExpr *Add = cast(S); |
0 |
| 6618 |
ConstantRange X = getRangeRef(Add->getOperand(0), SignHint, Depth + 1); |
0 |
6618 |
ConstantRange X = getRangeRef(Add->getOperand(0), SignHint, Depth + 1); |
0 |
| 6619 |
unsigned WrapType = OBO::AnyWrap; |
0 |
6619 |
unsigned WrapType = OBO::AnyWrap; |
0 |
| 6620 |
if (Add->hasNoSignedWrap()) |
0 |
6620 |
if (Add->hasNoSignedWrap()) |
0 |
| 6621 |
WrapType |= OBO::NoSignedWrap; |
0 |
6621 |
WrapType |= OBO::NoSignedWrap; |
0 |
| 6622 |
if (Add->hasNoUnsignedWrap()) |
0 |
6622 |
if (Add->hasNoUnsignedWrap()) |
0 |
| 6623 |
WrapType |= OBO::NoUnsignedWrap; |
0 |
6623 |
WrapType |= OBO::NoUnsignedWrap; |
0 |
| 6624 |
for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) |
0 |
6624 |
for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) |
0 |
| 6625 |
X = X.addWithNoWrap(getRangeRef(Add->getOperand(i), SignHint, Depth + 1), |
0 |
6625 |
X = X.addWithNoWrap(getRangeRef(Add->getOperand(i), SignHint, Depth + 1), |
0 |
| 6626 |
WrapType, RangeType); |
0 |
6626 |
WrapType, RangeType); |
0 |
| 6627 |
return setRange(Add, SignHint, |
0 |
6627 |
return setRange(Add, SignHint, |
0 |
| 6628 |
ConservativeResult.intersectWith(X, RangeType)); |
0 |
6628 |
ConservativeResult.intersectWith(X, RangeType)); |
0 |
| 6629 |
} |
0 |
6629 |
} |
0 |
| 6630 |
case scMulExpr: { |
0 |
6630 |
case scMulExpr: { |
0 |
| 6631 |
const SCEVMulExpr *Mul = cast(S); |
0 |
6631 |
const SCEVMulExpr *Mul = cast(S); |
0 |
| 6632 |
ConstantRange X = getRangeRef(Mul->getOperand(0), SignHint, Depth + 1); |
0 |
6632 |
ConstantRange X = getRangeRef(Mul->getOperand(0), SignHint, Depth + 1); |
0 |
| 6633 |
for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) |
0 |
6633 |
for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) |
0 |
| 6634 |
X = X.multiply(getRangeRef(Mul->getOperand(i), SignHint, Depth + 1)); |
0 |
6634 |
X = X.multiply(getRangeRef(Mul->getOperand(i), SignHint, Depth + 1)); |
0 |
| 6635 |
return setRange(Mul, SignHint, |
0 |
6635 |
return setRange(Mul, SignHint, |
0 |
| 6636 |
ConservativeResult.intersectWith(X, RangeType)); |
0 |
6636 |
ConservativeResult.intersectWith(X, RangeType)); |
0 |
| 6637 |
} |
0 |
6637 |
} |
0 |
| 6638 |
case scUDivExpr: { |
0 |
6638 |
case scUDivExpr: { |
0 |
| 6639 |
const SCEVUDivExpr *UDiv = cast(S); |
0 |
6639 |
const SCEVUDivExpr *UDiv = cast(S); |
0 |
| 6640 |
ConstantRange X = getRangeRef(UDiv->getLHS(), SignHint, Depth + 1); |
0 |
6640 |
ConstantRange X = getRangeRef(UDiv->getLHS(), SignHint, Depth + 1); |
0 |
| 6641 |
ConstantRange Y = getRangeRef(UDiv->getRHS(), SignHint, Depth + 1); |
0 |
6641 |
ConstantRange Y = getRangeRef(UDiv->getRHS(), SignHint, Depth + 1); |
0 |
| 6642 |
return setRange(UDiv, SignHint, |
0 |
6642 |
return setRange(UDiv, SignHint, |
0 |
| 6643 |
ConservativeResult.intersectWith(X.udiv(Y), RangeType)); |
0 |
6643 |
ConservativeResult.intersectWith(X.udiv(Y), RangeType)); |
0 |
| 6644 |
} |
0 |
6644 |
} |
0 |
| 6645 |
case scAddRecExpr: { |
0 |
6645 |
case scAddRecExpr: { |
0 |
| 6646 |
const SCEVAddRecExpr *AddRec = cast(S); |
0 |
6646 |
const SCEVAddRecExpr *AddRec = cast(S); |
0 |
| 6647 |
// If there's no unsigned wrap, the value will never be less than its |
--- |
6647 |
// If there's no unsigned wrap, the value will never be less than its |
--- |
| 6648 |
// initial value. |
--- |
6648 |
// initial value. |
--- |
| 6649 |
if (AddRec->hasNoUnsignedWrap()) { |
0 |
6649 |
if (AddRec->hasNoUnsignedWrap()) { |
0 |
| 6650 |
APInt UnsignedMinValue = getUnsignedRangeMin(AddRec->getStart()); |
0 |
6650 |
APInt UnsignedMinValue = getUnsignedRangeMin(AddRec->getStart()); |
0 |
| 6651 |
if (!UnsignedMinValue.isZero()) |
0 |
6651 |
if (!UnsignedMinValue.isZero()) |
0 |
| 6652 |
ConservativeResult = ConservativeResult.intersectWith( |
0 |
6652 |
ConservativeResult = ConservativeResult.intersectWith( |
0 |
| 6653 |
ConstantRange(UnsignedMinValue, APInt(BitWidth, 0)), RangeType); |
0 |
6653 |
ConstantRange(UnsignedMinValue, APInt(BitWidth, 0)), RangeType); |
0 |
| 6654 |
} |
0 |
6654 |
} |
0 |
| 6655 |
|
--- |
6655 |
|
--- |
| 6656 |
// If there's no signed wrap, and all the operands except initial value have |
--- |
6656 |
// If there's no signed wrap, and all the operands except initial value have |
--- |
| 6657 |
// the same sign or zero, the value won't ever be: |
--- |
6657 |
// the same sign or zero, the value won't ever be: |
--- |
| 6658 |
// 1: smaller than initial value if operands are non negative, |
--- |
6658 |
// 1: smaller than initial value if operands are non negative, |
--- |
| 6659 |
// 2: bigger than initial value if operands are non positive. |
--- |
6659 |
// 2: bigger than initial value if operands are non positive. |
--- |
| 6660 |
// For both cases, value can not cross signed min/max boundary. |
--- |
6660 |
// For both cases, value can not cross signed min/max boundary. |
--- |
| 6661 |
if (AddRec->hasNoSignedWrap()) { |
0 |
6661 |
if (AddRec->hasNoSignedWrap()) { |
0 |
| 6662 |
bool AllNonNeg = true; |
0 |
6662 |
bool AllNonNeg = true; |
0 |
| 6663 |
bool AllNonPos = true; |
0 |
6663 |
bool AllNonPos = true; |
0 |
| 6664 |
for (unsigned i = 1, e = AddRec->getNumOperands(); i != e; ++i) { |
0 |
6664 |
for (unsigned i = 1, e = AddRec->getNumOperands(); i != e; ++i) { |
0 |
| 6665 |
if (!isKnownNonNegative(AddRec->getOperand(i))) |
0 |
6665 |
if (!isKnownNonNegative(AddRec->getOperand(i))) |
0 |
| 6666 |
AllNonNeg = false; |
0 |
6666 |
AllNonNeg = false; |
0 |
| 6667 |
if (!isKnownNonPositive(AddRec->getOperand(i))) |
0 |
6667 |
if (!isKnownNonPositive(AddRec->getOperand(i))) |
0 |
| 6668 |
AllNonPos = false; |
0 |
6668 |
AllNonPos = false; |
0 |
| 6669 |
} |
--- |
6669 |
} |
--- |
| 6670 |
if (AllNonNeg) |
0 |
6670 |
if (AllNonNeg) |
0 |
| 6671 |
ConservativeResult = ConservativeResult.intersectWith( |
0 |
6671 |
ConservativeResult = ConservativeResult.intersectWith( |
0 |
| 6672 |
ConstantRange::getNonEmpty(getSignedRangeMin(AddRec->getStart()), |
0 |
6672 |
ConstantRange::getNonEmpty(getSignedRangeMin(AddRec->getStart()), |
0 |
| 6673 |
APInt::getSignedMinValue(BitWidth)), |
0 |
6673 |
APInt::getSignedMinValue(BitWidth)), |
0 |
| 6674 |
RangeType); |
0 |
6674 |
RangeType); |
0 |
| 6675 |
else if (AllNonPos) |
0 |
6675 |
else if (AllNonPos) |
0 |
| 6676 |
ConservativeResult = ConservativeResult.intersectWith( |
0 |
6676 |
ConservativeResult = ConservativeResult.intersectWith( |
0 |
| 6677 |
ConstantRange::getNonEmpty(APInt::getSignedMinValue(BitWidth), |
0 |
6677 |
ConstantRange::getNonEmpty(APInt::getSignedMinValue(BitWidth), |
0 |
| 6678 |
getSignedRangeMax(AddRec->getStart()) + |
0 |
6678 |
getSignedRangeMax(AddRec->getStart()) + |
0 |
| 6679 |
1), |
--- |
6679 |
1), |
--- |
| 6680 |
RangeType); |
0 |
6680 |
RangeType); |
0 |
| 6681 |
} |
--- |
6681 |
} |
--- |
| 6682 |
|
--- |
6682 |
|
--- |
| 6683 |
// TODO: non-affine addrec |
--- |
6683 |
// TODO: non-affine addrec |
--- |
| 6684 |
if (AddRec->isAffine()) { |
0 |
6684 |
if (AddRec->isAffine()) { |
0 |
| 6685 |
const SCEV *MaxBEScev = |
--- |
6685 |
const SCEV *MaxBEScev = |
--- |
| 6686 |
getConstantMaxBackedgeTakenCount(AddRec->getLoop()); |
0 |
6686 |
getConstantMaxBackedgeTakenCount(AddRec->getLoop()); |
0 |
| 6687 |
if (!isa(MaxBEScev)) { |
0 |
6687 |
if (!isa(MaxBEScev)) { |
0 |
| 6688 |
APInt MaxBECount = cast(MaxBEScev)->getAPInt(); |
0 |
6688 |
APInt MaxBECount = cast(MaxBEScev)->getAPInt(); |
0 |
| 6689 |
|
--- |
6689 |
|
--- |
| 6690 |
// Adjust MaxBECount to the same bitwidth as AddRec. We can truncate if |
--- |
6690 |
// Adjust MaxBECount to the same bitwidth as AddRec. We can truncate if |
--- |
| 6691 |
// MaxBECount's active bits are all <= AddRec's bit width. |
--- |
6691 |
// MaxBECount's active bits are all <= AddRec's bit width. |
--- |
| 6692 |
if (MaxBECount.getBitWidth() > BitWidth && |
0 |
6692 |
if (MaxBECount.getBitWidth() > BitWidth && |
0 |
| 6693 |
MaxBECount.getActiveBits() <= BitWidth) |
0 |
6693 |
MaxBECount.getActiveBits() <= BitWidth) |
0 |
| 6694 |
MaxBECount = MaxBECount.trunc(BitWidth); |
0 |
6694 |
MaxBECount = MaxBECount.trunc(BitWidth); |
0 |
| 6695 |
else if (MaxBECount.getBitWidth() < BitWidth) |
0 |
6695 |
else if (MaxBECount.getBitWidth() < BitWidth) |
0 |
| 6696 |
MaxBECount = MaxBECount.zext(BitWidth); |
0 |
6696 |
MaxBECount = MaxBECount.zext(BitWidth); |
0 |
| 6697 |
|
--- |
6697 |
|
--- |
| 6698 |
if (MaxBECount.getBitWidth() == BitWidth) { |
0 |
6698 |
if (MaxBECount.getBitWidth() == BitWidth) { |
0 |
| 6699 |
auto RangeFromAffine = getRangeForAffineAR( |
--- |
6699 |
auto RangeFromAffine = getRangeForAffineAR( |
--- |
| 6700 |
AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount); |
0 |
6700 |
AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount); |
0 |
| 6701 |
ConservativeResult = |
--- |
6701 |
ConservativeResult = |
--- |
| 6702 |
ConservativeResult.intersectWith(RangeFromAffine, RangeType); |
0 |
6702 |
ConservativeResult.intersectWith(RangeFromAffine, RangeType); |
0 |
| 6703 |
|
--- |
6703 |
|
--- |
| 6704 |
auto RangeFromFactoring = getRangeViaFactoring( |
--- |
6704 |
auto RangeFromFactoring = getRangeViaFactoring( |
--- |
| 6705 |
AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount); |
0 |
6705 |
AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount); |
0 |
| 6706 |
ConservativeResult = |
--- |
6706 |
ConservativeResult = |
--- |
| 6707 |
ConservativeResult.intersectWith(RangeFromFactoring, RangeType); |
0 |
6707 |
ConservativeResult.intersectWith(RangeFromFactoring, RangeType); |
0 |
| 6708 |
} |
0 |
6708 |
} |
0 |
| 6709 |
} |
0 |
6709 |
} |
0 |
| 6710 |
|
--- |
6710 |
|
--- |
| 6711 |
// Now try symbolic BE count and more powerful methods. |
--- |
6711 |
// Now try symbolic BE count and more powerful methods. |
--- |
| 6712 |
if (UseExpensiveRangeSharpening) { |
0 |
6712 |
if (UseExpensiveRangeSharpening) { |
0 |
| 6713 |
const SCEV *SymbolicMaxBECount = |
--- |
6713 |
const SCEV *SymbolicMaxBECount = |
--- |
| 6714 |
getSymbolicMaxBackedgeTakenCount(AddRec->getLoop()); |
0 |
6714 |
getSymbolicMaxBackedgeTakenCount(AddRec->getLoop()); |
0 |
| 6715 |
if (!isa(SymbolicMaxBECount) && |
0 |
6715 |
if (!isa(SymbolicMaxBECount) && |
0 |
| 6716 |
getTypeSizeInBits(MaxBEScev->getType()) <= BitWidth && |
0 |
6716 |
getTypeSizeInBits(MaxBEScev->getType()) <= BitWidth && |
0 |
| 6717 |
AddRec->hasNoSelfWrap()) { |
0 |
6717 |
AddRec->hasNoSelfWrap()) { |
0 |
| 6718 |
auto RangeFromAffineNew = getRangeForAffineNoSelfWrappingAR( |
--- |
6718 |
auto RangeFromAffineNew = getRangeForAffineNoSelfWrappingAR( |
--- |
| 6719 |
AddRec, SymbolicMaxBECount, BitWidth, SignHint); |
0 |
6719 |
AddRec, SymbolicMaxBECount, BitWidth, SignHint); |
0 |
| 6720 |
ConservativeResult = |
--- |
6720 |
ConservativeResult = |
--- |
| 6721 |
ConservativeResult.intersectWith(RangeFromAffineNew, RangeType); |
0 |
6721 |
ConservativeResult.intersectWith(RangeFromAffineNew, RangeType); |
0 |
| 6722 |
} |
0 |
6722 |
} |
0 |
| 6723 |
} |
--- |
6723 |
} |
--- |
| 6724 |
} |
--- |
6724 |
} |
--- |
| 6725 |
|
--- |
6725 |
|
--- |
| 6726 |
return setRange(AddRec, SignHint, std::move(ConservativeResult)); |
0 |
6726 |
return setRange(AddRec, SignHint, std::move(ConservativeResult)); |
0 |
| 6727 |
} |
--- |
6727 |
} |
--- |
| 6728 |
case scUMaxExpr: |
0 |
6728 |
case scUMaxExpr: |
0 |
| 6729 |
case scSMaxExpr: |
--- |
6729 |
case scSMaxExpr: |
--- |
| 6730 |
case scUMinExpr: |
--- |
6730 |
case scUMinExpr: |
--- |
| 6731 |
case scSMinExpr: |
--- |
6731 |
case scSMinExpr: |
--- |
| 6732 |
case scSequentialUMinExpr: { |
--- |
6732 |
case scSequentialUMinExpr: { |
--- |
| 6733 |
Intrinsic::ID ID; |
--- |
6733 |
Intrinsic::ID ID; |
--- |
| 6734 |
switch (S->getSCEVType()) { |
0 |
6734 |
switch (S->getSCEVType()) { |
0 |
| 6735 |
case scUMaxExpr: |
0 |
6735 |
case scUMaxExpr: |
0 |
| 6736 |
ID = Intrinsic::umax; |
0 |
6736 |
ID = Intrinsic::umax; |
0 |
| 6737 |
break; |
0 |
6737 |
break; |
0 |
| 6738 |
case scSMaxExpr: |
0 |
6738 |
case scSMaxExpr: |
0 |
| 6739 |
ID = Intrinsic::smax; |
0 |
6739 |
ID = Intrinsic::smax; |
0 |
| 6740 |
break; |
0 |
6740 |
break; |
0 |
| 6741 |
case scUMinExpr: |
0 |
6741 |
case scUMinExpr: |
0 |
| 6742 |
case scSequentialUMinExpr: |
--- |
6742 |
case scSequentialUMinExpr: |
--- |
| 6743 |
ID = Intrinsic::umin; |
0 |
6743 |
ID = Intrinsic::umin; |
0 |
| 6744 |
break; |
0 |
6744 |
break; |
0 |
| 6745 |
case scSMinExpr: |
0 |
6745 |
case scSMinExpr: |
0 |
| 6746 |
ID = Intrinsic::smin; |
0 |
6746 |
ID = Intrinsic::smin; |
0 |
| 6747 |
break; |
0 |
6747 |
break; |
0 |
| 6748 |
default: |
0 |
6748 |
default: |
0 |
| 6749 |
llvm_unreachable("Unknown SCEVMinMaxExpr/SCEVSequentialMinMaxExpr."); |
0 |
6749 |
llvm_unreachable("Unknown SCEVMinMaxExpr/SCEVSequentialMinMaxExpr."); |
0 |
| 6750 |
} |
--- |
6750 |
} |
--- |
| 6751 |
|
--- |
6751 |
|
--- |
| 6752 |
const auto *NAry = cast(S); |
0 |
6752 |
const auto *NAry = cast(S); |
0 |
| 6753 |
ConstantRange X = getRangeRef(NAry->getOperand(0), SignHint, Depth + 1); |
0 |
6753 |
ConstantRange X = getRangeRef(NAry->getOperand(0), SignHint, Depth + 1); |
0 |
| 6754 |
for (unsigned i = 1, e = NAry->getNumOperands(); i != e; ++i) |
0 |
6754 |
for (unsigned i = 1, e = NAry->getNumOperands(); i != e; ++i) |
0 |
| 6755 |
X = X.intrinsic( |
0 |
6755 |
X = X.intrinsic( |
0 |
| 6756 |
ID, {X, getRangeRef(NAry->getOperand(i), SignHint, Depth + 1)}); |
0 |
6756 |
ID, {X, getRangeRef(NAry->getOperand(i), SignHint, Depth + 1)}); |
0 |
| 6757 |
return setRange(S, SignHint, |
0 |
6757 |
return setRange(S, SignHint, |
0 |
| 6758 |
ConservativeResult.intersectWith(X, RangeType)); |
0 |
6758 |
ConservativeResult.intersectWith(X, RangeType)); |
0 |
| 6759 |
} |
--- |
6759 |
} |
--- |
| 6760 |
case scUnknown: { |
0 |
6760 |
case scUnknown: { |
0 |
| 6761 |
const SCEVUnknown *U = cast(S); |
0 |
6761 |
const SCEVUnknown *U = cast(S); |
0 |
| 6762 |
Value *V = U->getValue(); |
0 |
6762 |
Value *V = U->getValue(); |
0 |
| 6763 |
|
--- |
6763 |
|
--- |
| 6764 |
// Check if the IR explicitly contains !range metadata. |
--- |
6764 |
// Check if the IR explicitly contains !range metadata. |
--- |
| 6765 |
std::optional MDRange = GetRangeFromMetadata(V); |
0 |
6765 |
std::optional MDRange = GetRangeFromMetadata(V); |
0 |
| 6766 |
if (MDRange) |
0 |
6766 |
if (MDRange) |
0 |
| 6767 |
ConservativeResult = |
--- |
6767 |
ConservativeResult = |
--- |
| 6768 |
ConservativeResult.intersectWith(*MDRange, RangeType); |
0 |
6768 |
ConservativeResult.intersectWith(*MDRange, RangeType); |
0 |
| 6769 |
|
--- |
6769 |
|
--- |
| 6770 |
// Use facts about recurrences in the underlying IR. Note that add |
--- |
6770 |
// Use facts about recurrences in the underlying IR. Note that add |
--- |
| 6771 |
// recurrences are AddRecExprs and thus don't hit this path. This |
--- |
6771 |
// recurrences are AddRecExprs and thus don't hit this path. This |
--- |
| 6772 |
// primarily handles shift recurrences. |
--- |
6772 |
// primarily handles shift recurrences. |
--- |
| 6773 |
auto CR = getRangeForUnknownRecurrence(U); |
0 |
6773 |
auto CR = getRangeForUnknownRecurrence(U); |
0 |
| 6774 |
ConservativeResult = ConservativeResult.intersectWith(CR); |
0 |
6774 |
ConservativeResult = ConservativeResult.intersectWith(CR); |
0 |
| 6775 |
|
--- |
6775 |
|
--- |
| 6776 |
// See if ValueTracking can give us a useful range. |
--- |
6776 |
// See if ValueTracking can give us a useful range. |
--- |
| 6777 |
const DataLayout &DL = getDataLayout(); |
0 |
6777 |
const DataLayout &DL = getDataLayout(); |
0 |
| 6778 |
KnownBits Known = computeKnownBits(V, DL, 0, &AC, nullptr, &DT); |
0 |
6778 |
KnownBits Known = computeKnownBits(V, DL, 0, &AC, nullptr, &DT); |
0 |
| 6779 |
if (Known.getBitWidth() != BitWidth) |
0 |
6779 |
if (Known.getBitWidth() != BitWidth) |
0 |
| 6780 |
Known = Known.zextOrTrunc(BitWidth); |
0 |
6780 |
Known = Known.zextOrTrunc(BitWidth); |
0 |
| 6781 |
|
--- |
6781 |
|
--- |
| 6782 |
// ValueTracking may be able to compute a tighter result for the number of |
--- |
6782 |
// ValueTracking may be able to compute a tighter result for the number of |
--- |
| 6783 |
// sign bits than for the value of those sign bits. |
--- |
6783 |
// sign bits than for the value of those sign bits. |
--- |
| 6784 |
unsigned NS = ComputeNumSignBits(V, DL, 0, &AC, nullptr, &DT); |
0 |
6784 |
unsigned NS = ComputeNumSignBits(V, DL, 0, &AC, nullptr, &DT); |
0 |
| 6785 |
if (U->getType()->isPointerTy()) { |
0 |
6785 |
if (U->getType()->isPointerTy()) { |
0 |
| 6786 |
// If the pointer size is larger than the index size type, this can cause |
--- |
6786 |
// If the pointer size is larger than the index size type, this can cause |
--- |
| 6787 |
// NS to be larger than BitWidth. So compensate for this. |
--- |
6787 |
// NS to be larger than BitWidth. So compensate for this. |
--- |
| 6788 |
unsigned ptrSize = DL.getPointerTypeSizeInBits(U->getType()); |
0 |
6788 |
unsigned ptrSize = DL.getPointerTypeSizeInBits(U->getType()); |
0 |
| 6789 |
int ptrIdxDiff = ptrSize - BitWidth; |
0 |
6789 |
int ptrIdxDiff = ptrSize - BitWidth; |
0 |
| 6790 |
if (ptrIdxDiff > 0 && ptrSize > BitWidth && NS > (unsigned)ptrIdxDiff) |
0 |
6790 |
if (ptrIdxDiff > 0 && ptrSize > BitWidth && NS > (unsigned)ptrIdxDiff) |
0 |
| 6791 |
NS -= ptrIdxDiff; |
0 |
6791 |
NS -= ptrIdxDiff; |
0 |
| 6792 |
} |
--- |
6792 |
} |
--- |
| 6793 |
|
--- |
6793 |
|
--- |
| 6794 |
if (NS > 1) { |
0 |
6794 |
if (NS > 1) { |
0 |
| 6795 |
// If we know any of the sign bits, we know all of the sign bits. |
--- |
6795 |
// If we know any of the sign bits, we know all of the sign bits. |
--- |
| 6796 |
if (!Known.Zero.getHiBits(NS).isZero()) |
0 |
6796 |
if (!Known.Zero.getHiBits(NS).isZero()) |
0 |
| 6797 |
Known.Zero.setHighBits(NS); |
0 |
6797 |
Known.Zero.setHighBits(NS); |
0 |
| 6798 |
if (!Known.One.getHiBits(NS).isZero()) |
0 |
6798 |
if (!Known.One.getHiBits(NS).isZero()) |
0 |
| 6799 |
Known.One.setHighBits(NS); |
0 |
6799 |
Known.One.setHighBits(NS); |
0 |
| 6800 |
} |
--- |
6800 |
} |
--- |
| 6801 |
|
--- |
6801 |
|
--- |
| 6802 |
if (Known.getMinValue() != Known.getMaxValue() + 1) |
0 |
6802 |
if (Known.getMinValue() != Known.getMaxValue() + 1) |
0 |
| 6803 |
ConservativeResult = ConservativeResult.intersectWith( |
0 |
6803 |
ConservativeResult = ConservativeResult.intersectWith( |
0 |
| 6804 |
ConstantRange(Known.getMinValue(), Known.getMaxValue() + 1), |
0 |
6804 |
ConstantRange(Known.getMinValue(), Known.getMaxValue() + 1), |
0 |
| 6805 |
RangeType); |
0 |
6805 |
RangeType); |
0 |
| 6806 |
if (NS > 1) |
0 |
6806 |
if (NS > 1) |
0 |
| 6807 |
ConservativeResult = ConservativeResult.intersectWith( |
0 |
6807 |
ConservativeResult = ConservativeResult.intersectWith( |
0 |
| 6808 |
ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1), |
0 |
6808 |
ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1), |
0 |
| 6809 |
APInt::getSignedMaxValue(BitWidth).ashr(NS - 1) + 1), |
0 |
6809 |
APInt::getSignedMaxValue(BitWidth).ashr(NS - 1) + 1), |
0 |
| 6810 |
RangeType); |
0 |
6810 |
RangeType); |
0 |
| 6811 |
|
--- |
6811 |
|
--- |
| 6812 |
if (U->getType()->isPointerTy() && SignHint == HINT_RANGE_UNSIGNED) { |
0 |
6812 |
if (U->getType()->isPointerTy() && SignHint == HINT_RANGE_UNSIGNED) { |
0 |
| 6813 |
// Strengthen the range if the underlying IR value is a |
--- |
6813 |
// Strengthen the range if the underlying IR value is a |
--- |
| 6814 |
// global/alloca/heap allocation using the size of the object. |
--- |
6814 |
// global/alloca/heap allocation using the size of the object. |
--- |
| 6815 |
ObjectSizeOpts Opts; |
0 |
6815 |
ObjectSizeOpts Opts; |
0 |
| 6816 |
Opts.RoundToAlign = false; |
0 |
6816 |
Opts.RoundToAlign = false; |
0 |
| 6817 |
Opts.NullIsUnknownSize = true; |
0 |
6817 |
Opts.NullIsUnknownSize = true; |
0 |
| 6818 |
uint64_t ObjSize; |
--- |
6818 |
uint64_t ObjSize; |
--- |
| 6819 |
if ((isa(V) || isa(V) || |
0 |
6819 |
if ((isa(V) || isa(V) || |
0 |
| 6820 |
isAllocationFn(V, &TLI)) && |
0 |
6820 |
isAllocationFn(V, &TLI)) && |
0 |
| 6821 |
getObjectSize(V, ObjSize, DL, &TLI, Opts) && ObjSize > 1) { |
0 |
6821 |
getObjectSize(V, ObjSize, DL, &TLI, Opts) && ObjSize > 1) { |
0 |
| 6822 |
// The highest address the object can start is ObjSize bytes before the |
--- |
6822 |
// The highest address the object can start is ObjSize bytes before the |
--- |
| 6823 |
// end (unsigned max value). If this value is not a multiple of the |
--- |
6823 |
// end (unsigned max value). If this value is not a multiple of the |
--- |
| 6824 |
// alignment, the last possible start value is the next lowest multiple |
--- |
6824 |
// alignment, the last possible start value is the next lowest multiple |
--- |
| 6825 |
// of the alignment. Note: The computations below cannot overflow, |
--- |
6825 |
// of the alignment. Note: The computations below cannot overflow, |
--- |
| 6826 |
// because if they would there's no possible start address for the |
--- |
6826 |
// because if they would there's no possible start address for the |
--- |
| 6827 |
// object. |
--- |
6827 |
// object. |
--- |
| 6828 |
APInt MaxVal = APInt::getMaxValue(BitWidth) - APInt(BitWidth, ObjSize); |
0 |
6828 |
APInt MaxVal = APInt::getMaxValue(BitWidth) - APInt(BitWidth, ObjSize); |
0 |
| 6829 |
uint64_t Align = U->getValue()->getPointerAlignment(DL).value(); |
0 |
6829 |
uint64_t Align = U->getValue()->getPointerAlignment(DL).value(); |
0 |
| 6830 |
uint64_t Rem = MaxVal.urem(Align); |
0 |
6830 |
uint64_t Rem = MaxVal.urem(Align); |
0 |
| 6831 |
MaxVal -= APInt(BitWidth, Rem); |
0 |
6831 |
MaxVal -= APInt(BitWidth, Rem); |
0 |
| 6832 |
APInt MinVal = APInt::getZero(BitWidth); |
0 |
6832 |
APInt MinVal = APInt::getZero(BitWidth); |
0 |
| 6833 |
if (llvm::isKnownNonZero(V, DL)) |
0 |
6833 |
if (llvm::isKnownNonZero(V, DL)) |
0 |
| 6834 |
MinVal = Align; |
0 |
6834 |
MinVal = Align; |
0 |
| 6835 |
ConservativeResult = ConservativeResult.intersectWith( |
0 |
6835 |
ConservativeResult = ConservativeResult.intersectWith( |
0 |
| 6836 |
{MinVal, MaxVal + 1}, RangeType); |
0 |
6836 |
{MinVal, MaxVal + 1}, RangeType); |
0 |
| 6837 |
} |
0 |
6837 |
} |
0 |
| 6838 |
} |
--- |
6838 |
} |
--- |
| 6839 |
|
--- |
6839 |
|
--- |
| 6840 |
// A range of Phi is a subset of union of all ranges of its input. |
--- |
6840 |
// A range of Phi is a subset of union of all ranges of its input. |
--- |
| 6841 |
if (PHINode *Phi = dyn_cast(V)) { |
0 |
6841 |
if (PHINode *Phi = dyn_cast(V)) { |
0 |
| 6842 |
// Make sure that we do not run over cycled Phis. |
--- |
6842 |
// Make sure that we do not run over cycled Phis. |
--- |
| 6843 |
if (PendingPhiRanges.insert(Phi).second) { |
0 |
6843 |
if (PendingPhiRanges.insert(Phi).second) { |
0 |
| 6844 |
ConstantRange RangeFromOps(BitWidth, /*isFullSet=*/false); |
0 |
6844 |
ConstantRange RangeFromOps(BitWidth, /*isFullSet=*/false); |
0 |
| 6845 |
|
--- |
6845 |
|
--- |
| 6846 |
for (const auto &Op : Phi->operands()) { |
0 |
6846 |
for (const auto &Op : Phi->operands()) { |
0 |
| 6847 |
auto OpRange = getRangeRef(getSCEV(Op), SignHint, Depth + 1); |
0 |
6847 |
auto OpRange = getRangeRef(getSCEV(Op), SignHint, Depth + 1); |
0 |
| 6848 |
RangeFromOps = RangeFromOps.unionWith(OpRange); |
0 |
6848 |
RangeFromOps = RangeFromOps.unionWith(OpRange); |
0 |
| 6849 |
// No point to continue if we already have a full set. |
--- |
6849 |
// No point to continue if we already have a full set. |
--- |
| 6850 |
if (RangeFromOps.isFullSet()) |
0 |
6850 |
if (RangeFromOps.isFullSet()) |
0 |
| 6851 |
break; |
0 |
6851 |
break; |
0 |
| 6852 |
} |
0 |
6852 |
} |
0 |
| 6853 |
ConservativeResult = |
--- |
6853 |
ConservativeResult = |
--- |
| 6854 |
ConservativeResult.intersectWith(RangeFromOps, RangeType); |
0 |
6854 |
ConservativeResult.intersectWith(RangeFromOps, RangeType); |
0 |
| 6855 |
bool Erased = PendingPhiRanges.erase(Phi); |
0 |
6855 |
bool Erased = PendingPhiRanges.erase(Phi); |
0 |
| 6856 |
assert(Erased && "Failed to erase Phi properly?"); |
0 |
6856 |
assert(Erased && "Failed to erase Phi properly?"); |
0 |
| 6857 |
(void)Erased; |
--- |
6857 |
(void)Erased; |
--- |
| 6858 |
} |
0 |
6858 |
} |
0 |
| 6859 |
} |
--- |
6859 |
} |
--- |
| 6860 |
|
--- |
6860 |
|
--- |
| 6861 |
// vscale can't be equal to zero |
--- |
6861 |
// vscale can't be equal to zero |
--- |
| 6862 |
if (const auto *II = dyn_cast(V)) |
0 |
6862 |
if (const auto *II = dyn_cast(V)) |
0 |
| 6863 |
if (II->getIntrinsicID() == Intrinsic::vscale) { |
0 |
6863 |
if (II->getIntrinsicID() == Intrinsic::vscale) { |
0 |
| 6864 |
ConstantRange Disallowed = APInt::getZero(BitWidth); |
0 |
6864 |
ConstantRange Disallowed = APInt::getZero(BitWidth); |
0 |
| 6865 |
ConservativeResult = ConservativeResult.difference(Disallowed); |
0 |
6865 |
ConservativeResult = ConservativeResult.difference(Disallowed); |
0 |
| 6866 |
} |
0 |
6866 |
} |
0 |
| 6867 |
|
--- |
6867 |
|
--- |
| 6868 |
return setRange(U, SignHint, std::move(ConservativeResult)); |
0 |
6868 |
return setRange(U, SignHint, std::move(ConservativeResult)); |
0 |
| 6869 |
} |
0 |
6869 |
} |
0 |
| 6870 |
case scCouldNotCompute: |
0 |
6870 |
case scCouldNotCompute: |
0 |
| 6871 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
6871 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
| 6872 |
} |
--- |
6872 |
} |
--- |
| 6873 |
|
--- |
6873 |
|
--- |
| 6874 |
return setRange(S, SignHint, std::move(ConservativeResult)); |
0 |
6874 |
return setRange(S, SignHint, std::move(ConservativeResult)); |
0 |
| 6875 |
} |
0 |
6875 |
} |
0 |
| 6876 |
|
--- |
6876 |
|
--- |
| 6877 |
// Given a StartRange, Step and MaxBECount for an expression compute a range of |
--- |
6877 |
// Given a StartRange, Step and MaxBECount for an expression compute a range of |
--- |
| 6878 |
// values that the expression can take. Initially, the expression has a value |
--- |
6878 |
// values that the expression can take. Initially, the expression has a value |
--- |
| 6879 |
// from StartRange and then is changed by Step up to MaxBECount times. Signed |
--- |
6879 |
// from StartRange and then is changed by Step up to MaxBECount times. Signed |
--- |
| 6880 |
// argument defines if we treat Step as signed or unsigned. |
--- |
6880 |
// argument defines if we treat Step as signed or unsigned. |
--- |
| 6881 |
static ConstantRange getRangeForAffineARHelper(APInt Step, |
0 |
6881 |
static ConstantRange getRangeForAffineARHelper(APInt Step, |
0 |
| 6882 |
const ConstantRange &StartRange, |
--- |
6882 |
const ConstantRange &StartRange, |
--- |
| 6883 |
const APInt &MaxBECount, |
--- |
6883 |
const APInt &MaxBECount, |
--- |
| 6884 |
bool Signed) { |
--- |
6884 |
bool Signed) { |
--- |
| 6885 |
unsigned BitWidth = Step.getBitWidth(); |
0 |
6885 |
unsigned BitWidth = Step.getBitWidth(); |
0 |
| 6886 |
assert(BitWidth == StartRange.getBitWidth() && |
0 |
6886 |
assert(BitWidth == StartRange.getBitWidth() && |
0 |
| 6887 |
BitWidth == MaxBECount.getBitWidth() && "mismatched bit widths"); |
--- |
6887 |
BitWidth == MaxBECount.getBitWidth() && "mismatched bit widths"); |
--- |
| 6888 |
// If either Step or MaxBECount is 0, then the expression won't change, and we |
--- |
6888 |
// If either Step or MaxBECount is 0, then the expression won't change, and we |
--- |
| 6889 |
// just need to return the initial range. |
--- |
6889 |
// just need to return the initial range. |
--- |
| 6890 |
if (Step == 0 || MaxBECount == 0) |
0 |
6890 |
if (Step == 0 || MaxBECount == 0) |
0 |
| 6891 |
return StartRange; |
0 |
6891 |
return StartRange; |
0 |
| 6892 |
|
--- |
6892 |
|
--- |
| 6893 |
// If we don't know anything about the initial value (i.e. StartRange is |
--- |
6893 |
// If we don't know anything about the initial value (i.e. StartRange is |
--- |
| 6894 |
// FullRange), then we don't know anything about the final range either. |
--- |
6894 |
// FullRange), then we don't know anything about the final range either. |
--- |
| 6895 |
// Return FullRange. |
--- |
6895 |
// Return FullRange. |
--- |
| 6896 |
if (StartRange.isFullSet()) |
0 |
6896 |
if (StartRange.isFullSet()) |
0 |
| 6897 |
return ConstantRange::getFull(BitWidth); |
0 |
6897 |
return ConstantRange::getFull(BitWidth); |
0 |
| 6898 |
|
--- |
6898 |
|
--- |
| 6899 |
// If Step is signed and negative, then we use its absolute value, but we also |
--- |
6899 |
// If Step is signed and negative, then we use its absolute value, but we also |
--- |
| 6900 |
// note that we're moving in the opposite direction. |
--- |
6900 |
// note that we're moving in the opposite direction. |
--- |
| 6901 |
bool Descending = Signed && Step.isNegative(); |
0 |
6901 |
bool Descending = Signed && Step.isNegative(); |
0 |
| 6902 |
|
--- |
6902 |
|
--- |
| 6903 |
if (Signed) |
0 |
6903 |
if (Signed) |
0 |
| 6904 |
// This is correct even for INT_SMIN. Let's look at i8 to illustrate this: |
--- |
6904 |
// This is correct even for INT_SMIN. Let's look at i8 to illustrate this: |
--- |
| 6905 |
// abs(INT_SMIN) = abs(-128) = abs(0x80) = -0x80 = 0x80 = 128. |
--- |
6905 |
// abs(INT_SMIN) = abs(-128) = abs(0x80) = -0x80 = 0x80 = 128. |
--- |
| 6906 |
// This equations hold true due to the well-defined wrap-around behavior of |
--- |
6906 |
// This equations hold true due to the well-defined wrap-around behavior of |
--- |
| 6907 |
// APInt. |
--- |
6907 |
// APInt. |
--- |
| 6908 |
Step = Step.abs(); |
0 |
6908 |
Step = Step.abs(); |
0 |
| 6909 |
|
--- |
6909 |
|
--- |
| 6910 |
// Check if Offset is more than full span of BitWidth. If it is, the |
--- |
6910 |
// Check if Offset is more than full span of BitWidth. If it is, the |
--- |
| 6911 |
// expression is guaranteed to overflow. |
--- |
6911 |
// expression is guaranteed to overflow. |
--- |
| 6912 |
if (APInt::getMaxValue(StartRange.getBitWidth()).udiv(Step).ult(MaxBECount)) |
0 |
6912 |
if (APInt::getMaxValue(StartRange.getBitWidth()).udiv(Step).ult(MaxBECount)) |
0 |
| 6913 |
return ConstantRange::getFull(BitWidth); |
0 |
6913 |
return ConstantRange::getFull(BitWidth); |
0 |
| 6914 |
|
--- |
6914 |
|
--- |
| 6915 |
// Offset is by how much the expression can change. Checks above guarantee no |
--- |
6915 |
// Offset is by how much the expression can change. Checks above guarantee no |
--- |
| 6916 |
// overflow here. |
--- |
6916 |
// overflow here. |
--- |
| 6917 |
APInt Offset = Step * MaxBECount; |
0 |
6917 |
APInt Offset = Step * MaxBECount; |
0 |
| 6918 |
|
--- |
6918 |
|
--- |
| 6919 |
// Minimum value of the final range will match the minimal value of StartRange |
--- |
6919 |
// Minimum value of the final range will match the minimal value of StartRange |
--- |
| 6920 |
// if the expression is increasing and will be decreased by Offset otherwise. |
--- |
6920 |
// if the expression is increasing and will be decreased by Offset otherwise. |
--- |
| 6921 |
// Maximum value of the final range will match the maximal value of StartRange |
--- |
6921 |
// Maximum value of the final range will match the maximal value of StartRange |
--- |
| 6922 |
// if the expression is decreasing and will be increased by Offset otherwise. |
--- |
6922 |
// if the expression is decreasing and will be increased by Offset otherwise. |
--- |
| 6923 |
APInt StartLower = StartRange.getLower(); |
0 |
6923 |
APInt StartLower = StartRange.getLower(); |
0 |
| 6924 |
APInt StartUpper = StartRange.getUpper() - 1; |
0 |
6924 |
APInt StartUpper = StartRange.getUpper() - 1; |
0 |
| 6925 |
APInt MovedBoundary = Descending ? (StartLower - std::move(Offset)) |
0 |
6925 |
APInt MovedBoundary = Descending ? (StartLower - std::move(Offset)) |
0 |
| 6926 |
: (StartUpper + std::move(Offset)); |
0 |
6926 |
: (StartUpper + std::move(Offset)); |
0 |
| 6927 |
|
--- |
6927 |
|
--- |
| 6928 |
// It's possible that the new minimum/maximum value will fall into the initial |
--- |
6928 |
// It's possible that the new minimum/maximum value will fall into the initial |
--- |
| 6929 |
// range (due to wrap around). This means that the expression can take any |
--- |
6929 |
// range (due to wrap around). This means that the expression can take any |
--- |
| 6930 |
// value in this bitwidth, and we have to return full range. |
--- |
6930 |
// value in this bitwidth, and we have to return full range. |
--- |
| 6931 |
if (StartRange.contains(MovedBoundary)) |
0 |
6931 |
if (StartRange.contains(MovedBoundary)) |
0 |
| 6932 |
return ConstantRange::getFull(BitWidth); |
0 |
6932 |
return ConstantRange::getFull(BitWidth); |
0 |
| 6933 |
|
--- |
6933 |
|
--- |
| 6934 |
APInt NewLower = |
--- |
6934 |
APInt NewLower = |
--- |
| 6935 |
Descending ? std::move(MovedBoundary) : std::move(StartLower); |
0 |
6935 |
Descending ? std::move(MovedBoundary) : std::move(StartLower); |
0 |
| 6936 |
APInt NewUpper = |
--- |
6936 |
APInt NewUpper = |
--- |
| 6937 |
Descending ? std::move(StartUpper) : std::move(MovedBoundary); |
0 |
6937 |
Descending ? std::move(StartUpper) : std::move(MovedBoundary); |
0 |
| 6938 |
NewUpper += 1; |
0 |
6938 |
NewUpper += 1; |
0 |
| 6939 |
|
--- |
6939 |
|
--- |
| 6940 |
// No overflow detected, return [StartLower, StartUpper + Offset + 1) range. |
--- |
6940 |
// No overflow detected, return [StartLower, StartUpper + Offset + 1) range. |
--- |
| 6941 |
return ConstantRange::getNonEmpty(std::move(NewLower), std::move(NewUpper)); |
0 |
6941 |
return ConstantRange::getNonEmpty(std::move(NewLower), std::move(NewUpper)); |
0 |
| 6942 |
} |
0 |
6942 |
} |
0 |
| 6943 |
|
--- |
6943 |
|
--- |
| 6944 |
ConstantRange ScalarEvolution::getRangeForAffineAR(const SCEV *Start, |
0 |
6944 |
ConstantRange ScalarEvolution::getRangeForAffineAR(const SCEV *Start, |
0 |
| 6945 |
const SCEV *Step, |
--- |
6945 |
const SCEV *Step, |
--- |
| 6946 |
const APInt &MaxBECount) { |
--- |
6946 |
const APInt &MaxBECount) { |
--- |
| 6947 |
assert(getTypeSizeInBits(Start->getType()) == |
0 |
6947 |
assert(getTypeSizeInBits(Start->getType()) == |
0 |
| 6948 |
getTypeSizeInBits(Step->getType()) && |
--- |
6948 |
getTypeSizeInBits(Step->getType()) && |
--- |
| 6949 |
getTypeSizeInBits(Start->getType()) == MaxBECount.getBitWidth() && |
--- |
6949 |
getTypeSizeInBits(Start->getType()) == MaxBECount.getBitWidth() && |
--- |
| 6950 |
"mismatched bit widths"); |
--- |
6950 |
"mismatched bit widths"); |
--- |
| 6951 |
|
--- |
6951 |
|
--- |
| 6952 |
// First, consider step signed. |
--- |
6952 |
// First, consider step signed. |
--- |
| 6953 |
ConstantRange StartSRange = getSignedRange(Start); |
0 |
6953 |
ConstantRange StartSRange = getSignedRange(Start); |
0 |
| 6954 |
ConstantRange StepSRange = getSignedRange(Step); |
0 |
6954 |
ConstantRange StepSRange = getSignedRange(Step); |
0 |
| 6955 |
|
--- |
6955 |
|
--- |
| 6956 |
// If Step can be both positive and negative, we need to find ranges for the |
--- |
6956 |
// If Step can be both positive and negative, we need to find ranges for the |
--- |
| 6957 |
// maximum absolute step values in both directions and union them. |
--- |
6957 |
// maximum absolute step values in both directions and union them. |
--- |
| 6958 |
ConstantRange SR = getRangeForAffineARHelper( |
--- |
6958 |
ConstantRange SR = getRangeForAffineARHelper( |
--- |
| 6959 |
StepSRange.getSignedMin(), StartSRange, MaxBECount, /* Signed = */ true); |
0 |
6959 |
StepSRange.getSignedMin(), StartSRange, MaxBECount, /* Signed = */ true); |
0 |
| 6960 |
SR = SR.unionWith(getRangeForAffineARHelper(StepSRange.getSignedMax(), |
0 |
6960 |
SR = SR.unionWith(getRangeForAffineARHelper(StepSRange.getSignedMax(), |
0 |
| 6961 |
StartSRange, MaxBECount, |
--- |
6961 |
StartSRange, MaxBECount, |
--- |
| 6962 |
/* Signed = */ true)); |
0 |
6962 |
/* Signed = */ true)); |
0 |
| 6963 |
|
--- |
6963 |
|
--- |
| 6964 |
// Next, consider step unsigned. |
--- |
6964 |
// Next, consider step unsigned. |
--- |
| 6965 |
ConstantRange UR = getRangeForAffineARHelper( |
--- |
6965 |
ConstantRange UR = getRangeForAffineARHelper( |
--- |
| 6966 |
getUnsignedRangeMax(Step), getUnsignedRange(Start), MaxBECount, |
0 |
6966 |
getUnsignedRangeMax(Step), getUnsignedRange(Start), MaxBECount, |
0 |
| 6967 |
/* Signed = */ false); |
0 |
6967 |
/* Signed = */ false); |
0 |
| 6968 |
|
--- |
6968 |
|
--- |
| 6969 |
// Finally, intersect signed and unsigned ranges. |
--- |
6969 |
// Finally, intersect signed and unsigned ranges. |
--- |
| 6970 |
return SR.intersectWith(UR, ConstantRange::Smallest); |
0 |
6970 |
return SR.intersectWith(UR, ConstantRange::Smallest); |
0 |
| 6971 |
} |
0 |
6971 |
} |
0 |
| 6972 |
|
--- |
6972 |
|
--- |
| 6973 |
ConstantRange ScalarEvolution::getRangeForAffineNoSelfWrappingAR( |
0 |
6973 |
ConstantRange ScalarEvolution::getRangeForAffineNoSelfWrappingAR( |
0 |
| 6974 |
const SCEVAddRecExpr *AddRec, const SCEV *MaxBECount, unsigned BitWidth, |
--- |
6974 |
const SCEVAddRecExpr *AddRec, const SCEV *MaxBECount, unsigned BitWidth, |
--- |
| 6975 |
ScalarEvolution::RangeSignHint SignHint) { |
--- |
6975 |
ScalarEvolution::RangeSignHint SignHint) { |
--- |
| 6976 |
assert(AddRec->isAffine() && "Non-affine AddRecs are not suppored!\n"); |
0 |
6976 |
assert(AddRec->isAffine() && "Non-affine AddRecs are not suppored!\n"); |
0 |
| 6977 |
assert(AddRec->hasNoSelfWrap() && |
0 |
6977 |
assert(AddRec->hasNoSelfWrap() && |
0 |
| 6978 |
"This only works for non-self-wrapping AddRecs!"); |
--- |
6978 |
"This only works for non-self-wrapping AddRecs!"); |
--- |
| 6979 |
const bool IsSigned = SignHint == HINT_RANGE_SIGNED; |
0 |
6979 |
const bool IsSigned = SignHint == HINT_RANGE_SIGNED; |
0 |
| 6980 |
const SCEV *Step = AddRec->getStepRecurrence(*this); |
0 |
6980 |
const SCEV *Step = AddRec->getStepRecurrence(*this); |
0 |
| 6981 |
// Only deal with constant step to save compile time. |
--- |
6981 |
// Only deal with constant step to save compile time. |
--- |
| 6982 |
if (!isa(Step)) |
0 |
6982 |
if (!isa(Step)) |
0 |
| 6983 |
return ConstantRange::getFull(BitWidth); |
0 |
6983 |
return ConstantRange::getFull(BitWidth); |
0 |
| 6984 |
// Let's make sure that we can prove that we do not self-wrap during |
--- |
6984 |
// Let's make sure that we can prove that we do not self-wrap during |
--- |
| 6985 |
// MaxBECount iterations. We need this because MaxBECount is a maximum |
--- |
6985 |
// MaxBECount iterations. We need this because MaxBECount is a maximum |
--- |
| 6986 |
// iteration count estimate, and we might infer nw from some exit for which we |
--- |
6986 |
// iteration count estimate, and we might infer nw from some exit for which we |
--- |
| 6987 |
// do not know max exit count (or any other side reasoning). |
--- |
6987 |
// do not know max exit count (or any other side reasoning). |
--- |
| 6988 |
// TODO: Turn into assert at some point. |
--- |
6988 |
// TODO: Turn into assert at some point. |
--- |
| 6989 |
if (getTypeSizeInBits(MaxBECount->getType()) > |
0 |
6989 |
if (getTypeSizeInBits(MaxBECount->getType()) > |
0 |
| 6990 |
getTypeSizeInBits(AddRec->getType())) |
0 |
6990 |
getTypeSizeInBits(AddRec->getType())) |
0 |
| 6991 |
return ConstantRange::getFull(BitWidth); |
0 |
6991 |
return ConstantRange::getFull(BitWidth); |
0 |
| 6992 |
MaxBECount = getNoopOrZeroExtend(MaxBECount, AddRec->getType()); |
0 |
6992 |
MaxBECount = getNoopOrZeroExtend(MaxBECount, AddRec->getType()); |
0 |
| 6993 |
const SCEV *RangeWidth = getMinusOne(AddRec->getType()); |
0 |
6993 |
const SCEV *RangeWidth = getMinusOne(AddRec->getType()); |
0 |
| 6994 |
const SCEV *StepAbs = getUMinExpr(Step, getNegativeSCEV(Step)); |
0 |
6994 |
const SCEV *StepAbs = getUMinExpr(Step, getNegativeSCEV(Step)); |
0 |
| 6995 |
const SCEV *MaxItersWithoutWrap = getUDivExpr(RangeWidth, StepAbs); |
0 |
6995 |
const SCEV *MaxItersWithoutWrap = getUDivExpr(RangeWidth, StepAbs); |
0 |
| 6996 |
if (!isKnownPredicateViaConstantRanges(ICmpInst::ICMP_ULE, MaxBECount, |
0 |
6996 |
if (!isKnownPredicateViaConstantRanges(ICmpInst::ICMP_ULE, MaxBECount, |
0 |
| 6997 |
MaxItersWithoutWrap)) |
--- |
6997 |
MaxItersWithoutWrap)) |
--- |
| 6998 |
return ConstantRange::getFull(BitWidth); |
0 |
6998 |
return ConstantRange::getFull(BitWidth); |
0 |
| 6999 |
|
--- |
6999 |
|
--- |
| 7000 |
ICmpInst::Predicate LEPred = |
0 |
7000 |
ICmpInst::Predicate LEPred = |
0 |
| 7001 |
IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; |
0 |
7001 |
IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; |
0 |
| 7002 |
ICmpInst::Predicate GEPred = |
0 |
7002 |
ICmpInst::Predicate GEPred = |
0 |
| 7003 |
IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; |
0 |
7003 |
IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; |
0 |
| 7004 |
const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this); |
0 |
7004 |
const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this); |
0 |
| 7005 |
|
--- |
7005 |
|
--- |
| 7006 |
// We know that there is no self-wrap. Let's take Start and End values and |
--- |
7006 |
// We know that there is no self-wrap. Let's take Start and End values and |
--- |
| 7007 |
// look at all intermediate values V1, V2, ..., Vn that IndVar takes during |
--- |
7007 |
// look at all intermediate values V1, V2, ..., Vn that IndVar takes during |
--- |
| 7008 |
// the iteration. They either lie inside the range [Min(Start, End), |
--- |
7008 |
// the iteration. They either lie inside the range [Min(Start, End), |
--- |
| 7009 |
// Max(Start, End)] or outside it: |
--- |
7009 |
// Max(Start, End)] or outside it: |
--- |
| 7010 |
// |
--- |
7010 |
// |
--- |
| 7011 |
// Case 1: RangeMin ... Start V1 ... VN End ... RangeMax; |
--- |
7011 |
// Case 1: RangeMin ... Start V1 ... VN End ... RangeMax; |
--- |
| 7012 |
// Case 2: RangeMin Vk ... V1 Start ... End Vn ... Vk + 1 RangeMax; |
--- |
7012 |
// Case 2: RangeMin Vk ... V1 Start ... End Vn ... Vk + 1 RangeMax; |
--- |
| 7013 |
// |
--- |
7013 |
// |
--- |
| 7014 |
// No self wrap flag guarantees that the intermediate values cannot be BOTH |
--- |
7014 |
// No self wrap flag guarantees that the intermediate values cannot be BOTH |
--- |
| 7015 |
// outside and inside the range [Min(Start, End), Max(Start, End)]. Using that |
--- |
7015 |
// outside and inside the range [Min(Start, End), Max(Start, End)]. Using that |
--- |
| 7016 |
// knowledge, let's try to prove that we are dealing with Case 1. It is so if |
--- |
7016 |
// knowledge, let's try to prove that we are dealing with Case 1. It is so if |
--- |
| 7017 |
// Start <= End and step is positive, or Start >= End and step is negative. |
--- |
7017 |
// Start <= End and step is positive, or Start >= End and step is negative. |
--- |
| 7018 |
const SCEV *Start = applyLoopGuards(AddRec->getStart(), AddRec->getLoop()); |
0 |
7018 |
const SCEV *Start = applyLoopGuards(AddRec->getStart(), AddRec->getLoop()); |
0 |
| 7019 |
ConstantRange StartRange = getRangeRef(Start, SignHint); |
0 |
7019 |
ConstantRange StartRange = getRangeRef(Start, SignHint); |
0 |
| 7020 |
ConstantRange EndRange = getRangeRef(End, SignHint); |
0 |
7020 |
ConstantRange EndRange = getRangeRef(End, SignHint); |
0 |
| 7021 |
ConstantRange RangeBetween = StartRange.unionWith(EndRange); |
0 |
7021 |
ConstantRange RangeBetween = StartRange.unionWith(EndRange); |
0 |
| 7022 |
// If they already cover full iteration space, we will know nothing useful |
--- |
7022 |
// If they already cover full iteration space, we will know nothing useful |
--- |
| 7023 |
// even if we prove what we want to prove. |
--- |
7023 |
// even if we prove what we want to prove. |
--- |
| 7024 |
if (RangeBetween.isFullSet()) |
0 |
7024 |
if (RangeBetween.isFullSet()) |
0 |
| 7025 |
return RangeBetween; |
0 |
7025 |
return RangeBetween; |
0 |
| 7026 |
// Only deal with ranges that do not wrap (i.e. RangeMin < RangeMax). |
--- |
7026 |
// Only deal with ranges that do not wrap (i.e. RangeMin < RangeMax). |
--- |
| 7027 |
bool IsWrappedSet = IsSigned ? RangeBetween.isSignWrappedSet() |
0 |
7027 |
bool IsWrappedSet = IsSigned ? RangeBetween.isSignWrappedSet() |
0 |
| 7028 |
: RangeBetween.isWrappedSet(); |
0 |
7028 |
: RangeBetween.isWrappedSet(); |
0 |
| 7029 |
if (IsWrappedSet) |
0 |
7029 |
if (IsWrappedSet) |
0 |
| 7030 |
return ConstantRange::getFull(BitWidth); |
0 |
7030 |
return ConstantRange::getFull(BitWidth); |
0 |
| 7031 |
|
--- |
7031 |
|
--- |
| 7032 |
if (isKnownPositive(Step) && |
0 |
7032 |
if (isKnownPositive(Step) && |
0 |
| 7033 |
isKnownPredicateViaConstantRanges(LEPred, Start, End)) |
0 |
7033 |
isKnownPredicateViaConstantRanges(LEPred, Start, End)) |
0 |
| 7034 |
return RangeBetween; |
0 |
7034 |
return RangeBetween; |
0 |
| 7035 |
if (isKnownNegative(Step) && |
0 |
7035 |
if (isKnownNegative(Step) && |
0 |
| 7036 |
isKnownPredicateViaConstantRanges(GEPred, Start, End)) |
0 |
7036 |
isKnownPredicateViaConstantRanges(GEPred, Start, End)) |
0 |
| 7037 |
return RangeBetween; |
0 |
7037 |
return RangeBetween; |
0 |
| 7038 |
return ConstantRange::getFull(BitWidth); |
0 |
7038 |
return ConstantRange::getFull(BitWidth); |
0 |
| 7039 |
} |
0 |
7039 |
} |
0 |
| 7040 |
|
--- |
7040 |
|
--- |
| 7041 |
ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start, |
0 |
7041 |
ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start, |
0 |
| 7042 |
const SCEV *Step, |
--- |
7042 |
const SCEV *Step, |
--- |
| 7043 |
const APInt &MaxBECount) { |
--- |
7043 |
const APInt &MaxBECount) { |
--- |
| 7044 |
// RangeOf({C?A:B,+,C?P:Q}) == RangeOf(C?{A,+,P}:{B,+,Q}) |
--- |
7044 |
// RangeOf({C?A:B,+,C?P:Q}) == RangeOf(C?{A,+,P}:{B,+,Q}) |
--- |
| 7045 |
// == RangeOf({A,+,P}) union RangeOf({B,+,Q}) |
--- |
7045 |
// == RangeOf({A,+,P}) union RangeOf({B,+,Q}) |
--- |
| 7046 |
|
--- |
7046 |
|
--- |
| 7047 |
unsigned BitWidth = MaxBECount.getBitWidth(); |
0 |
7047 |
unsigned BitWidth = MaxBECount.getBitWidth(); |
0 |
| 7048 |
assert(getTypeSizeInBits(Start->getType()) == BitWidth && |
0 |
7048 |
assert(getTypeSizeInBits(Start->getType()) == BitWidth && |
0 |
| 7049 |
getTypeSizeInBits(Step->getType()) == BitWidth && |
--- |
7049 |
getTypeSizeInBits(Step->getType()) == BitWidth && |
--- |
| 7050 |
"mismatched bit widths"); |
--- |
7050 |
"mismatched bit widths"); |
--- |
| 7051 |
|
--- |
7051 |
|
--- |
| 7052 |
struct SelectPattern { |
--- |
7052 |
struct SelectPattern { |
--- |
| 7053 |
Value *Condition = nullptr; |
--- |
7053 |
Value *Condition = nullptr; |
--- |
| 7054 |
APInt TrueValue; |
--- |
7054 |
APInt TrueValue; |
--- |
| 7055 |
APInt FalseValue; |
--- |
7055 |
APInt FalseValue; |
--- |
| 7056 |
|
--- |
7056 |
|
--- |
| 7057 |
explicit SelectPattern(ScalarEvolution &SE, unsigned BitWidth, |
0 |
7057 |
explicit SelectPattern(ScalarEvolution &SE, unsigned BitWidth, |
0 |
| 7058 |
const SCEV *S) { |
0 |
7058 |
const SCEV *S) { |
0 |
| 7059 |
std::optional CastOp; |
0 |
7059 |
std::optional CastOp; |
0 |
| 7060 |
APInt Offset(BitWidth, 0); |
0 |
7060 |
APInt Offset(BitWidth, 0); |
0 |
| 7061 |
|
--- |
7061 |
|
--- |
| 7062 |
assert(SE.getTypeSizeInBits(S->getType()) == BitWidth && |
0 |
7062 |
assert(SE.getTypeSizeInBits(S->getType()) == BitWidth && |
0 |
| 7063 |
"Should be!"); |
--- |
7063 |
"Should be!"); |
--- |
| 7064 |
|
--- |
7064 |
|
--- |
| 7065 |
// Peel off a constant offset: |
--- |
7065 |
// Peel off a constant offset: |
--- |
| 7066 |
if (auto *SA = dyn_cast(S)) { |
0 |
7066 |
if (auto *SA = dyn_cast(S)) { |
0 |
| 7067 |
// In the future we could consider being smarter here and handle |
--- |
7067 |
// In the future we could consider being smarter here and handle |
--- |
| 7068 |
// {Start+Step,+,Step} too. |
--- |
7068 |
// {Start+Step,+,Step} too. |
--- |
| 7069 |
if (SA->getNumOperands() != 2 || !isa(SA->getOperand(0))) |
0 |
7069 |
if (SA->getNumOperands() != 2 || !isa(SA->getOperand(0))) |
0 |
| 7070 |
return; |
0 |
7070 |
return; |
0 |
| 7071 |
|
--- |
7071 |
|
--- |
| 7072 |
Offset = cast(SA->getOperand(0))->getAPInt(); |
0 |
7072 |
Offset = cast(SA->getOperand(0))->getAPInt(); |
0 |
| 7073 |
S = SA->getOperand(1); |
0 |
7073 |
S = SA->getOperand(1); |
0 |
| 7074 |
} |
--- |
7074 |
} |
--- |
| 7075 |
|
--- |
7075 |
|
--- |
| 7076 |
// Peel off a cast operation |
--- |
7076 |
// Peel off a cast operation |
--- |
| 7077 |
if (auto *SCast = dyn_cast(S)) { |
0 |
7077 |
if (auto *SCast = dyn_cast(S)) { |
0 |
| 7078 |
CastOp = SCast->getSCEVType(); |
0 |
7078 |
CastOp = SCast->getSCEVType(); |
0 |
| 7079 |
S = SCast->getOperand(); |
0 |
7079 |
S = SCast->getOperand(); |
0 |
| 7080 |
} |
--- |
7080 |
} |
--- |
| 7081 |
|
--- |
7081 |
|
--- |
| 7082 |
using namespace llvm::PatternMatch; |
--- |
7082 |
using namespace llvm::PatternMatch; |
--- |
| 7083 |
|
--- |
7083 |
|
--- |
| 7084 |
auto *SU = dyn_cast(S); |
0 |
7084 |
auto *SU = dyn_cast(S); |
0 |
| 7085 |
const APInt *TrueVal, *FalseVal; |
--- |
7085 |
const APInt *TrueVal, *FalseVal; |
--- |
| 7086 |
if (!SU || |
0 |
7086 |
if (!SU || |
0 |
| 7087 |
!match(SU->getValue(), m_Select(m_Value(Condition), m_APInt(TrueVal), |
0 |
7087 |
!match(SU->getValue(), m_Select(m_Value(Condition), m_APInt(TrueVal), |
0 |
| 7088 |
m_APInt(FalseVal)))) { |
0 |
7088 |
m_APInt(FalseVal)))) { |
0 |
| 7089 |
Condition = nullptr; |
0 |
7089 |
Condition = nullptr; |
0 |
| 7090 |
return; |
0 |
7090 |
return; |
0 |
| 7091 |
} |
--- |
7091 |
} |
--- |
| 7092 |
|
--- |
7092 |
|
--- |
| 7093 |
TrueValue = *TrueVal; |
0 |
7093 |
TrueValue = *TrueVal; |
0 |
| 7094 |
FalseValue = *FalseVal; |
0 |
7094 |
FalseValue = *FalseVal; |
0 |
| 7095 |
|
--- |
7095 |
|
--- |
| 7096 |
// Re-apply the cast we peeled off earlier |
--- |
7096 |
// Re-apply the cast we peeled off earlier |
--- |
| 7097 |
if (CastOp) |
0 |
7097 |
if (CastOp) |
0 |
| 7098 |
switch (*CastOp) { |
0 |
7098 |
switch (*CastOp) { |
0 |
| 7099 |
default: |
0 |
7099 |
default: |
0 |
| 7100 |
llvm_unreachable("Unknown SCEV cast type!"); |
0 |
7100 |
llvm_unreachable("Unknown SCEV cast type!"); |
0 |
| 7101 |
|
--- |
7101 |
|
--- |
| 7102 |
case scTruncate: |
0 |
7102 |
case scTruncate: |
0 |
| 7103 |
TrueValue = TrueValue.trunc(BitWidth); |
0 |
7103 |
TrueValue = TrueValue.trunc(BitWidth); |
0 |
| 7104 |
FalseValue = FalseValue.trunc(BitWidth); |
0 |
7104 |
FalseValue = FalseValue.trunc(BitWidth); |
0 |
| 7105 |
break; |
0 |
7105 |
break; |
0 |
| 7106 |
case scZeroExtend: |
0 |
7106 |
case scZeroExtend: |
0 |
| 7107 |
TrueValue = TrueValue.zext(BitWidth); |
0 |
7107 |
TrueValue = TrueValue.zext(BitWidth); |
0 |
| 7108 |
FalseValue = FalseValue.zext(BitWidth); |
0 |
7108 |
FalseValue = FalseValue.zext(BitWidth); |
0 |
| 7109 |
break; |
0 |
7109 |
break; |
0 |
| 7110 |
case scSignExtend: |
0 |
7110 |
case scSignExtend: |
0 |
| 7111 |
TrueValue = TrueValue.sext(BitWidth); |
0 |
7111 |
TrueValue = TrueValue.sext(BitWidth); |
0 |
| 7112 |
FalseValue = FalseValue.sext(BitWidth); |
0 |
7112 |
FalseValue = FalseValue.sext(BitWidth); |
0 |
| 7113 |
break; |
0 |
7113 |
break; |
0 |
| 7114 |
} |
--- |
7114 |
} |
--- |
| 7115 |
|
--- |
7115 |
|
--- |
| 7116 |
// Re-apply the constant offset we peeled off earlier |
--- |
7116 |
// Re-apply the constant offset we peeled off earlier |
--- |
| 7117 |
TrueValue += Offset; |
0 |
7117 |
TrueValue += Offset; |
0 |
| 7118 |
FalseValue += Offset; |
0 |
7118 |
FalseValue += Offset; |
0 |
| 7119 |
} |
0 |
7119 |
} |
0 |
| 7120 |
|
--- |
7120 |
|
--- |
| 7121 |
bool isRecognized() { return Condition != nullptr; } |
0 |
7121 |
bool isRecognized() { return Condition != nullptr; } |
0 |
| 7122 |
}; |
--- |
7122 |
}; |
--- |
| 7123 |
|
--- |
7123 |
|
--- |
| 7124 |
SelectPattern StartPattern(*this, BitWidth, Start); |
0 |
7124 |
SelectPattern StartPattern(*this, BitWidth, Start); |
0 |
| 7125 |
if (!StartPattern.isRecognized()) |
0 |
7125 |
if (!StartPattern.isRecognized()) |
0 |
| 7126 |
return ConstantRange::getFull(BitWidth); |
0 |
7126 |
return ConstantRange::getFull(BitWidth); |
0 |
| 7127 |
|
--- |
7127 |
|
--- |
| 7128 |
SelectPattern StepPattern(*this, BitWidth, Step); |
0 |
7128 |
SelectPattern StepPattern(*this, BitWidth, Step); |
0 |
| 7129 |
if (!StepPattern.isRecognized()) |
0 |
7129 |
if (!StepPattern.isRecognized()) |
0 |
| 7130 |
return ConstantRange::getFull(BitWidth); |
0 |
7130 |
return ConstantRange::getFull(BitWidth); |
0 |
| 7131 |
|
--- |
7131 |
|
--- |
| 7132 |
if (StartPattern.Condition != StepPattern.Condition) { |
0 |
7132 |
if (StartPattern.Condition != StepPattern.Condition) { |
0 |
| 7133 |
// We don't handle this case today; but we could, by considering four |
--- |
7133 |
// We don't handle this case today; but we could, by considering four |
--- |
| 7134 |
// possibilities below instead of two. I'm not sure if there are cases where |
--- |
7134 |
// possibilities below instead of two. I'm not sure if there are cases where |
--- |
| 7135 |
// that will help over what getRange already does, though. |
--- |
7135 |
// that will help over what getRange already does, though. |
--- |
| 7136 |
return ConstantRange::getFull(BitWidth); |
0 |
7136 |
return ConstantRange::getFull(BitWidth); |
0 |
| 7137 |
} |
--- |
7137 |
} |
--- |
| 7138 |
|
--- |
7138 |
|
--- |
| 7139 |
// NB! Calling ScalarEvolution::getConstant is fine, but we should not try to |
--- |
7139 |
// NB! Calling ScalarEvolution::getConstant is fine, but we should not try to |
--- |
| 7140 |
// construct arbitrary general SCEV expressions here. This function is called |
--- |
7140 |
// construct arbitrary general SCEV expressions here. This function is called |
--- |
| 7141 |
// from deep in the call stack, and calling getSCEV (on a sext instruction, |
--- |
7141 |
// from deep in the call stack, and calling getSCEV (on a sext instruction, |
--- |
| 7142 |
// say) can end up caching a suboptimal value. |
--- |
7142 |
// say) can end up caching a suboptimal value. |
--- |
| 7143 |
|
--- |
7143 |
|
--- |
| 7144 |
// FIXME: without the explicit `this` receiver below, MSVC errors out with |
--- |
7144 |
// FIXME: without the explicit `this` receiver below, MSVC errors out with |
--- |
| 7145 |
// C2352 and C2512 (otherwise it isn't needed). |
--- |
7145 |
// C2352 and C2512 (otherwise it isn't needed). |
--- |
| 7146 |
|
--- |
7146 |
|
--- |
| 7147 |
const SCEV *TrueStart = this->getConstant(StartPattern.TrueValue); |
0 |
7147 |
const SCEV *TrueStart = this->getConstant(StartPattern.TrueValue); |
0 |
| 7148 |
const SCEV *TrueStep = this->getConstant(StepPattern.TrueValue); |
0 |
7148 |
const SCEV *TrueStep = this->getConstant(StepPattern.TrueValue); |
0 |
| 7149 |
const SCEV *FalseStart = this->getConstant(StartPattern.FalseValue); |
0 |
7149 |
const SCEV *FalseStart = this->getConstant(StartPattern.FalseValue); |
0 |
| 7150 |
const SCEV *FalseStep = this->getConstant(StepPattern.FalseValue); |
0 |
7150 |
const SCEV *FalseStep = this->getConstant(StepPattern.FalseValue); |
0 |
| 7151 |
|
--- |
7151 |
|
--- |
| 7152 |
ConstantRange TrueRange = |
--- |
7152 |
ConstantRange TrueRange = |
--- |
| 7153 |
this->getRangeForAffineAR(TrueStart, TrueStep, MaxBECount); |
0 |
7153 |
this->getRangeForAffineAR(TrueStart, TrueStep, MaxBECount); |
0 |
| 7154 |
ConstantRange FalseRange = |
--- |
7154 |
ConstantRange FalseRange = |
--- |
| 7155 |
this->getRangeForAffineAR(FalseStart, FalseStep, MaxBECount); |
0 |
7155 |
this->getRangeForAffineAR(FalseStart, FalseStep, MaxBECount); |
0 |
| 7156 |
|
--- |
7156 |
|
--- |
| 7157 |
return TrueRange.unionWith(FalseRange); |
0 |
7157 |
return TrueRange.unionWith(FalseRange); |
0 |
| 7158 |
} |
0 |
7158 |
} |
0 |
| 7159 |
|
--- |
7159 |
|
--- |
| 7160 |
SCEV::NoWrapFlags ScalarEvolution::getNoWrapFlagsFromUB(const Value *V) { |
0 |
7160 |
SCEV::NoWrapFlags ScalarEvolution::getNoWrapFlagsFromUB(const Value *V) { |
0 |
| 7161 |
if (isa(V)) return SCEV::FlagAnyWrap; |
0 |
7161 |
if (isa(V)) return SCEV::FlagAnyWrap; |
0 |
| 7162 |
const BinaryOperator *BinOp = cast(V); |
0 |
7162 |
const BinaryOperator *BinOp = cast(V); |
0 |
| 7163 |
|
--- |
7163 |
|
--- |
| 7164 |
// Return early if there are no flags to propagate to the SCEV. |
--- |
7164 |
// Return early if there are no flags to propagate to the SCEV. |
--- |
| 7165 |
SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; |
0 |
7165 |
SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; |
0 |
| 7166 |
if (BinOp->hasNoUnsignedWrap()) |
0 |
7166 |
if (BinOp->hasNoUnsignedWrap()) |
0 |
| 7167 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); |
0 |
7167 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); |
0 |
| 7168 |
if (BinOp->hasNoSignedWrap()) |
0 |
7168 |
if (BinOp->hasNoSignedWrap()) |
0 |
| 7169 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); |
0 |
7169 |
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); |
0 |
| 7170 |
if (Flags == SCEV::FlagAnyWrap) |
0 |
7170 |
if (Flags == SCEV::FlagAnyWrap) |
0 |
| 7171 |
return SCEV::FlagAnyWrap; |
0 |
7171 |
return SCEV::FlagAnyWrap; |
0 |
| 7172 |
|
--- |
7172 |
|
--- |
| 7173 |
return isSCEVExprNeverPoison(BinOp) ? Flags : SCEV::FlagAnyWrap; |
0 |
7173 |
return isSCEVExprNeverPoison(BinOp) ? Flags : SCEV::FlagAnyWrap; |
0 |
| 7174 |
} |
--- |
7174 |
} |
--- |
| 7175 |
|
--- |
7175 |
|
--- |
| 7176 |
const Instruction * |
--- |
7176 |
const Instruction * |
--- |
| 7177 |
ScalarEvolution::getNonTrivialDefiningScopeBound(const SCEV *S) { |
0 |
7177 |
ScalarEvolution::getNonTrivialDefiningScopeBound(const SCEV *S) { |
0 |
| 7178 |
if (auto *AddRec = dyn_cast(S)) |
0 |
7178 |
if (auto *AddRec = dyn_cast(S)) |
0 |
| 7179 |
return &*AddRec->getLoop()->getHeader()->begin(); |
0 |
7179 |
return &*AddRec->getLoop()->getHeader()->begin(); |
0 |
| 7180 |
if (auto *U = dyn_cast(S)) |
0 |
7180 |
if (auto *U = dyn_cast(S)) |
0 |
| 7181 |
if (auto *I = dyn_cast(U->getValue())) |
0 |
7181 |
if (auto *I = dyn_cast(U->getValue())) |
0 |
| 7182 |
return I; |
0 |
7182 |
return I; |
0 |
| 7183 |
return nullptr; |
0 |
7183 |
return nullptr; |
0 |
| 7184 |
} |
--- |
7184 |
} |
--- |
| 7185 |
|
--- |
7185 |
|
--- |
| 7186 |
const Instruction * |
--- |
7186 |
const Instruction * |
--- |
| 7187 |
ScalarEvolution::getDefiningScopeBound(ArrayRef Ops, |
0 |
7187 |
ScalarEvolution::getDefiningScopeBound(ArrayRef Ops, |
0 |
| 7188 |
bool &Precise) { |
--- |
7188 |
bool &Precise) { |
--- |
| 7189 |
Precise = true; |
0 |
7189 |
Precise = true; |
0 |
| 7190 |
// Do a bounded search of the def relation of the requested SCEVs. |
--- |
7190 |
// Do a bounded search of the def relation of the requested SCEVs. |
--- |
| 7191 |
SmallSet Visited; |
0 |
7191 |
SmallSet Visited; |
0 |
| 7192 |
SmallVector Worklist; |
0 |
7192 |
SmallVector Worklist; |
0 |
| 7193 |
auto pushOp = [&](const SCEV *S) { |
0 |
7193 |
auto pushOp = [&](const SCEV *S) { |
0 |
| 7194 |
if (!Visited.insert(S).second) |
0 |
7194 |
if (!Visited.insert(S).second) |
0 |
| 7195 |
return; |
0 |
7195 |
return; |
0 |
| 7196 |
// Threshold of 30 here is arbitrary. |
--- |
7196 |
// Threshold of 30 here is arbitrary. |
--- |
| 7197 |
if (Visited.size() > 30) { |
0 |
7197 |
if (Visited.size() > 30) { |
0 |
| 7198 |
Precise = false; |
0 |
7198 |
Precise = false; |
0 |
| 7199 |
return; |
0 |
7199 |
return; |
0 |
| 7200 |
} |
--- |
7200 |
} |
--- |
| 7201 |
Worklist.push_back(S); |
0 |
7201 |
Worklist.push_back(S); |
0 |
| 7202 |
}; |
0 |
7202 |
}; |
0 |
| 7203 |
|
--- |
7203 |
|
--- |
| 7204 |
for (const auto *S : Ops) |
0 |
7204 |
for (const auto *S : Ops) |
0 |
| 7205 |
pushOp(S); |
0 |
7205 |
pushOp(S); |
0 |
| 7206 |
|
--- |
7206 |
|
--- |
| 7207 |
const Instruction *Bound = nullptr; |
0 |
7207 |
const Instruction *Bound = nullptr; |
0 |
| 7208 |
while (!Worklist.empty()) { |
0 |
7208 |
while (!Worklist.empty()) { |
0 |
| 7209 |
auto *S = Worklist.pop_back_val(); |
0 |
7209 |
auto *S = Worklist.pop_back_val(); |
0 |
| 7210 |
if (auto *DefI = getNonTrivialDefiningScopeBound(S)) { |
0 |
7210 |
if (auto *DefI = getNonTrivialDefiningScopeBound(S)) { |
0 |
| 7211 |
if (!Bound || DT.dominates(Bound, DefI)) |
0 |
7211 |
if (!Bound || DT.dominates(Bound, DefI)) |
0 |
| 7212 |
Bound = DefI; |
0 |
7212 |
Bound = DefI; |
0 |
| 7213 |
} else { |
--- |
7213 |
} else { |
--- |
| 7214 |
for (const auto *Op : S->operands()) |
0 |
7214 |
for (const auto *Op : S->operands()) |
0 |
| 7215 |
pushOp(Op); |
0 |
7215 |
pushOp(Op); |
0 |
| 7216 |
} |
--- |
7216 |
} |
--- |
| 7217 |
} |
--- |
7217 |
} |
--- |
| 7218 |
return Bound ? Bound : &*F.getEntryBlock().begin(); |
0 |
7218 |
return Bound ? Bound : &*F.getEntryBlock().begin(); |
0 |
| 7219 |
} |
0 |
7219 |
} |
0 |
| 7220 |
|
--- |
7220 |
|
--- |
| 7221 |
const Instruction * |
--- |
7221 |
const Instruction * |
--- |
| 7222 |
ScalarEvolution::getDefiningScopeBound(ArrayRef Ops) { |
0 |
7222 |
ScalarEvolution::getDefiningScopeBound(ArrayRef Ops) { |
0 |
| 7223 |
bool Discard; |
--- |
7223 |
bool Discard; |
--- |
| 7224 |
return getDefiningScopeBound(Ops, Discard); |
0 |
7224 |
return getDefiningScopeBound(Ops, Discard); |
0 |
| 7225 |
} |
--- |
7225 |
} |
--- |
| 7226 |
|
--- |
7226 |
|
--- |
| 7227 |
bool ScalarEvolution::isGuaranteedToTransferExecutionTo(const Instruction *A, |
0 |
7227 |
bool ScalarEvolution::isGuaranteedToTransferExecutionTo(const Instruction *A, |
0 |
| 7228 |
const Instruction *B) { |
--- |
7228 |
const Instruction *B) { |
--- |
| 7229 |
if (A->getParent() == B->getParent() && |
0 |
7229 |
if (A->getParent() == B->getParent() && |
0 |
| 7230 |
isGuaranteedToTransferExecutionToSuccessor(A->getIterator(), |
0 |
7230 |
isGuaranteedToTransferExecutionToSuccessor(A->getIterator(), |
0 |
| 7231 |
B->getIterator())) |
--- |
7231 |
B->getIterator())) |
--- |
| 7232 |
return true; |
0 |
7232 |
return true; |
0 |
| 7233 |
|
--- |
7233 |
|
--- |
| 7234 |
auto *BLoop = LI.getLoopFor(B->getParent()); |
0 |
7234 |
auto *BLoop = LI.getLoopFor(B->getParent()); |
0 |
| 7235 |
if (BLoop && BLoop->getHeader() == B->getParent() && |
0 |
7235 |
if (BLoop && BLoop->getHeader() == B->getParent() && |
0 |
| 7236 |
BLoop->getLoopPreheader() == A->getParent() && |
0 |
7236 |
BLoop->getLoopPreheader() == A->getParent() && |
0 |
| 7237 |
isGuaranteedToTransferExecutionToSuccessor(A->getIterator(), |
0 |
7237 |
isGuaranteedToTransferExecutionToSuccessor(A->getIterator(), |
0 |
| 7238 |
A->getParent()->end()) && |
0 |
7238 |
A->getParent()->end()) && |
0 |
| 7239 |
isGuaranteedToTransferExecutionToSuccessor(B->getParent()->begin(), |
0 |
7239 |
isGuaranteedToTransferExecutionToSuccessor(B->getParent()->begin(), |
0 |
| 7240 |
B->getIterator())) |
--- |
7240 |
B->getIterator())) |
--- |
| 7241 |
return true; |
0 |
7241 |
return true; |
0 |
| 7242 |
return false; |
0 |
7242 |
return false; |
0 |
| 7243 |
} |
--- |
7243 |
} |
--- |
| 7244 |
|
--- |
7244 |
|
--- |
| 7245 |
|
--- |
7245 |
|
--- |
| 7246 |
bool ScalarEvolution::isSCEVExprNeverPoison(const Instruction *I) { |
0 |
7246 |
bool ScalarEvolution::isSCEVExprNeverPoison(const Instruction *I) { |
0 |
| 7247 |
// Only proceed if we can prove that I does not yield poison. |
--- |
7247 |
// Only proceed if we can prove that I does not yield poison. |
--- |
| 7248 |
if (!programUndefinedIfPoison(I)) |
0 |
7248 |
if (!programUndefinedIfPoison(I)) |
0 |
| 7249 |
return false; |
0 |
7249 |
return false; |
0 |
| 7250 |
|
--- |
7250 |
|
--- |
| 7251 |
// At this point we know that if I is executed, then it does not wrap |
--- |
7251 |
// At this point we know that if I is executed, then it does not wrap |
--- |
| 7252 |
// according to at least one of NSW or NUW. If I is not executed, then we do |
--- |
7252 |
// according to at least one of NSW or NUW. If I is not executed, then we do |
--- |
| 7253 |
// not know if the calculation that I represents would wrap. Multiple |
--- |
7253 |
// not know if the calculation that I represents would wrap. Multiple |
--- |
| 7254 |
// instructions can map to the same SCEV. If we apply NSW or NUW from I to |
--- |
7254 |
// instructions can map to the same SCEV. If we apply NSW or NUW from I to |
--- |
| 7255 |
// the SCEV, we must guarantee no wrapping for that SCEV also when it is |
--- |
7255 |
// the SCEV, we must guarantee no wrapping for that SCEV also when it is |
--- |
| 7256 |
// derived from other instructions that map to the same SCEV. We cannot make |
--- |
7256 |
// derived from other instructions that map to the same SCEV. We cannot make |
--- |
| 7257 |
// that guarantee for cases where I is not executed. So we need to find a |
--- |
7257 |
// that guarantee for cases where I is not executed. So we need to find a |
--- |
| 7258 |
// upper bound on the defining scope for the SCEV, and prove that I is |
--- |
7258 |
// upper bound on the defining scope for the SCEV, and prove that I is |
--- |
| 7259 |
// executed every time we enter that scope. When the bounding scope is a |
--- |
7259 |
// executed every time we enter that scope. When the bounding scope is a |
--- |
| 7260 |
// loop (the common case), this is equivalent to proving I executes on every |
--- |
7260 |
// loop (the common case), this is equivalent to proving I executes on every |
--- |
| 7261 |
// iteration of that loop. |
--- |
7261 |
// iteration of that loop. |
--- |
| 7262 |
SmallVector SCEVOps; |
0 |
7262 |
SmallVector SCEVOps; |
0 |
| 7263 |
for (const Use &Op : I->operands()) { |
0 |
7263 |
for (const Use &Op : I->operands()) { |
0 |
| 7264 |
// I could be an extractvalue from a call to an overflow intrinsic. |
--- |
7264 |
// I could be an extractvalue from a call to an overflow intrinsic. |
--- |
| 7265 |
// TODO: We can do better here in some cases. |
--- |
7265 |
// TODO: We can do better here in some cases. |
--- |
| 7266 |
if (isSCEVable(Op->getType())) |
0 |
7266 |
if (isSCEVable(Op->getType())) |
0 |
| 7267 |
SCEVOps.push_back(getSCEV(Op)); |
0 |
7267 |
SCEVOps.push_back(getSCEV(Op)); |
0 |
| 7268 |
} |
--- |
7268 |
} |
--- |
| 7269 |
auto *DefI = getDefiningScopeBound(SCEVOps); |
0 |
7269 |
auto *DefI = getDefiningScopeBound(SCEVOps); |
0 |
| 7270 |
return isGuaranteedToTransferExecutionTo(DefI, I); |
0 |
7270 |
return isGuaranteedToTransferExecutionTo(DefI, I); |
0 |
| 7271 |
} |
0 |
7271 |
} |
0 |
| 7272 |
|
--- |
7272 |
|
--- |
| 7273 |
bool ScalarEvolution::isAddRecNeverPoison(const Instruction *I, const Loop *L) { |
0 |
7273 |
bool ScalarEvolution::isAddRecNeverPoison(const Instruction *I, const Loop *L) { |
0 |
| 7274 |
// If we know that \c I can never be poison period, then that's enough. |
--- |
7274 |
// If we know that \c I can never be poison period, then that's enough. |
--- |
| 7275 |
if (isSCEVExprNeverPoison(I)) |
0 |
7275 |
if (isSCEVExprNeverPoison(I)) |
0 |
| 7276 |
return true; |
0 |
7276 |
return true; |
0 |
| 7277 |
|
--- |
7277 |
|
--- |
| 7278 |
// If the loop only has one exit, then we know that, if the loop is entered, |
--- |
7278 |
// If the loop only has one exit, then we know that, if the loop is entered, |
--- |
| 7279 |
// any instruction dominating that exit will be executed. If any such |
--- |
7279 |
// any instruction dominating that exit will be executed. If any such |
--- |
| 7280 |
// instruction would result in UB, the addrec cannot be poison. |
--- |
7280 |
// instruction would result in UB, the addrec cannot be poison. |
--- |
| 7281 |
// |
--- |
7281 |
// |
--- |
| 7282 |
// This is basically the same reasoning as in isSCEVExprNeverPoison(), but |
--- |
7282 |
// This is basically the same reasoning as in isSCEVExprNeverPoison(), but |
--- |
| 7283 |
// also handles uses outside the loop header (they just need to dominate the |
--- |
7283 |
// also handles uses outside the loop header (they just need to dominate the |
--- |
| 7284 |
// single exit). |
--- |
7284 |
// single exit). |
--- |
| 7285 |
|
--- |
7285 |
|
--- |
| 7286 |
auto *ExitingBB = L->getExitingBlock(); |
0 |
7286 |
auto *ExitingBB = L->getExitingBlock(); |
0 |
| 7287 |
if (!ExitingBB || !loopHasNoAbnormalExits(L)) |
0 |
7287 |
if (!ExitingBB || !loopHasNoAbnormalExits(L)) |
0 |
| 7288 |
return false; |
0 |
7288 |
return false; |
0 |
| 7289 |
|
--- |
7289 |
|
--- |
| 7290 |
SmallPtrSet KnownPoison; |
0 |
7290 |
SmallPtrSet KnownPoison; |
0 |
| 7291 |
SmallVector Worklist; |
0 |
7291 |
SmallVector Worklist; |
0 |
| 7292 |
|
--- |
7292 |
|
--- |
| 7293 |
// We start by assuming \c I, the post-inc add recurrence, is poison. Only |
--- |
7293 |
// We start by assuming \c I, the post-inc add recurrence, is poison. Only |
--- |
| 7294 |
// things that are known to be poison under that assumption go on the |
--- |
7294 |
// things that are known to be poison under that assumption go on the |
--- |
| 7295 |
// Worklist. |
--- |
7295 |
// Worklist. |
--- |
| 7296 |
KnownPoison.insert(I); |
0 |
7296 |
KnownPoison.insert(I); |
0 |
| 7297 |
Worklist.push_back(I); |
0 |
7297 |
Worklist.push_back(I); |
0 |
| 7298 |
|
--- |
7298 |
|
--- |
| 7299 |
while (!Worklist.empty()) { |
0 |
7299 |
while (!Worklist.empty()) { |
0 |
| 7300 |
const Instruction *Poison = Worklist.pop_back_val(); |
0 |
7300 |
const Instruction *Poison = Worklist.pop_back_val(); |
0 |
| 7301 |
|
--- |
7301 |
|
--- |
| 7302 |
for (const Use &U : Poison->uses()) { |
0 |
7302 |
for (const Use &U : Poison->uses()) { |
0 |
| 7303 |
const Instruction *PoisonUser = cast(U.getUser()); |
0 |
7303 |
const Instruction *PoisonUser = cast(U.getUser()); |
0 |
| 7304 |
if (mustTriggerUB(PoisonUser, KnownPoison) && |
0 |
7304 |
if (mustTriggerUB(PoisonUser, KnownPoison) && |
0 |
| 7305 |
DT.dominates(PoisonUser->getParent(), ExitingBB)) |
0 |
7305 |
DT.dominates(PoisonUser->getParent(), ExitingBB)) |
0 |
| 7306 |
return true; |
0 |
7306 |
return true; |
0 |
| 7307 |
|
--- |
7307 |
|
--- |
| 7308 |
if (propagatesPoison(U) && L->contains(PoisonUser)) |
0 |
7308 |
if (propagatesPoison(U) && L->contains(PoisonUser)) |
0 |
| 7309 |
if (KnownPoison.insert(PoisonUser).second) |
0 |
7309 |
if (KnownPoison.insert(PoisonUser).second) |
0 |
| 7310 |
Worklist.push_back(PoisonUser); |
0 |
7310 |
Worklist.push_back(PoisonUser); |
0 |
| 7311 |
} |
--- |
7311 |
} |
--- |
| 7312 |
} |
--- |
7312 |
} |
--- |
| 7313 |
|
--- |
7313 |
|
--- |
| 7314 |
return false; |
0 |
7314 |
return false; |
0 |
| 7315 |
} |
0 |
7315 |
} |
0 |
| 7316 |
|
--- |
7316 |
|
--- |
| 7317 |
ScalarEvolution::LoopProperties |
--- |
7317 |
ScalarEvolution::LoopProperties |
--- |
| 7318 |
ScalarEvolution::getLoopProperties(const Loop *L) { |
0 |
7318 |
ScalarEvolution::getLoopProperties(const Loop *L) { |
0 |
| 7319 |
using LoopProperties = ScalarEvolution::LoopProperties; |
--- |
7319 |
using LoopProperties = ScalarEvolution::LoopProperties; |
--- |
| 7320 |
|
--- |
7320 |
|
--- |
| 7321 |
auto Itr = LoopPropertiesCache.find(L); |
0 |
7321 |
auto Itr = LoopPropertiesCache.find(L); |
0 |
| 7322 |
if (Itr == LoopPropertiesCache.end()) { |
0 |
7322 |
if (Itr == LoopPropertiesCache.end()) { |
0 |
| 7323 |
auto HasSideEffects = [](Instruction *I) { |
0 |
7323 |
auto HasSideEffects = [](Instruction *I) { |
0 |
| 7324 |
if (auto *SI = dyn_cast(I)) |
0 |
7324 |
if (auto *SI = dyn_cast(I)) |
0 |
| 7325 |
return !SI->isSimple(); |
0 |
7325 |
return !SI->isSimple(); |
0 |
| 7326 |
|
--- |
7326 |
|
--- |
| 7327 |
return I->mayThrow() || I->mayWriteToMemory(); |
0 |
7327 |
return I->mayThrow() || I->mayWriteToMemory(); |
0 |
| 7328 |
}; |
--- |
7328 |
}; |
--- |
| 7329 |
|
--- |
7329 |
|
--- |
| 7330 |
LoopProperties LP = {/* HasNoAbnormalExits */ true, |
0 |
7330 |
LoopProperties LP = {/* HasNoAbnormalExits */ true, |
0 |
| 7331 |
/*HasNoSideEffects*/ true}; |
--- |
7331 |
/*HasNoSideEffects*/ true}; |
--- |
| 7332 |
|
--- |
7332 |
|
--- |
| 7333 |
for (auto *BB : L->getBlocks()) |
0 |
7333 |
for (auto *BB : L->getBlocks()) |
0 |
| 7334 |
for (auto &I : *BB) { |
0 |
7334 |
for (auto &I : *BB) { |
0 |
| 7335 |
if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
0 |
7335 |
if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
0 |
| 7336 |
LP.HasNoAbnormalExits = false; |
0 |
7336 |
LP.HasNoAbnormalExits = false; |
0 |
| 7337 |
if (HasSideEffects(&I)) |
0 |
7337 |
if (HasSideEffects(&I)) |
0 |
| 7338 |
LP.HasNoSideEffects = false; |
0 |
7338 |
LP.HasNoSideEffects = false; |
0 |
| 7339 |
if (!LP.HasNoAbnormalExits && !LP.HasNoSideEffects) |
0 |
7339 |
if (!LP.HasNoAbnormalExits && !LP.HasNoSideEffects) |
0 |
| 7340 |
break; // We're already as pessimistic as we can get. |
0 |
7340 |
break; // We're already as pessimistic as we can get. |
0 |
| 7341 |
} |
--- |
7341 |
} |
--- |
| 7342 |
|
--- |
7342 |
|
--- |
| 7343 |
auto InsertPair = LoopPropertiesCache.insert({L, LP}); |
0 |
7343 |
auto InsertPair = LoopPropertiesCache.insert({L, LP}); |
0 |
| 7344 |
assert(InsertPair.second && "We just checked!"); |
0 |
7344 |
assert(InsertPair.second && "We just checked!"); |
0 |
| 7345 |
Itr = InsertPair.first; |
0 |
7345 |
Itr = InsertPair.first; |
0 |
| 7346 |
} |
--- |
7346 |
} |
--- |
| 7347 |
|
--- |
7347 |
|
--- |
| 7348 |
return Itr->second; |
0 |
7348 |
return Itr->second; |
0 |
| 7349 |
} |
--- |
7349 |
} |
--- |
| 7350 |
|
--- |
7350 |
|
--- |
| 7351 |
bool ScalarEvolution::loopIsFiniteByAssumption(const Loop *L) { |
0 |
7351 |
bool ScalarEvolution::loopIsFiniteByAssumption(const Loop *L) { |
0 |
| 7352 |
// A mustprogress loop without side effects must be finite. |
--- |
7352 |
// A mustprogress loop without side effects must be finite. |
--- |
| 7353 |
// TODO: The check used here is very conservative. It's only *specific* |
--- |
7353 |
// TODO: The check used here is very conservative. It's only *specific* |
--- |
| 7354 |
// side effects which are well defined in infinite loops. |
--- |
7354 |
// side effects which are well defined in infinite loops. |
--- |
| 7355 |
return isFinite(L) || (isMustProgress(L) && loopHasNoSideEffects(L)); |
0 |
7355 |
return isFinite(L) || (isMustProgress(L) && loopHasNoSideEffects(L)); |
0 |
| 7356 |
} |
--- |
7356 |
} |
--- |
| 7357 |
|
--- |
7357 |
|
--- |
| 7358 |
const SCEV *ScalarEvolution::createSCEVIter(Value *V) { |
0 |
7358 |
const SCEV *ScalarEvolution::createSCEVIter(Value *V) { |
0 |
| 7359 |
// Worklist item with a Value and a bool indicating whether all operands have |
--- |
7359 |
// Worklist item with a Value and a bool indicating whether all operands have |
--- |
| 7360 |
// been visited already. |
--- |
7360 |
// been visited already. |
--- |
| 7361 |
using PointerTy = PointerIntPair; |
--- |
7361 |
using PointerTy = PointerIntPair; |
--- |
| 7362 |
SmallVector Stack; |
0 |
7362 |
SmallVector Stack; |
0 |
| 7363 |
|
--- |
7363 |
|
--- |
| 7364 |
Stack.emplace_back(V, true); |
0 |
7364 |
Stack.emplace_back(V, true); |
0 |
| 7365 |
Stack.emplace_back(V, false); |
0 |
7365 |
Stack.emplace_back(V, false); |
0 |
| 7366 |
while (!Stack.empty()) { |
0 |
7366 |
while (!Stack.empty()) { |
0 |
| 7367 |
auto E = Stack.pop_back_val(); |
0 |
7367 |
auto E = Stack.pop_back_val(); |
0 |
| 7368 |
Value *CurV = E.getPointer(); |
0 |
7368 |
Value *CurV = E.getPointer(); |
0 |
| 7369 |
|
--- |
7369 |
|
--- |
| 7370 |
if (getExistingSCEV(CurV)) |
0 |
7370 |
if (getExistingSCEV(CurV)) |
0 |
| 7371 |
continue; |
0 |
7371 |
continue; |
0 |
| 7372 |
|
--- |
7372 |
|
--- |
| 7373 |
SmallVector Ops; |
0 |
7373 |
SmallVector Ops; |
0 |
| 7374 |
const SCEV *CreatedSCEV = nullptr; |
0 |
7374 |
const SCEV *CreatedSCEV = nullptr; |
0 |
| 7375 |
// If all operands have been visited already, create the SCEV. |
--- |
7375 |
// If all operands have been visited already, create the SCEV. |
--- |
| 7376 |
if (E.getInt()) { |
0 |
7376 |
if (E.getInt()) { |
0 |
| 7377 |
CreatedSCEV = createSCEV(CurV); |
0 |
7377 |
CreatedSCEV = createSCEV(CurV); |
0 |
| 7378 |
} else { |
--- |
7378 |
} else { |
--- |
| 7379 |
// Otherwise get the operands we need to create SCEV's for before creating |
--- |
7379 |
// Otherwise get the operands we need to create SCEV's for before creating |
--- |
| 7380 |
// the SCEV for CurV. If the SCEV for CurV can be constructed trivially, |
--- |
7380 |
// the SCEV for CurV. If the SCEV for CurV can be constructed trivially, |
--- |
| 7381 |
// just use it. |
--- |
7381 |
// just use it. |
--- |
| 7382 |
CreatedSCEV = getOperandsToCreate(CurV, Ops); |
0 |
7382 |
CreatedSCEV = getOperandsToCreate(CurV, Ops); |
0 |
| 7383 |
} |
--- |
7383 |
} |
--- |
| 7384 |
|
--- |
7384 |
|
--- |
| 7385 |
if (CreatedSCEV) { |
0 |
7385 |
if (CreatedSCEV) { |
0 |
| 7386 |
insertValueToMap(CurV, CreatedSCEV); |
0 |
7386 |
insertValueToMap(CurV, CreatedSCEV); |
0 |
| 7387 |
} else { |
--- |
7387 |
} else { |
--- |
| 7388 |
// Queue CurV for SCEV creation, followed by its's operands which need to |
--- |
7388 |
// Queue CurV for SCEV creation, followed by its's operands which need to |
--- |
| 7389 |
// be constructed first. |
--- |
7389 |
// be constructed first. |
--- |
| 7390 |
Stack.emplace_back(CurV, true); |
0 |
7390 |
Stack.emplace_back(CurV, true); |
0 |
| 7391 |
for (Value *Op : Ops) |
0 |
7391 |
for (Value *Op : Ops) |
0 |
| 7392 |
Stack.emplace_back(Op, false); |
0 |
7392 |
Stack.emplace_back(Op, false); |
0 |
| 7393 |
} |
--- |
7393 |
} |
--- |
| 7394 |
} |
0 |
7394 |
} |
0 |
| 7395 |
|
--- |
7395 |
|
--- |
| 7396 |
return getExistingSCEV(V); |
0 |
7396 |
return getExistingSCEV(V); |
0 |
| 7397 |
} |
0 |
7397 |
} |
0 |
| 7398 |
|
--- |
7398 |
|
--- |
| 7399 |
const SCEV * |
--- |
7399 |
const SCEV * |
--- |
| 7400 |
ScalarEvolution::getOperandsToCreate(Value *V, SmallVectorImpl &Ops) { |
0 |
7400 |
ScalarEvolution::getOperandsToCreate(Value *V, SmallVectorImpl &Ops) { |
0 |
| 7401 |
if (!isSCEVable(V->getType())) |
0 |
7401 |
if (!isSCEVable(V->getType())) |
0 |
| 7402 |
return getUnknown(V); |
0 |
7402 |
return getUnknown(V); |
0 |
| 7403 |
|
--- |
7403 |
|
--- |
| 7404 |
if (Instruction *I = dyn_cast(V)) { |
0 |
7404 |
if (Instruction *I = dyn_cast(V)) { |
0 |
| 7405 |
// Don't attempt to analyze instructions in blocks that aren't |
--- |
7405 |
// Don't attempt to analyze instructions in blocks that aren't |
--- |
| 7406 |
// reachable. Such instructions don't matter, and they aren't required |
--- |
7406 |
// reachable. Such instructions don't matter, and they aren't required |
--- |
| 7407 |
// to obey basic rules for definitions dominating uses which this |
--- |
7407 |
// to obey basic rules for definitions dominating uses which this |
--- |
| 7408 |
// analysis depends on. |
--- |
7408 |
// analysis depends on. |
--- |
| 7409 |
if (!DT.isReachableFromEntry(I->getParent())) |
0 |
7409 |
if (!DT.isReachableFromEntry(I->getParent())) |
0 |
| 7410 |
return getUnknown(PoisonValue::get(V->getType())); |
0 |
7410 |
return getUnknown(PoisonValue::get(V->getType())); |
0 |
| 7411 |
} else if (ConstantInt *CI = dyn_cast(V)) |
0 |
7411 |
} else if (ConstantInt *CI = dyn_cast(V)) |
0 |
| 7412 |
return getConstant(CI); |
0 |
7412 |
return getConstant(CI); |
0 |
| 7413 |
else if (isa(V)) |
0 |
7413 |
else if (isa(V)) |
0 |
| 7414 |
return getUnknown(V); |
0 |
7414 |
return getUnknown(V); |
0 |
| 7415 |
else if (!isa(V)) |
0 |
7415 |
else if (!isa(V)) |
0 |
| 7416 |
return getUnknown(V); |
0 |
7416 |
return getUnknown(V); |
0 |
| 7417 |
|
--- |
7417 |
|
--- |
| 7418 |
Operator *U = cast(V); |
0 |
7418 |
Operator *U = cast(V); |
0 |
| 7419 |
if (auto BO = |
0 |
7419 |
if (auto BO = |
0 |
| 7420 |
MatchBinaryOp(U, getDataLayout(), AC, DT, dyn_cast(V))) { |
0 |
7420 |
MatchBinaryOp(U, getDataLayout(), AC, DT, dyn_cast(V))) { |
0 |
| 7421 |
bool IsConstArg = isa(BO->RHS); |
0 |
7421 |
bool IsConstArg = isa(BO->RHS); |
0 |
| 7422 |
switch (BO->Opcode) { |
0 |
7422 |
switch (BO->Opcode) { |
0 |
| 7423 |
case Instruction::Add: |
0 |
7423 |
case Instruction::Add: |
0 |
| 7424 |
case Instruction::Mul: { |
--- |
7424 |
case Instruction::Mul: { |
--- |
| 7425 |
// For additions and multiplications, traverse add/mul chains for which we |
--- |
7425 |
// For additions and multiplications, traverse add/mul chains for which we |
--- |
| 7426 |
// can potentially create a single SCEV, to reduce the number of |
--- |
7426 |
// can potentially create a single SCEV, to reduce the number of |
--- |
| 7427 |
// get{Add,Mul}Expr calls. |
--- |
7427 |
// get{Add,Mul}Expr calls. |
--- |
| 7428 |
do { |
--- |
7428 |
do { |
--- |
| 7429 |
if (BO->Op) { |
0 |
7429 |
if (BO->Op) { |
0 |
| 7430 |
if (BO->Op != V && getExistingSCEV(BO->Op)) { |
0 |
7430 |
if (BO->Op != V && getExistingSCEV(BO->Op)) { |
0 |
| 7431 |
Ops.push_back(BO->Op); |
0 |
7431 |
Ops.push_back(BO->Op); |
0 |
| 7432 |
break; |
0 |
7432 |
break; |
0 |
| 7433 |
} |
--- |
7433 |
} |
--- |
| 7434 |
} |
--- |
7434 |
} |
--- |
| 7435 |
Ops.push_back(BO->RHS); |
0 |
7435 |
Ops.push_back(BO->RHS); |
0 |
| 7436 |
auto NewBO = MatchBinaryOp(BO->LHS, getDataLayout(), AC, DT, |
0 |
7436 |
auto NewBO = MatchBinaryOp(BO->LHS, getDataLayout(), AC, DT, |
0 |
| 7437 |
dyn_cast(V)); |
0 |
7437 |
dyn_cast(V)); |
0 |
| 7438 |
if (!NewBO || |
0 |
7438 |
if (!NewBO || |
0 |
| 7439 |
(BO->Opcode == Instruction::Add && |
0 |
7439 |
(BO->Opcode == Instruction::Add && |
0 |
| 7440 |
(NewBO->Opcode != Instruction::Add && |
0 |
7440 |
(NewBO->Opcode != Instruction::Add && |
0 |
| 7441 |
NewBO->Opcode != Instruction::Sub)) || |
0 |
7441 |
NewBO->Opcode != Instruction::Sub)) || |
0 |
| 7442 |
(BO->Opcode == Instruction::Mul && |
0 |
7442 |
(BO->Opcode == Instruction::Mul && |
0 |
| 7443 |
NewBO->Opcode != Instruction::Mul)) { |
0 |
7443 |
NewBO->Opcode != Instruction::Mul)) { |
0 |
| 7444 |
Ops.push_back(BO->LHS); |
0 |
7444 |
Ops.push_back(BO->LHS); |
0 |
| 7445 |
break; |
0 |
7445 |
break; |
0 |
| 7446 |
} |
--- |
7446 |
} |
--- |
| 7447 |
// CreateSCEV calls getNoWrapFlagsFromUB, which under certain conditions |
--- |
7447 |
// CreateSCEV calls getNoWrapFlagsFromUB, which under certain conditions |
--- |
| 7448 |
// requires a SCEV for the LHS. |
--- |
7448 |
// requires a SCEV for the LHS. |
--- |
| 7449 |
if (BO->Op && (BO->IsNSW || BO->IsNUW)) { |
0 |
7449 |
if (BO->Op && (BO->IsNSW || BO->IsNUW)) { |
0 |
| 7450 |
auto *I = dyn_cast(BO->Op); |
0 |
7450 |
auto *I = dyn_cast(BO->Op); |
0 |
| 7451 |
if (I && programUndefinedIfPoison(I)) { |
0 |
7451 |
if (I && programUndefinedIfPoison(I)) { |
0 |
| 7452 |
Ops.push_back(BO->LHS); |
0 |
7452 |
Ops.push_back(BO->LHS); |
0 |
| 7453 |
break; |
0 |
7453 |
break; |
0 |
| 7454 |
} |
--- |
7454 |
} |
--- |
| 7455 |
} |
--- |
7455 |
} |
--- |
| 7456 |
BO = NewBO; |
0 |
7456 |
BO = NewBO; |
0 |
| 7457 |
} while (true); |
0 |
7457 |
} while (true); |
0 |
| 7458 |
return nullptr; |
0 |
7458 |
return nullptr; |
0 |
| 7459 |
} |
--- |
7459 |
} |
--- |
| 7460 |
case Instruction::Sub: |
0 |
7460 |
case Instruction::Sub: |
0 |
| 7461 |
case Instruction::UDiv: |
--- |
7461 |
case Instruction::UDiv: |
--- |
| 7462 |
case Instruction::URem: |
--- |
7462 |
case Instruction::URem: |
--- |
| 7463 |
break; |
0 |
7463 |
break; |
0 |
| 7464 |
case Instruction::AShr: |
0 |
7464 |
case Instruction::AShr: |
0 |
| 7465 |
case Instruction::Shl: |
--- |
7465 |
case Instruction::Shl: |
--- |
| 7466 |
case Instruction::Xor: |
--- |
7466 |
case Instruction::Xor: |
--- |
| 7467 |
if (!IsConstArg) |
0 |
7467 |
if (!IsConstArg) |
0 |
| 7468 |
return nullptr; |
0 |
7468 |
return nullptr; |
0 |
| 7469 |
break; |
0 |
7469 |
break; |
0 |
| 7470 |
case Instruction::And: |
0 |
7470 |
case Instruction::And: |
0 |
| 7471 |
case Instruction::Or: |
--- |
7471 |
case Instruction::Or: |
--- |
| 7472 |
if (!IsConstArg && !BO->LHS->getType()->isIntegerTy(1)) |
0 |
7472 |
if (!IsConstArg && !BO->LHS->getType()->isIntegerTy(1)) |
0 |
| 7473 |
return nullptr; |
0 |
7473 |
return nullptr; |
0 |
| 7474 |
break; |
0 |
7474 |
break; |
0 |
| 7475 |
case Instruction::LShr: |
0 |
7475 |
case Instruction::LShr: |
0 |
| 7476 |
return getUnknown(V); |
0 |
7476 |
return getUnknown(V); |
0 |
| 7477 |
default: |
0 |
7477 |
default: |
0 |
| 7478 |
llvm_unreachable("Unhandled binop"); |
0 |
7478 |
llvm_unreachable("Unhandled binop"); |
0 |
| 7479 |
break; |
--- |
7479 |
break; |
--- |
| 7480 |
} |
--- |
7480 |
} |
--- |
| 7481 |
|
--- |
7481 |
|
--- |
| 7482 |
Ops.push_back(BO->LHS); |
0 |
7482 |
Ops.push_back(BO->LHS); |
0 |
| 7483 |
Ops.push_back(BO->RHS); |
0 |
7483 |
Ops.push_back(BO->RHS); |
0 |
| 7484 |
return nullptr; |
0 |
7484 |
return nullptr; |
0 |
| 7485 |
} |
--- |
7485 |
} |
--- |
| 7486 |
|
--- |
7486 |
|
--- |
| 7487 |
switch (U->getOpcode()) { |
0 |
7487 |
switch (U->getOpcode()) { |
0 |
| 7488 |
case Instruction::Trunc: |
0 |
7488 |
case Instruction::Trunc: |
0 |
| 7489 |
case Instruction::ZExt: |
--- |
7489 |
case Instruction::ZExt: |
--- |
| 7490 |
case Instruction::SExt: |
--- |
7490 |
case Instruction::SExt: |
--- |
| 7491 |
case Instruction::PtrToInt: |
--- |
7491 |
case Instruction::PtrToInt: |
--- |
| 7492 |
Ops.push_back(U->getOperand(0)); |
0 |
7492 |
Ops.push_back(U->getOperand(0)); |
0 |
| 7493 |
return nullptr; |
0 |
7493 |
return nullptr; |
0 |
| 7494 |
|
--- |
7494 |
|
--- |
| 7495 |
case Instruction::BitCast: |
0 |
7495 |
case Instruction::BitCast: |
0 |
| 7496 |
if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) { |
0 |
7496 |
if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) { |
0 |
| 7497 |
Ops.push_back(U->getOperand(0)); |
0 |
7497 |
Ops.push_back(U->getOperand(0)); |
0 |
| 7498 |
return nullptr; |
0 |
7498 |
return nullptr; |
0 |
| 7499 |
} |
--- |
7499 |
} |
--- |
| 7500 |
return getUnknown(V); |
0 |
7500 |
return getUnknown(V); |
0 |
| 7501 |
|
--- |
7501 |
|
--- |
| 7502 |
case Instruction::SDiv: |
0 |
7502 |
case Instruction::SDiv: |
0 |
| 7503 |
case Instruction::SRem: |
--- |
7503 |
case Instruction::SRem: |
--- |
| 7504 |
Ops.push_back(U->getOperand(0)); |
0 |
7504 |
Ops.push_back(U->getOperand(0)); |
0 |
| 7505 |
Ops.push_back(U->getOperand(1)); |
0 |
7505 |
Ops.push_back(U->getOperand(1)); |
0 |
| 7506 |
return nullptr; |
0 |
7506 |
return nullptr; |
0 |
| 7507 |
|
--- |
7507 |
|
--- |
| 7508 |
case Instruction::GetElementPtr: |
0 |
7508 |
case Instruction::GetElementPtr: |
0 |
| 7509 |
assert(cast(U)->getSourceElementType()->isSized() && |
0 |
7509 |
assert(cast(U)->getSourceElementType()->isSized() && |
0 |
| 7510 |
"GEP source element type must be sized"); |
--- |
7510 |
"GEP source element type must be sized"); |
--- |
| 7511 |
for (Value *Index : U->operands()) |
0 |
7511 |
for (Value *Index : U->operands()) |
0 |
| 7512 |
Ops.push_back(Index); |
0 |
7512 |
Ops.push_back(Index); |
0 |
| 7513 |
return nullptr; |
0 |
7513 |
return nullptr; |
0 |
| 7514 |
|
--- |
7514 |
|
--- |
| 7515 |
case Instruction::IntToPtr: |
0 |
7515 |
case Instruction::IntToPtr: |
0 |
| 7516 |
return getUnknown(V); |
0 |
7516 |
return getUnknown(V); |
0 |
| 7517 |
|
--- |
7517 |
|
--- |
| 7518 |
case Instruction::PHI: |
0 |
7518 |
case Instruction::PHI: |
0 |
| 7519 |
// Keep constructing SCEVs' for phis recursively for now. |
--- |
7519 |
// Keep constructing SCEVs' for phis recursively for now. |
--- |
| 7520 |
return nullptr; |
0 |
7520 |
return nullptr; |
0 |
| 7521 |
|
--- |
7521 |
|
--- |
| 7522 |
case Instruction::Select: { |
0 |
7522 |
case Instruction::Select: { |
0 |
| 7523 |
// Check if U is a select that can be simplified to a SCEVUnknown. |
--- |
7523 |
// Check if U is a select that can be simplified to a SCEVUnknown. |
--- |
| 7524 |
auto CanSimplifyToUnknown = [this, U]() { |
0 |
7524 |
auto CanSimplifyToUnknown = [this, U]() { |
0 |
| 7525 |
if (U->getType()->isIntegerTy(1) || isa(U->getOperand(0))) |
0 |
7525 |
if (U->getType()->isIntegerTy(1) || isa(U->getOperand(0))) |
0 |
| 7526 |
return false; |
0 |
7526 |
return false; |
0 |
| 7527 |
|
--- |
7527 |
|
--- |
| 7528 |
auto *ICI = dyn_cast(U->getOperand(0)); |
0 |
7528 |
auto *ICI = dyn_cast(U->getOperand(0)); |
0 |
| 7529 |
if (!ICI) |
0 |
7529 |
if (!ICI) |
0 |
| 7530 |
return false; |
0 |
7530 |
return false; |
0 |
| 7531 |
Value *LHS = ICI->getOperand(0); |
0 |
7531 |
Value *LHS = ICI->getOperand(0); |
0 |
| 7532 |
Value *RHS = ICI->getOperand(1); |
0 |
7532 |
Value *RHS = ICI->getOperand(1); |
0 |
| 7533 |
if (ICI->getPredicate() == CmpInst::ICMP_EQ || |
0 |
7533 |
if (ICI->getPredicate() == CmpInst::ICMP_EQ || |
0 |
| 7534 |
ICI->getPredicate() == CmpInst::ICMP_NE) { |
0 |
7534 |
ICI->getPredicate() == CmpInst::ICMP_NE) { |
0 |
| 7535 |
if (!(isa(RHS) && cast(RHS)->isZero())) |
0 |
7535 |
if (!(isa(RHS) && cast(RHS)->isZero())) |
0 |
| 7536 |
return true; |
0 |
7536 |
return true; |
0 |
| 7537 |
} else if (getTypeSizeInBits(LHS->getType()) > |
0 |
7537 |
} else if (getTypeSizeInBits(LHS->getType()) > |
0 |
| 7538 |
getTypeSizeInBits(U->getType())) |
0 |
7538 |
getTypeSizeInBits(U->getType())) |
0 |
| 7539 |
return true; |
0 |
7539 |
return true; |
0 |
| 7540 |
return false; |
0 |
7540 |
return false; |
0 |
| 7541 |
}; |
0 |
7541 |
}; |
0 |
| 7542 |
if (CanSimplifyToUnknown()) |
0 |
7542 |
if (CanSimplifyToUnknown()) |
0 |
| 7543 |
return getUnknown(U); |
0 |
7543 |
return getUnknown(U); |
0 |
| 7544 |
|
--- |
7544 |
|
--- |
| 7545 |
for (Value *Inc : U->operands()) |
0 |
7545 |
for (Value *Inc : U->operands()) |
0 |
| 7546 |
Ops.push_back(Inc); |
0 |
7546 |
Ops.push_back(Inc); |
0 |
| 7547 |
return nullptr; |
0 |
7547 |
return nullptr; |
0 |
| 7548 |
break; |
--- |
7548 |
break; |
--- |
| 7549 |
} |
--- |
7549 |
} |
--- |
| 7550 |
case Instruction::Call: |
0 |
7550 |
case Instruction::Call: |
0 |
| 7551 |
case Instruction::Invoke: |
--- |
7551 |
case Instruction::Invoke: |
--- |
| 7552 |
if (Value *RV = cast(U)->getReturnedArgOperand()) { |
0 |
7552 |
if (Value *RV = cast(U)->getReturnedArgOperand()) { |
0 |
| 7553 |
Ops.push_back(RV); |
0 |
7553 |
Ops.push_back(RV); |
0 |
| 7554 |
return nullptr; |
0 |
7554 |
return nullptr; |
0 |
| 7555 |
} |
--- |
7555 |
} |
--- |
| 7556 |
|
--- |
7556 |
|
--- |
| 7557 |
if (auto *II = dyn_cast(U)) { |
0 |
7557 |
if (auto *II = dyn_cast(U)) { |
0 |
| 7558 |
switch (II->getIntrinsicID()) { |
0 |
7558 |
switch (II->getIntrinsicID()) { |
0 |
| 7559 |
case Intrinsic::abs: |
0 |
7559 |
case Intrinsic::abs: |
0 |
| 7560 |
Ops.push_back(II->getArgOperand(0)); |
0 |
7560 |
Ops.push_back(II->getArgOperand(0)); |
0 |
| 7561 |
return nullptr; |
0 |
7561 |
return nullptr; |
0 |
| 7562 |
case Intrinsic::umax: |
0 |
7562 |
case Intrinsic::umax: |
0 |
| 7563 |
case Intrinsic::umin: |
--- |
7563 |
case Intrinsic::umin: |
--- |
| 7564 |
case Intrinsic::smax: |
--- |
7564 |
case Intrinsic::smax: |
--- |
| 7565 |
case Intrinsic::smin: |
--- |
7565 |
case Intrinsic::smin: |
--- |
| 7566 |
case Intrinsic::usub_sat: |
--- |
7566 |
case Intrinsic::usub_sat: |
--- |
| 7567 |
case Intrinsic::uadd_sat: |
--- |
7567 |
case Intrinsic::uadd_sat: |
--- |
| 7568 |
Ops.push_back(II->getArgOperand(0)); |
0 |
7568 |
Ops.push_back(II->getArgOperand(0)); |
0 |
| 7569 |
Ops.push_back(II->getArgOperand(1)); |
0 |
7569 |
Ops.push_back(II->getArgOperand(1)); |
0 |
| 7570 |
return nullptr; |
0 |
7570 |
return nullptr; |
0 |
| 7571 |
case Intrinsic::start_loop_iterations: |
0 |
7571 |
case Intrinsic::start_loop_iterations: |
0 |
| 7572 |
case Intrinsic::annotation: |
--- |
7572 |
case Intrinsic::annotation: |
--- |
| 7573 |
case Intrinsic::ptr_annotation: |
--- |
7573 |
case Intrinsic::ptr_annotation: |
--- |
| 7574 |
Ops.push_back(II->getArgOperand(0)); |
0 |
7574 |
Ops.push_back(II->getArgOperand(0)); |
0 |
| 7575 |
return nullptr; |
0 |
7575 |
return nullptr; |
0 |
| 7576 |
default: |
0 |
7576 |
default: |
0 |
| 7577 |
break; |
0 |
7577 |
break; |
0 |
| 7578 |
} |
--- |
7578 |
} |
--- |
| 7579 |
} |
--- |
7579 |
} |
--- |
| 7580 |
break; |
0 |
7580 |
break; |
0 |
| 7581 |
} |
--- |
7581 |
} |
--- |
| 7582 |
|
--- |
7582 |
|
--- |
| 7583 |
return nullptr; |
0 |
7583 |
return nullptr; |
0 |
| 7584 |
} |
--- |
7584 |
} |
--- |
| 7585 |
|
--- |
7585 |
|
--- |
| 7586 |
const SCEV *ScalarEvolution::createSCEV(Value *V) { |
0 |
7586 |
const SCEV *ScalarEvolution::createSCEV(Value *V) { |
0 |
| 7587 |
if (!isSCEVable(V->getType())) |
0 |
7587 |
if (!isSCEVable(V->getType())) |
0 |
| 7588 |
return getUnknown(V); |
0 |
7588 |
return getUnknown(V); |
0 |
| 7589 |
|
--- |
7589 |
|
--- |
| 7590 |
if (Instruction *I = dyn_cast(V)) { |
0 |
7590 |
if (Instruction *I = dyn_cast(V)) { |
0 |
| 7591 |
// Don't attempt to analyze instructions in blocks that aren't |
--- |
7591 |
// Don't attempt to analyze instructions in blocks that aren't |
--- |
| 7592 |
// reachable. Such instructions don't matter, and they aren't required |
--- |
7592 |
// reachable. Such instructions don't matter, and they aren't required |
--- |
| 7593 |
// to obey basic rules for definitions dominating uses which this |
--- |
7593 |
// to obey basic rules for definitions dominating uses which this |
--- |
| 7594 |
// analysis depends on. |
--- |
7594 |
// analysis depends on. |
--- |
| 7595 |
if (!DT.isReachableFromEntry(I->getParent())) |
0 |
7595 |
if (!DT.isReachableFromEntry(I->getParent())) |
0 |
| 7596 |
return getUnknown(PoisonValue::get(V->getType())); |
0 |
7596 |
return getUnknown(PoisonValue::get(V->getType())); |
0 |
| 7597 |
} else if (ConstantInt *CI = dyn_cast(V)) |
0 |
7597 |
} else if (ConstantInt *CI = dyn_cast(V)) |
0 |
| 7598 |
return getConstant(CI); |
0 |
7598 |
return getConstant(CI); |
0 |
| 7599 |
else if (isa(V)) |
0 |
7599 |
else if (isa(V)) |
0 |
| 7600 |
return getUnknown(V); |
0 |
7600 |
return getUnknown(V); |
0 |
| 7601 |
else if (!isa(V)) |
0 |
7601 |
else if (!isa(V)) |
0 |
| 7602 |
return getUnknown(V); |
0 |
7602 |
return getUnknown(V); |
0 |
| 7603 |
|
--- |
7603 |
|
--- |
| 7604 |
const SCEV *LHS; |
--- |
7604 |
const SCEV *LHS; |
--- |
| 7605 |
const SCEV *RHS; |
--- |
7605 |
const SCEV *RHS; |
--- |
| 7606 |
|
--- |
7606 |
|
--- |
| 7607 |
Operator *U = cast(V); |
0 |
7607 |
Operator *U = cast(V); |
0 |
| 7608 |
if (auto BO = |
0 |
7608 |
if (auto BO = |
0 |
| 7609 |
MatchBinaryOp(U, getDataLayout(), AC, DT, dyn_cast(V))) { |
0 |
7609 |
MatchBinaryOp(U, getDataLayout(), AC, DT, dyn_cast(V))) { |
0 |
| 7610 |
switch (BO->Opcode) { |
0 |
7610 |
switch (BO->Opcode) { |
0 |
| 7611 |
case Instruction::Add: { |
0 |
7611 |
case Instruction::Add: { |
0 |
| 7612 |
// The simple thing to do would be to just call getSCEV on both operands |
--- |
7612 |
// The simple thing to do would be to just call getSCEV on both operands |
--- |
| 7613 |
// and call getAddExpr with the result. However if we're looking at a |
--- |
7613 |
// and call getAddExpr with the result. However if we're looking at a |
--- |
| 7614 |
// bunch of things all added together, this can be quite inefficient, |
--- |
7614 |
// bunch of things all added together, this can be quite inefficient, |
--- |
| 7615 |
// because it leads to N-1 getAddExpr calls for N ultimate operands. |
--- |
7615 |
// because it leads to N-1 getAddExpr calls for N ultimate operands. |
--- |
| 7616 |
// Instead, gather up all the operands and make a single getAddExpr call. |
--- |
7616 |
// Instead, gather up all the operands and make a single getAddExpr call. |
--- |
| 7617 |
// LLVM IR canonical form means we need only traverse the left operands. |
--- |
7617 |
// LLVM IR canonical form means we need only traverse the left operands. |
--- |
| 7618 |
SmallVector AddOps; |
0 |
7618 |
SmallVector AddOps; |
0 |
| 7619 |
do { |
--- |
7619 |
do { |
--- |
| 7620 |
if (BO->Op) { |
0 |
7620 |
if (BO->Op) { |
0 |
| 7621 |
if (auto *OpSCEV = getExistingSCEV(BO->Op)) { |
0 |
7621 |
if (auto *OpSCEV = getExistingSCEV(BO->Op)) { |
0 |
| 7622 |
AddOps.push_back(OpSCEV); |
0 |
7622 |
AddOps.push_back(OpSCEV); |
0 |
| 7623 |
break; |
0 |
7623 |
break; |
0 |
| 7624 |
} |
--- |
7624 |
} |
--- |
| 7625 |
|
--- |
7625 |
|
--- |
| 7626 |
// If a NUW or NSW flag can be applied to the SCEV for this |
--- |
7626 |
// If a NUW or NSW flag can be applied to the SCEV for this |
--- |
| 7627 |
// addition, then compute the SCEV for this addition by itself |
--- |
7627 |
// addition, then compute the SCEV for this addition by itself |
--- |
| 7628 |
// with a separate call to getAddExpr. We need to do that |
--- |
7628 |
// with a separate call to getAddExpr. We need to do that |
--- |
| 7629 |
// instead of pushing the operands of the addition onto AddOps, |
--- |
7629 |
// instead of pushing the operands of the addition onto AddOps, |
--- |
| 7630 |
// since the flags are only known to apply to this particular |
--- |
7630 |
// since the flags are only known to apply to this particular |
--- |
| 7631 |
// addition - they may not apply to other additions that can be |
--- |
7631 |
// addition - they may not apply to other additions that can be |
--- |
| 7632 |
// formed with operands from AddOps. |
--- |
7632 |
// formed with operands from AddOps. |
--- |
| 7633 |
const SCEV *RHS = getSCEV(BO->RHS); |
0 |
7633 |
const SCEV *RHS = getSCEV(BO->RHS); |
0 |
| 7634 |
SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op); |
0 |
7634 |
SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op); |
0 |
| 7635 |
if (Flags != SCEV::FlagAnyWrap) { |
0 |
7635 |
if (Flags != SCEV::FlagAnyWrap) { |
0 |
| 7636 |
const SCEV *LHS = getSCEV(BO->LHS); |
0 |
7636 |
const SCEV *LHS = getSCEV(BO->LHS); |
0 |
| 7637 |
if (BO->Opcode == Instruction::Sub) |
0 |
7637 |
if (BO->Opcode == Instruction::Sub) |
0 |
| 7638 |
AddOps.push_back(getMinusSCEV(LHS, RHS, Flags)); |
0 |
7638 |
AddOps.push_back(getMinusSCEV(LHS, RHS, Flags)); |
0 |
| 7639 |
else |
--- |
7639 |
else |
--- |
| 7640 |
AddOps.push_back(getAddExpr(LHS, RHS, Flags)); |
0 |
7640 |
AddOps.push_back(getAddExpr(LHS, RHS, Flags)); |
0 |
| 7641 |
break; |
0 |
7641 |
break; |
0 |
| 7642 |
} |
--- |
7642 |
} |
--- |
| 7643 |
} |
--- |
7643 |
} |
--- |
| 7644 |
|
--- |
7644 |
|
--- |
| 7645 |
if (BO->Opcode == Instruction::Sub) |
0 |
7645 |
if (BO->Opcode == Instruction::Sub) |
0 |
| 7646 |
AddOps.push_back(getNegativeSCEV(getSCEV(BO->RHS))); |
0 |
7646 |
AddOps.push_back(getNegativeSCEV(getSCEV(BO->RHS))); |
0 |
| 7647 |
else |
--- |
7647 |
else |
--- |
| 7648 |
AddOps.push_back(getSCEV(BO->RHS)); |
0 |
7648 |
AddOps.push_back(getSCEV(BO->RHS)); |
0 |
| 7649 |
|
--- |
7649 |
|
--- |
| 7650 |
auto NewBO = MatchBinaryOp(BO->LHS, getDataLayout(), AC, DT, |
0 |
7650 |
auto NewBO = MatchBinaryOp(BO->LHS, getDataLayout(), AC, DT, |
0 |
| 7651 |
dyn_cast(V)); |
0 |
7651 |
dyn_cast(V)); |
0 |
| 7652 |
if (!NewBO || (NewBO->Opcode != Instruction::Add && |
0 |
7652 |
if (!NewBO || (NewBO->Opcode != Instruction::Add && |
0 |
| 7653 |
NewBO->Opcode != Instruction::Sub)) { |
0 |
7653 |
NewBO->Opcode != Instruction::Sub)) { |
0 |
| 7654 |
AddOps.push_back(getSCEV(BO->LHS)); |
0 |
7654 |
AddOps.push_back(getSCEV(BO->LHS)); |
0 |
| 7655 |
break; |
0 |
7655 |
break; |
0 |
| 7656 |
} |
--- |
7656 |
} |
--- |
| 7657 |
BO = NewBO; |
0 |
7657 |
BO = NewBO; |
0 |
| 7658 |
} while (true); |
0 |
7658 |
} while (true); |
0 |
| 7659 |
|
--- |
7659 |
|
--- |
| 7660 |
return getAddExpr(AddOps); |
0 |
7660 |
return getAddExpr(AddOps); |
0 |
| 7661 |
} |
0 |
7661 |
} |
0 |
| 7662 |
|
--- |
7662 |
|
--- |
| 7663 |
case Instruction::Mul: { |
0 |
7663 |
case Instruction::Mul: { |
0 |
| 7664 |
SmallVector MulOps; |
0 |
7664 |
SmallVector MulOps; |
0 |
| 7665 |
do { |
--- |
7665 |
do { |
--- |
| 7666 |
if (BO->Op) { |
0 |
7666 |
if (BO->Op) { |
0 |
| 7667 |
if (auto *OpSCEV = getExistingSCEV(BO->Op)) { |
0 |
7667 |
if (auto *OpSCEV = getExistingSCEV(BO->Op)) { |
0 |
| 7668 |
MulOps.push_back(OpSCEV); |
0 |
7668 |
MulOps.push_back(OpSCEV); |
0 |
| 7669 |
break; |
0 |
7669 |
break; |
0 |
| 7670 |
} |
--- |
7670 |
} |
--- |
| 7671 |
|
--- |
7671 |
|
--- |
| 7672 |
SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op); |
0 |
7672 |
SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op); |
0 |
| 7673 |
if (Flags != SCEV::FlagAnyWrap) { |
0 |
7673 |
if (Flags != SCEV::FlagAnyWrap) { |
0 |
| 7674 |
LHS = getSCEV(BO->LHS); |
0 |
7674 |
LHS = getSCEV(BO->LHS); |
0 |
| 7675 |
RHS = getSCEV(BO->RHS); |
0 |
7675 |
RHS = getSCEV(BO->RHS); |
0 |
| 7676 |
MulOps.push_back(getMulExpr(LHS, RHS, Flags)); |
0 |
7676 |
MulOps.push_back(getMulExpr(LHS, RHS, Flags)); |
0 |
| 7677 |
break; |
0 |
7677 |
break; |
0 |
| 7678 |
} |
--- |
7678 |
} |
--- |
| 7679 |
} |
--- |
7679 |
} |
--- |
| 7680 |
|
--- |
7680 |
|
--- |
| 7681 |
MulOps.push_back(getSCEV(BO->RHS)); |
0 |
7681 |
MulOps.push_back(getSCEV(BO->RHS)); |
0 |
| 7682 |
auto NewBO = MatchBinaryOp(BO->LHS, getDataLayout(), AC, DT, |
0 |
7682 |
auto NewBO = MatchBinaryOp(BO->LHS, getDataLayout(), AC, DT, |
0 |
| 7683 |
dyn_cast(V)); |
0 |
7683 |
dyn_cast(V)); |
0 |
| 7684 |
if (!NewBO || NewBO->Opcode != Instruction::Mul) { |
0 |
7684 |
if (!NewBO || NewBO->Opcode != Instruction::Mul) { |
0 |
| 7685 |
MulOps.push_back(getSCEV(BO->LHS)); |
0 |
7685 |
MulOps.push_back(getSCEV(BO->LHS)); |
0 |
| 7686 |
break; |
0 |
7686 |
break; |
0 |
| 7687 |
} |
--- |
7687 |
} |
--- |
| 7688 |
BO = NewBO; |
0 |
7688 |
BO = NewBO; |
0 |
| 7689 |
} while (true); |
0 |
7689 |
} while (true); |
0 |
| 7690 |
|
--- |
7690 |
|
--- |
| 7691 |
return getMulExpr(MulOps); |
0 |
7691 |
return getMulExpr(MulOps); |
0 |
| 7692 |
} |
0 |
7692 |
} |
0 |
| 7693 |
case Instruction::UDiv: |
0 |
7693 |
case Instruction::UDiv: |
0 |
| 7694 |
LHS = getSCEV(BO->LHS); |
0 |
7694 |
LHS = getSCEV(BO->LHS); |
0 |
| 7695 |
RHS = getSCEV(BO->RHS); |
0 |
7695 |
RHS = getSCEV(BO->RHS); |
0 |
| 7696 |
return getUDivExpr(LHS, RHS); |
0 |
7696 |
return getUDivExpr(LHS, RHS); |
0 |
| 7697 |
case Instruction::URem: |
0 |
7697 |
case Instruction::URem: |
0 |
| 7698 |
LHS = getSCEV(BO->LHS); |
0 |
7698 |
LHS = getSCEV(BO->LHS); |
0 |
| 7699 |
RHS = getSCEV(BO->RHS); |
0 |
7699 |
RHS = getSCEV(BO->RHS); |
0 |
| 7700 |
return getURemExpr(LHS, RHS); |
0 |
7700 |
return getURemExpr(LHS, RHS); |
0 |
| 7701 |
case Instruction::Sub: { |
0 |
7701 |
case Instruction::Sub: { |
0 |
| 7702 |
SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; |
0 |
7702 |
SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; |
0 |
| 7703 |
if (BO->Op) |
0 |
7703 |
if (BO->Op) |
0 |
| 7704 |
Flags = getNoWrapFlagsFromUB(BO->Op); |
0 |
7704 |
Flags = getNoWrapFlagsFromUB(BO->Op); |
0 |
| 7705 |
LHS = getSCEV(BO->LHS); |
0 |
7705 |
LHS = getSCEV(BO->LHS); |
0 |
| 7706 |
RHS = getSCEV(BO->RHS); |
0 |
7706 |
RHS = getSCEV(BO->RHS); |
0 |
| 7707 |
return getMinusSCEV(LHS, RHS, Flags); |
0 |
7707 |
return getMinusSCEV(LHS, RHS, Flags); |
0 |
| 7708 |
} |
--- |
7708 |
} |
--- |
| 7709 |
case Instruction::And: |
0 |
7709 |
case Instruction::And: |
0 |
| 7710 |
// For an expression like x&255 that merely masks off the high bits, |
--- |
7710 |
// For an expression like x&255 that merely masks off the high bits, |
--- |
| 7711 |
// use zext(trunc(x)) as the SCEV expression. |
--- |
7711 |
// use zext(trunc(x)) as the SCEV expression. |
--- |
| 7712 |
if (ConstantInt *CI = dyn_cast(BO->RHS)) { |
0 |
7712 |
if (ConstantInt *CI = dyn_cast(BO->RHS)) { |
0 |
| 7713 |
if (CI->isZero()) |
0 |
7713 |
if (CI->isZero()) |
0 |
| 7714 |
return getSCEV(BO->RHS); |
0 |
7714 |
return getSCEV(BO->RHS); |
0 |
| 7715 |
if (CI->isMinusOne()) |
0 |
7715 |
if (CI->isMinusOne()) |
0 |
| 7716 |
return getSCEV(BO->LHS); |
0 |
7716 |
return getSCEV(BO->LHS); |
0 |
| 7717 |
const APInt &A = CI->getValue(); |
0 |
7717 |
const APInt &A = CI->getValue(); |
0 |
| 7718 |
|
--- |
7718 |
|
--- |
| 7719 |
// Instcombine's ShrinkDemandedConstant may strip bits out of |
--- |
7719 |
// Instcombine's ShrinkDemandedConstant may strip bits out of |
--- |
| 7720 |
// constants, obscuring what would otherwise be a low-bits mask. |
--- |
7720 |
// constants, obscuring what would otherwise be a low-bits mask. |
--- |
| 7721 |
// Use computeKnownBits to compute what ShrinkDemandedConstant |
--- |
7721 |
// Use computeKnownBits to compute what ShrinkDemandedConstant |
--- |
| 7722 |
// knew about to reconstruct a low-bits mask value. |
--- |
7722 |
// knew about to reconstruct a low-bits mask value. |
--- |
| 7723 |
unsigned LZ = A.countl_zero(); |
0 |
7723 |
unsigned LZ = A.countl_zero(); |
0 |
| 7724 |
unsigned TZ = A.countr_zero(); |
0 |
7724 |
unsigned TZ = A.countr_zero(); |
0 |
| 7725 |
unsigned BitWidth = A.getBitWidth(); |
0 |
7725 |
unsigned BitWidth = A.getBitWidth(); |
0 |
| 7726 |
KnownBits Known(BitWidth); |
0 |
7726 |
KnownBits Known(BitWidth); |
0 |
| 7727 |
computeKnownBits(BO->LHS, Known, getDataLayout(), |
0 |
7727 |
computeKnownBits(BO->LHS, Known, getDataLayout(), |
0 |
| 7728 |
0, &AC, nullptr, &DT); |
0 |
7728 |
0, &AC, nullptr, &DT); |
0 |
| 7729 |
|
--- |
7729 |
|
--- |
| 7730 |
APInt EffectiveMask = |
--- |
7730 |
APInt EffectiveMask = |
--- |
| 7731 |
APInt::getLowBitsSet(BitWidth, BitWidth - LZ - TZ).shl(TZ); |
0 |
7731 |
APInt::getLowBitsSet(BitWidth, BitWidth - LZ - TZ).shl(TZ); |
0 |
| 7732 |
if ((LZ != 0 || TZ != 0) && !((~A & ~Known.Zero) & EffectiveMask)) { |
0 |
7732 |
if ((LZ != 0 || TZ != 0) && !((~A & ~Known.Zero) & EffectiveMask)) { |
0 |
| 7733 |
const SCEV *MulCount = getConstant(APInt::getOneBitSet(BitWidth, TZ)); |
0 |
7733 |
const SCEV *MulCount = getConstant(APInt::getOneBitSet(BitWidth, TZ)); |
0 |
| 7734 |
const SCEV *LHS = getSCEV(BO->LHS); |
0 |
7734 |
const SCEV *LHS = getSCEV(BO->LHS); |
0 |
| 7735 |
const SCEV *ShiftedLHS = nullptr; |
0 |
7735 |
const SCEV *ShiftedLHS = nullptr; |
0 |
| 7736 |
if (auto *LHSMul = dyn_cast(LHS)) { |
0 |
7736 |
if (auto *LHSMul = dyn_cast(LHS)) { |
0 |
| 7737 |
if (auto *OpC = dyn_cast(LHSMul->getOperand(0))) { |
0 |
7737 |
if (auto *OpC = dyn_cast(LHSMul->getOperand(0))) { |
0 |
| 7738 |
// For an expression like (x * 8) & 8, simplify the multiply. |
--- |
7738 |
// For an expression like (x * 8) & 8, simplify the multiply. |
--- |
| 7739 |
unsigned MulZeros = OpC->getAPInt().countr_zero(); |
0 |
7739 |
unsigned MulZeros = OpC->getAPInt().countr_zero(); |
0 |
| 7740 |
unsigned GCD = std::min(MulZeros, TZ); |
0 |
7740 |
unsigned GCD = std::min(MulZeros, TZ); |
0 |
| 7741 |
APInt DivAmt = APInt::getOneBitSet(BitWidth, TZ - GCD); |
0 |
7741 |
APInt DivAmt = APInt::getOneBitSet(BitWidth, TZ - GCD); |
0 |
| 7742 |
SmallVector MulOps; |
0 |
7742 |
SmallVector MulOps; |
0 |
| 7743 |
MulOps.push_back(getConstant(OpC->getAPInt().lshr(GCD))); |
0 |
7743 |
MulOps.push_back(getConstant(OpC->getAPInt().lshr(GCD))); |
0 |
| 7744 |
append_range(MulOps, LHSMul->operands().drop_front()); |
0 |
7744 |
append_range(MulOps, LHSMul->operands().drop_front()); |
0 |
| 7745 |
auto *NewMul = getMulExpr(MulOps, LHSMul->getNoWrapFlags()); |
0 |
7745 |
auto *NewMul = getMulExpr(MulOps, LHSMul->getNoWrapFlags()); |
0 |
| 7746 |
ShiftedLHS = getUDivExpr(NewMul, getConstant(DivAmt)); |
0 |
7746 |
ShiftedLHS = getUDivExpr(NewMul, getConstant(DivAmt)); |
0 |
| 7747 |
} |
0 |
7747 |
} |
0 |
| 7748 |
} |
--- |
7748 |
} |
--- |
| 7749 |
if (!ShiftedLHS) |
0 |
7749 |
if (!ShiftedLHS) |
0 |
| 7750 |
ShiftedLHS = getUDivExpr(LHS, MulCount); |
0 |
7750 |
ShiftedLHS = getUDivExpr(LHS, MulCount); |
0 |
| 7751 |
return getMulExpr( |
0 |
7751 |
return getMulExpr( |
0 |
| 7752 |
getZeroExtendExpr( |
--- |
7752 |
getZeroExtendExpr( |
--- |
| 7753 |
getTruncateExpr(ShiftedLHS, |
--- |
7753 |
getTruncateExpr(ShiftedLHS, |
--- |
| 7754 |
IntegerType::get(getContext(), BitWidth - LZ - TZ)), |
0 |
7754 |
IntegerType::get(getContext(), BitWidth - LZ - TZ)), |
0 |
| 7755 |
BO->LHS->getType()), |
0 |
7755 |
BO->LHS->getType()), |
0 |
| 7756 |
MulCount); |
0 |
7756 |
MulCount); |
0 |
| 7757 |
} |
--- |
7757 |
} |
--- |
| 7758 |
} |
0 |
7758 |
} |
0 |
| 7759 |
// Binary `and` is a bit-wise `umin`. |
--- |
7759 |
// Binary `and` is a bit-wise `umin`. |
--- |
| 7760 |
if (BO->LHS->getType()->isIntegerTy(1)) { |
0 |
7760 |
if (BO->LHS->getType()->isIntegerTy(1)) { |
0 |
| 7761 |
LHS = getSCEV(BO->LHS); |
0 |
7761 |
LHS = getSCEV(BO->LHS); |
0 |
| 7762 |
RHS = getSCEV(BO->RHS); |
0 |
7762 |
RHS = getSCEV(BO->RHS); |
0 |
| 7763 |
return getUMinExpr(LHS, RHS); |
0 |
7763 |
return getUMinExpr(LHS, RHS); |
0 |
| 7764 |
} |
--- |
7764 |
} |
--- |
| 7765 |
break; |
0 |
7765 |
break; |
0 |
| 7766 |
|
--- |
7766 |
|
--- |
| 7767 |
case Instruction::Or: |
0 |
7767 |
case Instruction::Or: |
0 |
| 7768 |
// Binary `or` is a bit-wise `umax`. |
--- |
7768 |
// Binary `or` is a bit-wise `umax`. |
--- |
| 7769 |
if (BO->LHS->getType()->isIntegerTy(1)) { |
0 |
7769 |
if (BO->LHS->getType()->isIntegerTy(1)) { |
0 |
| 7770 |
LHS = getSCEV(BO->LHS); |
0 |
7770 |
LHS = getSCEV(BO->LHS); |
0 |
| 7771 |
RHS = getSCEV(BO->RHS); |
0 |
7771 |
RHS = getSCEV(BO->RHS); |
0 |
| 7772 |
return getUMaxExpr(LHS, RHS); |
0 |
7772 |
return getUMaxExpr(LHS, RHS); |
0 |
| 7773 |
} |
--- |
7773 |
} |
--- |
| 7774 |
break; |
0 |
7774 |
break; |
0 |
| 7775 |
|
--- |
7775 |
|
--- |
| 7776 |
case Instruction::Xor: |
0 |
7776 |
case Instruction::Xor: |
0 |
| 7777 |
if (ConstantInt *CI = dyn_cast(BO->RHS)) { |
0 |
7777 |
if (ConstantInt *CI = dyn_cast(BO->RHS)) { |
0 |
| 7778 |
// If the RHS of xor is -1, then this is a not operation. |
--- |
7778 |
// If the RHS of xor is -1, then this is a not operation. |
--- |
| 7779 |
if (CI->isMinusOne()) |
0 |
7779 |
if (CI->isMinusOne()) |
0 |
| 7780 |
return getNotSCEV(getSCEV(BO->LHS)); |
0 |
7780 |
return getNotSCEV(getSCEV(BO->LHS)); |
0 |
| 7781 |
|
--- |
7781 |
|
--- |
| 7782 |
// Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. |
--- |
7782 |
// Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. |
--- |
| 7783 |
// This is a variant of the check for xor with -1, and it handles |
--- |
7783 |
// This is a variant of the check for xor with -1, and it handles |
--- |
| 7784 |
// the case where instcombine has trimmed non-demanded bits out |
--- |
7784 |
// the case where instcombine has trimmed non-demanded bits out |
--- |
| 7785 |
// of an xor with -1. |
--- |
7785 |
// of an xor with -1. |
--- |
| 7786 |
if (auto *LBO = dyn_cast(BO->LHS)) |
0 |
7786 |
if (auto *LBO = dyn_cast(BO->LHS)) |
0 |
| 7787 |
if (ConstantInt *LCI = dyn_cast(LBO->getOperand(1))) |
0 |
7787 |
if (ConstantInt *LCI = dyn_cast(LBO->getOperand(1))) |
0 |
| 7788 |
if (LBO->getOpcode() == Instruction::And && |
0 |
7788 |
if (LBO->getOpcode() == Instruction::And && |
0 |
| 7789 |
LCI->getValue() == CI->getValue()) |
0 |
7789 |
LCI->getValue() == CI->getValue()) |
0 |
| 7790 |
if (const SCEVZeroExtendExpr *Z = |
0 |
7790 |
if (const SCEVZeroExtendExpr *Z = |
0 |
| 7791 |
dyn_cast(getSCEV(BO->LHS))) { |
0 |
7791 |
dyn_cast(getSCEV(BO->LHS))) { |
0 |
| 7792 |
Type *UTy = BO->LHS->getType(); |
0 |
7792 |
Type *UTy = BO->LHS->getType(); |
0 |
| 7793 |
const SCEV *Z0 = Z->getOperand(); |
0 |
7793 |
const SCEV *Z0 = Z->getOperand(); |
0 |
| 7794 |
Type *Z0Ty = Z0->getType(); |
0 |
7794 |
Type *Z0Ty = Z0->getType(); |
0 |
| 7795 |
unsigned Z0TySize = getTypeSizeInBits(Z0Ty); |
0 |
7795 |
unsigned Z0TySize = getTypeSizeInBits(Z0Ty); |
0 |
| 7796 |
|
--- |
7796 |
|
--- |
| 7797 |
// If C is a low-bits mask, the zero extend is serving to |
--- |
7797 |
// If C is a low-bits mask, the zero extend is serving to |
--- |
| 7798 |
// mask off the high bits. Complement the operand and |
--- |
7798 |
// mask off the high bits. Complement the operand and |
--- |
| 7799 |
// re-apply the zext. |
--- |
7799 |
// re-apply the zext. |
--- |
| 7800 |
if (CI->getValue().isMask(Z0TySize)) |
0 |
7800 |
if (CI->getValue().isMask(Z0TySize)) |
0 |
| 7801 |
return getZeroExtendExpr(getNotSCEV(Z0), UTy); |
0 |
7801 |
return getZeroExtendExpr(getNotSCEV(Z0), UTy); |
0 |
| 7802 |
|
--- |
7802 |
|
--- |
| 7803 |
// If C is a single bit, it may be in the sign-bit position |
--- |
7803 |
// If C is a single bit, it may be in the sign-bit position |
--- |
| 7804 |
// before the zero-extend. In this case, represent the xor |
--- |
7804 |
// before the zero-extend. In this case, represent the xor |
--- |
| 7805 |
// using an add, which is equivalent, and re-apply the zext. |
--- |
7805 |
// using an add, which is equivalent, and re-apply the zext. |
--- |
| 7806 |
APInt Trunc = CI->getValue().trunc(Z0TySize); |
0 |
7806 |
APInt Trunc = CI->getValue().trunc(Z0TySize); |
0 |
| 7807 |
if (Trunc.zext(getTypeSizeInBits(UTy)) == CI->getValue() && |
0 |
7807 |
if (Trunc.zext(getTypeSizeInBits(UTy)) == CI->getValue() && |
0 |
| 7808 |
Trunc.isSignMask()) |
0 |
7808 |
Trunc.isSignMask()) |
0 |
| 7809 |
return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), |
0 |
7809 |
return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), |
0 |
| 7810 |
UTy); |
0 |
7810 |
UTy); |
0 |
| 7811 |
} |
0 |
7811 |
} |
0 |
| 7812 |
} |
--- |
7812 |
} |
--- |
| 7813 |
break; |
0 |
7813 |
break; |
0 |
| 7814 |
|
--- |
7814 |
|
--- |
| 7815 |
case Instruction::Shl: |
0 |
7815 |
case Instruction::Shl: |
0 |
| 7816 |
// Turn shift left of a constant amount into a multiply. |
--- |
7816 |
// Turn shift left of a constant amount into a multiply. |
--- |
| 7817 |
if (ConstantInt *SA = dyn_cast(BO->RHS)) { |
0 |
7817 |
if (ConstantInt *SA = dyn_cast(BO->RHS)) { |
0 |
| 7818 |
uint32_t BitWidth = cast(SA->getType())->getBitWidth(); |
0 |
7818 |
uint32_t BitWidth = cast(SA->getType())->getBitWidth(); |
0 |
| 7819 |
|
--- |
7819 |
|
--- |
| 7820 |
// If the shift count is not less than the bitwidth, the result of |
--- |
7820 |
// If the shift count is not less than the bitwidth, the result of |
--- |
| 7821 |
// the shift is undefined. Don't try to analyze it, because the |
--- |
7821 |
// the shift is undefined. Don't try to analyze it, because the |
--- |
| 7822 |
// resolution chosen here may differ from the resolution chosen in |
--- |
7822 |
// resolution chosen here may differ from the resolution chosen in |
--- |
| 7823 |
// other parts of the compiler. |
--- |
7823 |
// other parts of the compiler. |
--- |
| 7824 |
if (SA->getValue().uge(BitWidth)) |
0 |
7824 |
if (SA->getValue().uge(BitWidth)) |
0 |
| 7825 |
break; |
0 |
7825 |
break; |
0 |
| 7826 |
|
--- |
7826 |
|
--- |
| 7827 |
// We can safely preserve the nuw flag in all cases. It's also safe to |
--- |
7827 |
// We can safely preserve the nuw flag in all cases. It's also safe to |
--- |
| 7828 |
// turn a nuw nsw shl into a nuw nsw mul. However, nsw in isolation |
--- |
7828 |
// turn a nuw nsw shl into a nuw nsw mul. However, nsw in isolation |
--- |
| 7829 |
// requires special handling. It can be preserved as long as we're not |
--- |
7829 |
// requires special handling. It can be preserved as long as we're not |
--- |
| 7830 |
// left shifting by bitwidth - 1. |
--- |
7830 |
// left shifting by bitwidth - 1. |
--- |
| 7831 |
auto Flags = SCEV::FlagAnyWrap; |
0 |
7831 |
auto Flags = SCEV::FlagAnyWrap; |
0 |
| 7832 |
if (BO->Op) { |
0 |
7832 |
if (BO->Op) { |
0 |
| 7833 |
auto MulFlags = getNoWrapFlagsFromUB(BO->Op); |
0 |
7833 |
auto MulFlags = getNoWrapFlagsFromUB(BO->Op); |
0 |
| 7834 |
if ((MulFlags & SCEV::FlagNSW) && |
0 |
7834 |
if ((MulFlags & SCEV::FlagNSW) && |
0 |
| 7835 |
((MulFlags & SCEV::FlagNUW) || SA->getValue().ult(BitWidth - 1))) |
0 |
7835 |
((MulFlags & SCEV::FlagNUW) || SA->getValue().ult(BitWidth - 1))) |
0 |
| 7836 |
Flags = (SCEV::NoWrapFlags)(Flags | SCEV::FlagNSW); |
0 |
7836 |
Flags = (SCEV::NoWrapFlags)(Flags | SCEV::FlagNSW); |
0 |
| 7837 |
if (MulFlags & SCEV::FlagNUW) |
0 |
7837 |
if (MulFlags & SCEV::FlagNUW) |
0 |
| 7838 |
Flags = (SCEV::NoWrapFlags)(Flags | SCEV::FlagNUW); |
0 |
7838 |
Flags = (SCEV::NoWrapFlags)(Flags | SCEV::FlagNUW); |
0 |
| 7839 |
} |
--- |
7839 |
} |
--- |
| 7840 |
|
--- |
7840 |
|
--- |
| 7841 |
ConstantInt *X = ConstantInt::get( |
0 |
7841 |
ConstantInt *X = ConstantInt::get( |
0 |
| 7842 |
getContext(), APInt::getOneBitSet(BitWidth, SA->getZExtValue())); |
0 |
7842 |
getContext(), APInt::getOneBitSet(BitWidth, SA->getZExtValue())); |
0 |
| 7843 |
return getMulExpr(getSCEV(BO->LHS), getConstant(X), Flags); |
0 |
7843 |
return getMulExpr(getSCEV(BO->LHS), getConstant(X), Flags); |
0 |
| 7844 |
} |
--- |
7844 |
} |
--- |
| 7845 |
break; |
0 |
7845 |
break; |
0 |
| 7846 |
|
--- |
7846 |
|
--- |
| 7847 |
case Instruction::AShr: { |
0 |
7847 |
case Instruction::AShr: { |
0 |
| 7848 |
// AShr X, C, where C is a constant. |
--- |
7848 |
// AShr X, C, where C is a constant. |
--- |
| 7849 |
ConstantInt *CI = dyn_cast(BO->RHS); |
0 |
7849 |
ConstantInt *CI = dyn_cast(BO->RHS); |
0 |
| 7850 |
if (!CI) |
0 |
7850 |
if (!CI) |
0 |
| 7851 |
break; |
0 |
7851 |
break; |
0 |
| 7852 |
|
--- |
7852 |
|
--- |
| 7853 |
Type *OuterTy = BO->LHS->getType(); |
0 |
7853 |
Type *OuterTy = BO->LHS->getType(); |
0 |
| 7854 |
uint64_t BitWidth = getTypeSizeInBits(OuterTy); |
0 |
7854 |
uint64_t BitWidth = getTypeSizeInBits(OuterTy); |
0 |
| 7855 |
// If the shift count is not less than the bitwidth, the result of |
--- |
7855 |
// If the shift count is not less than the bitwidth, the result of |
--- |
| 7856 |
// the shift is undefined. Don't try to analyze it, because the |
--- |
7856 |
// the shift is undefined. Don't try to analyze it, because the |
--- |
| 7857 |
// resolution chosen here may differ from the resolution chosen in |
--- |
7857 |
// resolution chosen here may differ from the resolution chosen in |
--- |
| 7858 |
// other parts of the compiler. |
--- |
7858 |
// other parts of the compiler. |
--- |
| 7859 |
if (CI->getValue().uge(BitWidth)) |
0 |
7859 |
if (CI->getValue().uge(BitWidth)) |
0 |
| 7860 |
break; |
0 |
7860 |
break; |
0 |
| 7861 |
|
--- |
7861 |
|
--- |
| 7862 |
if (CI->isZero()) |
0 |
7862 |
if (CI->isZero()) |
0 |
| 7863 |
return getSCEV(BO->LHS); // shift by zero --> noop |
0 |
7863 |
return getSCEV(BO->LHS); // shift by zero --> noop |
0 |
| 7864 |
|
--- |
7864 |
|
--- |
| 7865 |
uint64_t AShrAmt = CI->getZExtValue(); |
0 |
7865 |
uint64_t AShrAmt = CI->getZExtValue(); |
0 |
| 7866 |
Type *TruncTy = IntegerType::get(getContext(), BitWidth - AShrAmt); |
0 |
7866 |
Type *TruncTy = IntegerType::get(getContext(), BitWidth - AShrAmt); |
0 |
| 7867 |
|
--- |
7867 |
|
--- |
| 7868 |
Operator *L = dyn_cast(BO->LHS); |
0 |
7868 |
Operator *L = dyn_cast(BO->LHS); |
0 |
| 7869 |
if (L && L->getOpcode() == Instruction::Shl) { |
0 |
7869 |
if (L && L->getOpcode() == Instruction::Shl) { |
0 |
| 7870 |
// X = Shl A, n |
--- |
7870 |
// X = Shl A, n |
--- |
| 7871 |
// Y = AShr X, m |
--- |
7871 |
// Y = AShr X, m |
--- |
| 7872 |
// Both n and m are constant. |
--- |
7872 |
// Both n and m are constant. |
--- |
| 7873 |
|
--- |
7873 |
|
--- |
| 7874 |
const SCEV *ShlOp0SCEV = getSCEV(L->getOperand(0)); |
0 |
7874 |
const SCEV *ShlOp0SCEV = getSCEV(L->getOperand(0)); |
0 |
| 7875 |
if (L->getOperand(1) == BO->RHS) |
0 |
7875 |
if (L->getOperand(1) == BO->RHS) |
0 |
| 7876 |
// For a two-shift sext-inreg, i.e. n = m, |
--- |
7876 |
// For a two-shift sext-inreg, i.e. n = m, |
--- |
| 7877 |
// use sext(trunc(x)) as the SCEV expression. |
--- |
7877 |
// use sext(trunc(x)) as the SCEV expression. |
--- |
| 7878 |
return getSignExtendExpr( |
0 |
7878 |
return getSignExtendExpr( |
0 |
| 7879 |
getTruncateExpr(ShlOp0SCEV, TruncTy), OuterTy); |
0 |
7879 |
getTruncateExpr(ShlOp0SCEV, TruncTy), OuterTy); |
0 |
| 7880 |
|
--- |
7880 |
|
--- |
| 7881 |
ConstantInt *ShlAmtCI = dyn_cast(L->getOperand(1)); |
0 |
7881 |
ConstantInt *ShlAmtCI = dyn_cast(L->getOperand(1)); |
0 |
| 7882 |
if (ShlAmtCI && ShlAmtCI->getValue().ult(BitWidth)) { |
0 |
7882 |
if (ShlAmtCI && ShlAmtCI->getValue().ult(BitWidth)) { |
0 |
| 7883 |
uint64_t ShlAmt = ShlAmtCI->getZExtValue(); |
0 |
7883 |
uint64_t ShlAmt = ShlAmtCI->getZExtValue(); |
0 |
| 7884 |
if (ShlAmt > AShrAmt) { |
0 |
7884 |
if (ShlAmt > AShrAmt) { |
0 |
| 7885 |
// When n > m, use sext(mul(trunc(x), 2^(n-m)))) as the SCEV |
--- |
7885 |
// When n > m, use sext(mul(trunc(x), 2^(n-m)))) as the SCEV |
--- |
| 7886 |
// expression. We already checked that ShlAmt < BitWidth, so |
--- |
7886 |
// expression. We already checked that ShlAmt < BitWidth, so |
--- |
| 7887 |
// the multiplier, 1 << (ShlAmt - AShrAmt), fits into TruncTy as |
--- |
7887 |
// the multiplier, 1 << (ShlAmt - AShrAmt), fits into TruncTy as |
--- |
| 7888 |
// ShlAmt - AShrAmt < Amt. |
--- |
7888 |
// ShlAmt - AShrAmt < Amt. |
--- |
| 7889 |
APInt Mul = APInt::getOneBitSet(BitWidth - AShrAmt, |
--- |
7889 |
APInt Mul = APInt::getOneBitSet(BitWidth - AShrAmt, |
--- |
| 7890 |
ShlAmt - AShrAmt); |
0 |
7890 |
ShlAmt - AShrAmt); |
0 |
| 7891 |
return getSignExtendExpr( |
0 |
7891 |
return getSignExtendExpr( |
0 |
| 7892 |
getMulExpr(getTruncateExpr(ShlOp0SCEV, TruncTy), |
--- |
7892 |
getMulExpr(getTruncateExpr(ShlOp0SCEV, TruncTy), |
--- |
| 7893 |
getConstant(Mul)), OuterTy); |
0 |
7893 |
getConstant(Mul)), OuterTy); |
0 |
| 7894 |
} |
0 |
7894 |
} |
0 |
| 7895 |
} |
--- |
7895 |
} |
--- |
| 7896 |
} |
--- |
7896 |
} |
--- |
| 7897 |
break; |
0 |
7897 |
break; |
0 |
| 7898 |
} |
--- |
7898 |
} |
--- |
| 7899 |
} |
--- |
7899 |
} |
--- |
| 7900 |
} |
--- |
7900 |
} |
--- |
| 7901 |
|
--- |
7901 |
|
--- |
| 7902 |
switch (U->getOpcode()) { |
0 |
7902 |
switch (U->getOpcode()) { |
0 |
| 7903 |
case Instruction::Trunc: |
0 |
7903 |
case Instruction::Trunc: |
0 |
| 7904 |
return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); |
0 |
7904 |
return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); |
0 |
| 7905 |
|
--- |
7905 |
|
--- |
| 7906 |
case Instruction::ZExt: |
0 |
7906 |
case Instruction::ZExt: |
0 |
| 7907 |
return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
0 |
7907 |
return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
0 |
| 7908 |
|
--- |
7908 |
|
--- |
| 7909 |
case Instruction::SExt: |
0 |
7909 |
case Instruction::SExt: |
0 |
| 7910 |
if (auto BO = MatchBinaryOp(U->getOperand(0), getDataLayout(), AC, DT, |
0 |
7910 |
if (auto BO = MatchBinaryOp(U->getOperand(0), getDataLayout(), AC, DT, |
0 |
| 7911 |
dyn_cast(V))) { |
0 |
7911 |
dyn_cast(V))) { |
0 |
| 7912 |
// The NSW flag of a subtract does not always survive the conversion to |
--- |
7912 |
// The NSW flag of a subtract does not always survive the conversion to |
--- |
| 7913 |
// A + (-1)*B. By pushing sign extension onto its operands we are much |
--- |
7913 |
// A + (-1)*B. By pushing sign extension onto its operands we are much |
--- |
| 7914 |
// more likely to preserve NSW and allow later AddRec optimisations. |
--- |
7914 |
// more likely to preserve NSW and allow later AddRec optimisations. |
--- |
| 7915 |
// |
--- |
7915 |
// |
--- |
| 7916 |
// NOTE: This is effectively duplicating this logic from getSignExtend: |
--- |
7916 |
// NOTE: This is effectively duplicating this logic from getSignExtend: |
--- |
| 7917 |
// sext((A + B + ...)) --> (sext(A) + sext(B) + ...) |
--- |
7917 |
// sext((A + B + ...)) --> (sext(A) + sext(B) + ...) |
--- |
| 7918 |
// but by that point the NSW information has potentially been lost. |
--- |
7918 |
// but by that point the NSW information has potentially been lost. |
--- |
| 7919 |
if (BO->Opcode == Instruction::Sub && BO->IsNSW) { |
0 |
7919 |
if (BO->Opcode == Instruction::Sub && BO->IsNSW) { |
0 |
| 7920 |
Type *Ty = U->getType(); |
0 |
7920 |
Type *Ty = U->getType(); |
0 |
| 7921 |
auto *V1 = getSignExtendExpr(getSCEV(BO->LHS), Ty); |
0 |
7921 |
auto *V1 = getSignExtendExpr(getSCEV(BO->LHS), Ty); |
0 |
| 7922 |
auto *V2 = getSignExtendExpr(getSCEV(BO->RHS), Ty); |
0 |
7922 |
auto *V2 = getSignExtendExpr(getSCEV(BO->RHS), Ty); |
0 |
| 7923 |
return getMinusSCEV(V1, V2, SCEV::FlagNSW); |
0 |
7923 |
return getMinusSCEV(V1, V2, SCEV::FlagNSW); |
0 |
| 7924 |
} |
--- |
7924 |
} |
--- |
| 7925 |
} |
--- |
7925 |
} |
--- |
| 7926 |
return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
0 |
7926 |
return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
0 |
| 7927 |
|
--- |
7927 |
|
--- |
| 7928 |
case Instruction::BitCast: |
0 |
7928 |
case Instruction::BitCast: |
0 |
| 7929 |
// BitCasts are no-op casts so we just eliminate the cast. |
--- |
7929 |
// BitCasts are no-op casts so we just eliminate the cast. |
--- |
| 7930 |
if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) |
0 |
7930 |
if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) |
0 |
| 7931 |
return getSCEV(U->getOperand(0)); |
0 |
7931 |
return getSCEV(U->getOperand(0)); |
0 |
| 7932 |
break; |
0 |
7932 |
break; |
0 |
| 7933 |
|
--- |
7933 |
|
--- |
| 7934 |
case Instruction::PtrToInt: { |
0 |
7934 |
case Instruction::PtrToInt: { |
0 |
| 7935 |
// Pointer to integer cast is straight-forward, so do model it. |
--- |
7935 |
// Pointer to integer cast is straight-forward, so do model it. |
--- |
| 7936 |
const SCEV *Op = getSCEV(U->getOperand(0)); |
0 |
7936 |
const SCEV *Op = getSCEV(U->getOperand(0)); |
0 |
| 7937 |
Type *DstIntTy = U->getType(); |
0 |
7937 |
Type *DstIntTy = U->getType(); |
0 |
| 7938 |
// But only if effective SCEV (integer) type is wide enough to represent |
--- |
7938 |
// But only if effective SCEV (integer) type is wide enough to represent |
--- |
| 7939 |
// all possible pointer values. |
--- |
7939 |
// all possible pointer values. |
--- |
| 7940 |
const SCEV *IntOp = getPtrToIntExpr(Op, DstIntTy); |
0 |
7940 |
const SCEV *IntOp = getPtrToIntExpr(Op, DstIntTy); |
0 |
| 7941 |
if (isa(IntOp)) |
0 |
7941 |
if (isa(IntOp)) |
0 |
| 7942 |
return getUnknown(V); |
0 |
7942 |
return getUnknown(V); |
0 |
| 7943 |
return IntOp; |
0 |
7943 |
return IntOp; |
0 |
| 7944 |
} |
--- |
7944 |
} |
--- |
| 7945 |
case Instruction::IntToPtr: |
0 |
7945 |
case Instruction::IntToPtr: |
0 |
| 7946 |
// Just don't deal with inttoptr casts. |
--- |
7946 |
// Just don't deal with inttoptr casts. |
--- |
| 7947 |
return getUnknown(V); |
0 |
7947 |
return getUnknown(V); |
0 |
| 7948 |
|
--- |
7948 |
|
--- |
| 7949 |
case Instruction::SDiv: |
0 |
7949 |
case Instruction::SDiv: |
0 |
| 7950 |
// If both operands are non-negative, this is just an udiv. |
--- |
7950 |
// If both operands are non-negative, this is just an udiv. |
--- |
| 7951 |
if (isKnownNonNegative(getSCEV(U->getOperand(0))) && |
0 |
7951 |
if (isKnownNonNegative(getSCEV(U->getOperand(0))) && |
0 |
| 7952 |
isKnownNonNegative(getSCEV(U->getOperand(1)))) |
0 |
7952 |
isKnownNonNegative(getSCEV(U->getOperand(1)))) |
0 |
| 7953 |
return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(U->getOperand(1))); |
0 |
7953 |
return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(U->getOperand(1))); |
0 |
| 7954 |
break; |
0 |
7954 |
break; |
0 |
| 7955 |
|
--- |
7955 |
|
--- |
| 7956 |
case Instruction::SRem: |
0 |
7956 |
case Instruction::SRem: |
0 |
| 7957 |
// If both operands are non-negative, this is just an urem. |
--- |
7957 |
// If both operands are non-negative, this is just an urem. |
--- |
| 7958 |
if (isKnownNonNegative(getSCEV(U->getOperand(0))) && |
0 |
7958 |
if (isKnownNonNegative(getSCEV(U->getOperand(0))) && |
0 |
| 7959 |
isKnownNonNegative(getSCEV(U->getOperand(1)))) |
0 |
7959 |
isKnownNonNegative(getSCEV(U->getOperand(1)))) |
0 |
| 7960 |
return getURemExpr(getSCEV(U->getOperand(0)), getSCEV(U->getOperand(1))); |
0 |
7960 |
return getURemExpr(getSCEV(U->getOperand(0)), getSCEV(U->getOperand(1))); |
0 |
| 7961 |
break; |
0 |
7961 |
break; |
0 |
| 7962 |
|
--- |
7962 |
|
--- |
| 7963 |
case Instruction::GetElementPtr: |
0 |
7963 |
case Instruction::GetElementPtr: |
0 |
| 7964 |
return createNodeForGEP(cast(U)); |
0 |
7964 |
return createNodeForGEP(cast(U)); |
0 |
| 7965 |
|
--- |
7965 |
|
--- |
| 7966 |
case Instruction::PHI: |
0 |
7966 |
case Instruction::PHI: |
0 |
| 7967 |
return createNodeForPHI(cast(U)); |
0 |
7967 |
return createNodeForPHI(cast(U)); |
0 |
| 7968 |
|
--- |
7968 |
|
--- |
| 7969 |
case Instruction::Select: |
0 |
7969 |
case Instruction::Select: |
0 |
| 7970 |
return createNodeForSelectOrPHI(U, U->getOperand(0), U->getOperand(1), |
0 |
7970 |
return createNodeForSelectOrPHI(U, U->getOperand(0), U->getOperand(1), |
0 |
| 7971 |
U->getOperand(2)); |
0 |
7971 |
U->getOperand(2)); |
0 |
| 7972 |
|
--- |
7972 |
|
--- |
| 7973 |
case Instruction::Call: |
0 |
7973 |
case Instruction::Call: |
0 |
| 7974 |
case Instruction::Invoke: |
--- |
7974 |
case Instruction::Invoke: |
--- |
| 7975 |
if (Value *RV = cast(U)->getReturnedArgOperand()) |
0 |
7975 |
if (Value *RV = cast(U)->getReturnedArgOperand()) |
0 |
| 7976 |
return getSCEV(RV); |
0 |
7976 |
return getSCEV(RV); |
0 |
| 7977 |
|
--- |
7977 |
|
--- |
| 7978 |
if (auto *II = dyn_cast(U)) { |
0 |
7978 |
if (auto *II = dyn_cast(U)) { |
0 |
| 7979 |
switch (II->getIntrinsicID()) { |
0 |
7979 |
switch (II->getIntrinsicID()) { |
0 |
| 7980 |
case Intrinsic::abs: |
0 |
7980 |
case Intrinsic::abs: |
0 |
| 7981 |
return getAbsExpr( |
0 |
7981 |
return getAbsExpr( |
0 |
| 7982 |
getSCEV(II->getArgOperand(0)), |
--- |
7982 |
getSCEV(II->getArgOperand(0)), |
--- |
| 7983 |
/*IsNSW=*/cast(II->getArgOperand(1))->isOne()); |
0 |
7983 |
/*IsNSW=*/cast(II->getArgOperand(1))->isOne()); |
0 |
| 7984 |
case Intrinsic::umax: |
0 |
7984 |
case Intrinsic::umax: |
0 |
| 7985 |
LHS = getSCEV(II->getArgOperand(0)); |
0 |
7985 |
LHS = getSCEV(II->getArgOperand(0)); |
0 |
| 7986 |
RHS = getSCEV(II->getArgOperand(1)); |
0 |
7986 |
RHS = getSCEV(II->getArgOperand(1)); |
0 |
| 7987 |
return getUMaxExpr(LHS, RHS); |
0 |
7987 |
return getUMaxExpr(LHS, RHS); |
0 |
| 7988 |
case Intrinsic::umin: |
0 |
7988 |
case Intrinsic::umin: |
0 |
| 7989 |
LHS = getSCEV(II->getArgOperand(0)); |
0 |
7989 |
LHS = getSCEV(II->getArgOperand(0)); |
0 |
| 7990 |
RHS = getSCEV(II->getArgOperand(1)); |
0 |
7990 |
RHS = getSCEV(II->getArgOperand(1)); |
0 |
| 7991 |
return getUMinExpr(LHS, RHS); |
0 |
7991 |
return getUMinExpr(LHS, RHS); |
0 |
| 7992 |
case Intrinsic::smax: |
0 |
7992 |
case Intrinsic::smax: |
0 |
| 7993 |
LHS = getSCEV(II->getArgOperand(0)); |
0 |
7993 |
LHS = getSCEV(II->getArgOperand(0)); |
0 |
| 7994 |
RHS = getSCEV(II->getArgOperand(1)); |
0 |
7994 |
RHS = getSCEV(II->getArgOperand(1)); |
0 |
| 7995 |
return getSMaxExpr(LHS, RHS); |
0 |
7995 |
return getSMaxExpr(LHS, RHS); |
0 |
| 7996 |
case Intrinsic::smin: |
0 |
7996 |
case Intrinsic::smin: |
0 |
| 7997 |
LHS = getSCEV(II->getArgOperand(0)); |
0 |
7997 |
LHS = getSCEV(II->getArgOperand(0)); |
0 |
| 7998 |
RHS = getSCEV(II->getArgOperand(1)); |
0 |
7998 |
RHS = getSCEV(II->getArgOperand(1)); |
0 |
| 7999 |
return getSMinExpr(LHS, RHS); |
0 |
7999 |
return getSMinExpr(LHS, RHS); |
0 |
| 8000 |
case Intrinsic::usub_sat: { |
0 |
8000 |
case Intrinsic::usub_sat: { |
0 |
| 8001 |
const SCEV *X = getSCEV(II->getArgOperand(0)); |
0 |
8001 |
const SCEV *X = getSCEV(II->getArgOperand(0)); |
0 |
| 8002 |
const SCEV *Y = getSCEV(II->getArgOperand(1)); |
0 |
8002 |
const SCEV *Y = getSCEV(II->getArgOperand(1)); |
0 |
| 8003 |
const SCEV *ClampedY = getUMinExpr(X, Y); |
0 |
8003 |
const SCEV *ClampedY = getUMinExpr(X, Y); |
0 |
| 8004 |
return getMinusSCEV(X, ClampedY, SCEV::FlagNUW); |
0 |
8004 |
return getMinusSCEV(X, ClampedY, SCEV::FlagNUW); |
0 |
| 8005 |
} |
--- |
8005 |
} |
--- |
| 8006 |
case Intrinsic::uadd_sat: { |
0 |
8006 |
case Intrinsic::uadd_sat: { |
0 |
| 8007 |
const SCEV *X = getSCEV(II->getArgOperand(0)); |
0 |
8007 |
const SCEV *X = getSCEV(II->getArgOperand(0)); |
0 |
| 8008 |
const SCEV *Y = getSCEV(II->getArgOperand(1)); |
0 |
8008 |
const SCEV *Y = getSCEV(II->getArgOperand(1)); |
0 |
| 8009 |
const SCEV *ClampedX = getUMinExpr(X, getNotSCEV(Y)); |
0 |
8009 |
const SCEV *ClampedX = getUMinExpr(X, getNotSCEV(Y)); |
0 |
| 8010 |
return getAddExpr(ClampedX, Y, SCEV::FlagNUW); |
0 |
8010 |
return getAddExpr(ClampedX, Y, SCEV::FlagNUW); |
0 |
| 8011 |
} |
--- |
8011 |
} |
--- |
| 8012 |
case Intrinsic::start_loop_iterations: |
0 |
8012 |
case Intrinsic::start_loop_iterations: |
0 |
| 8013 |
case Intrinsic::annotation: |
--- |
8013 |
case Intrinsic::annotation: |
--- |
| 8014 |
case Intrinsic::ptr_annotation: |
--- |
8014 |
case Intrinsic::ptr_annotation: |
--- |
| 8015 |
// A start_loop_iterations or llvm.annotation or llvm.prt.annotation is |
--- |
8015 |
// A start_loop_iterations or llvm.annotation or llvm.prt.annotation is |
--- |
| 8016 |
// just eqivalent to the first operand for SCEV purposes. |
--- |
8016 |
// just eqivalent to the first operand for SCEV purposes. |
--- |
| 8017 |
return getSCEV(II->getArgOperand(0)); |
0 |
8017 |
return getSCEV(II->getArgOperand(0)); |
0 |
| 8018 |
case Intrinsic::vscale: |
0 |
8018 |
case Intrinsic::vscale: |
0 |
| 8019 |
return getVScale(II->getType()); |
0 |
8019 |
return getVScale(II->getType()); |
0 |
| 8020 |
default: |
0 |
8020 |
default: |
0 |
| 8021 |
break; |
0 |
8021 |
break; |
0 |
| 8022 |
} |
--- |
8022 |
} |
--- |
| 8023 |
} |
--- |
8023 |
} |
--- |
| 8024 |
break; |
0 |
8024 |
break; |
0 |
| 8025 |
} |
--- |
8025 |
} |
--- |
| 8026 |
|
--- |
8026 |
|
--- |
| 8027 |
return getUnknown(V); |
0 |
8027 |
return getUnknown(V); |
0 |
| 8028 |
} |
--- |
8028 |
} |
--- |
| 8029 |
|
--- |
8029 |
|
--- |
| 8030 |
//===----------------------------------------------------------------------===// |
--- |
8030 |
//===----------------------------------------------------------------------===// |
--- |
| 8031 |
// Iteration Count Computation Code |
--- |
8031 |
// Iteration Count Computation Code |
--- |
| 8032 |
// |
--- |
8032 |
// |
--- |
| 8033 |
|
--- |
8033 |
|
--- |
| 8034 |
const SCEV *ScalarEvolution::getTripCountFromExitCount(const SCEV *ExitCount) { |
0 |
8034 |
const SCEV *ScalarEvolution::getTripCountFromExitCount(const SCEV *ExitCount) { |
0 |
| 8035 |
if (isa(ExitCount)) |
0 |
8035 |
if (isa(ExitCount)) |
0 |
| 8036 |
return getCouldNotCompute(); |
0 |
8036 |
return getCouldNotCompute(); |
0 |
| 8037 |
|
--- |
8037 |
|
--- |
| 8038 |
auto *ExitCountType = ExitCount->getType(); |
0 |
8038 |
auto *ExitCountType = ExitCount->getType(); |
0 |
| 8039 |
assert(ExitCountType->isIntegerTy()); |
0 |
8039 |
assert(ExitCountType->isIntegerTy()); |
0 |
| 8040 |
auto *EvalTy = Type::getIntNTy(ExitCountType->getContext(), |
0 |
8040 |
auto *EvalTy = Type::getIntNTy(ExitCountType->getContext(), |
0 |
| 8041 |
1 + ExitCountType->getScalarSizeInBits()); |
0 |
8041 |
1 + ExitCountType->getScalarSizeInBits()); |
0 |
| 8042 |
return getTripCountFromExitCount(ExitCount, EvalTy, nullptr); |
0 |
8042 |
return getTripCountFromExitCount(ExitCount, EvalTy, nullptr); |
0 |
| 8043 |
} |
--- |
8043 |
} |
--- |
| 8044 |
|
--- |
8044 |
|
--- |
| 8045 |
const SCEV *ScalarEvolution::getTripCountFromExitCount(const SCEV *ExitCount, |
0 |
8045 |
const SCEV *ScalarEvolution::getTripCountFromExitCount(const SCEV *ExitCount, |
0 |
| 8046 |
Type *EvalTy, |
--- |
8046 |
Type *EvalTy, |
--- |
| 8047 |
const Loop *L) { |
--- |
8047 |
const Loop *L) { |
--- |
| 8048 |
if (isa(ExitCount)) |
0 |
8048 |
if (isa(ExitCount)) |
0 |
| 8049 |
return getCouldNotCompute(); |
0 |
8049 |
return getCouldNotCompute(); |
0 |
| 8050 |
|
--- |
8050 |
|
--- |
| 8051 |
unsigned ExitCountSize = getTypeSizeInBits(ExitCount->getType()); |
0 |
8051 |
unsigned ExitCountSize = getTypeSizeInBits(ExitCount->getType()); |
0 |
| 8052 |
unsigned EvalSize = EvalTy->getPrimitiveSizeInBits(); |
0 |
8052 |
unsigned EvalSize = EvalTy->getPrimitiveSizeInBits(); |
0 |
| 8053 |
|
--- |
8053 |
|
--- |
| 8054 |
auto CanAddOneWithoutOverflow = [&]() { |
0 |
8054 |
auto CanAddOneWithoutOverflow = [&]() { |
0 |
| 8055 |
ConstantRange ExitCountRange = |
--- |
8055 |
ConstantRange ExitCountRange = |
--- |
| 8056 |
getRangeRef(ExitCount, RangeSignHint::HINT_RANGE_UNSIGNED); |
0 |
8056 |
getRangeRef(ExitCount, RangeSignHint::HINT_RANGE_UNSIGNED); |
0 |
| 8057 |
if (!ExitCountRange.contains(APInt::getMaxValue(ExitCountSize))) |
0 |
8057 |
if (!ExitCountRange.contains(APInt::getMaxValue(ExitCountSize))) |
0 |
| 8058 |
return true; |
0 |
8058 |
return true; |
0 |
| 8059 |
|
--- |
8059 |
|
--- |
| 8060 |
return L && isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, ExitCount, |
0 |
8060 |
return L && isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, ExitCount, |
0 |
| 8061 |
getMinusOne(ExitCount->getType())); |
0 |
8061 |
getMinusOne(ExitCount->getType())); |
0 |
| 8062 |
}; |
0 |
8062 |
}; |
0 |
| 8063 |
|
--- |
8063 |
|
--- |
| 8064 |
// If we need to zero extend the backedge count, check if we can add one to |
--- |
8064 |
// If we need to zero extend the backedge count, check if we can add one to |
--- |
| 8065 |
// it prior to zero extending without overflow. Provided this is safe, it |
--- |
8065 |
// it prior to zero extending without overflow. Provided this is safe, it |
--- |
| 8066 |
// allows better simplification of the +1. |
--- |
8066 |
// allows better simplification of the +1. |
--- |
| 8067 |
if (EvalSize > ExitCountSize && CanAddOneWithoutOverflow()) |
0 |
8067 |
if (EvalSize > ExitCountSize && CanAddOneWithoutOverflow()) |
0 |
| 8068 |
return getZeroExtendExpr( |
0 |
8068 |
return getZeroExtendExpr( |
0 |
| 8069 |
getAddExpr(ExitCount, getOne(ExitCount->getType())), EvalTy); |
0 |
8069 |
getAddExpr(ExitCount, getOne(ExitCount->getType())), EvalTy); |
0 |
| 8070 |
|
--- |
8070 |
|
--- |
| 8071 |
// Get the total trip count from the count by adding 1. This may wrap. |
--- |
8071 |
// Get the total trip count from the count by adding 1. This may wrap. |
--- |
| 8072 |
return getAddExpr(getTruncateOrZeroExtend(ExitCount, EvalTy), getOne(EvalTy)); |
0 |
8072 |
return getAddExpr(getTruncateOrZeroExtend(ExitCount, EvalTy), getOne(EvalTy)); |
0 |
| 8073 |
} |
--- |
8073 |
} |
--- |
| 8074 |
|
--- |
8074 |
|
--- |
| 8075 |
static unsigned getConstantTripCount(const SCEVConstant *ExitCount) { |
0 |
8075 |
static unsigned getConstantTripCount(const SCEVConstant *ExitCount) { |
0 |
| 8076 |
if (!ExitCount) |
0 |
8076 |
if (!ExitCount) |
0 |
| 8077 |
return 0; |
0 |
8077 |
return 0; |
0 |
| 8078 |
|
--- |
8078 |
|
--- |
| 8079 |
ConstantInt *ExitConst = ExitCount->getValue(); |
0 |
8079 |
ConstantInt *ExitConst = ExitCount->getValue(); |
0 |
| 8080 |
|
--- |
8080 |
|
--- |
| 8081 |
// Guard against huge trip counts. |
--- |
8081 |
// Guard against huge trip counts. |
--- |
| 8082 |
if (ExitConst->getValue().getActiveBits() > 32) |
0 |
8082 |
if (ExitConst->getValue().getActiveBits() > 32) |
0 |
| 8083 |
return 0; |
0 |
8083 |
return 0; |
0 |
| 8084 |
|
--- |
8084 |
|
--- |
| 8085 |
// In case of integer overflow, this returns 0, which is correct. |
--- |
8085 |
// In case of integer overflow, this returns 0, which is correct. |
--- |
| 8086 |
return ((unsigned)ExitConst->getZExtValue()) + 1; |
0 |
8086 |
return ((unsigned)ExitConst->getZExtValue()) + 1; |
0 |
| 8087 |
} |
--- |
8087 |
} |
--- |
| 8088 |
|
--- |
8088 |
|
--- |
| 8089 |
unsigned ScalarEvolution::getSmallConstantTripCount(const Loop *L) { |
0 |
8089 |
unsigned ScalarEvolution::getSmallConstantTripCount(const Loop *L) { |
0 |
| 8090 |
auto *ExitCount = dyn_cast(getBackedgeTakenCount(L, Exact)); |
0 |
8090 |
auto *ExitCount = dyn_cast(getBackedgeTakenCount(L, Exact)); |
0 |
| 8091 |
return getConstantTripCount(ExitCount); |
0 |
8091 |
return getConstantTripCount(ExitCount); |
0 |
| 8092 |
} |
--- |
8092 |
} |
--- |
| 8093 |
|
--- |
8093 |
|
--- |
| 8094 |
unsigned |
--- |
8094 |
unsigned |
--- |
| 8095 |
ScalarEvolution::getSmallConstantTripCount(const Loop *L, |
0 |
8095 |
ScalarEvolution::getSmallConstantTripCount(const Loop *L, |
0 |
| 8096 |
const BasicBlock *ExitingBlock) { |
--- |
8096 |
const BasicBlock *ExitingBlock) { |
--- |
| 8097 |
assert(ExitingBlock && "Must pass a non-null exiting block!"); |
0 |
8097 |
assert(ExitingBlock && "Must pass a non-null exiting block!"); |
0 |
| 8098 |
assert(L->isLoopExiting(ExitingBlock) && |
0 |
8098 |
assert(L->isLoopExiting(ExitingBlock) && |
0 |
| 8099 |
"Exiting block must actually branch out of the loop!"); |
--- |
8099 |
"Exiting block must actually branch out of the loop!"); |
--- |
| 8100 |
const SCEVConstant *ExitCount = |
--- |
8100 |
const SCEVConstant *ExitCount = |
--- |
| 8101 |
dyn_cast(getExitCount(L, ExitingBlock)); |
0 |
8101 |
dyn_cast(getExitCount(L, ExitingBlock)); |
0 |
| 8102 |
return getConstantTripCount(ExitCount); |
0 |
8102 |
return getConstantTripCount(ExitCount); |
0 |
| 8103 |
} |
--- |
8103 |
} |
--- |
| 8104 |
|
--- |
8104 |
|
--- |
| 8105 |
unsigned ScalarEvolution::getSmallConstantMaxTripCount(const Loop *L) { |
0 |
8105 |
unsigned ScalarEvolution::getSmallConstantMaxTripCount(const Loop *L) { |
0 |
| 8106 |
const auto *MaxExitCount = |
--- |
8106 |
const auto *MaxExitCount = |
--- |
| 8107 |
dyn_cast(getConstantMaxBackedgeTakenCount(L)); |
0 |
8107 |
dyn_cast(getConstantMaxBackedgeTakenCount(L)); |
0 |
| 8108 |
return getConstantTripCount(MaxExitCount); |
0 |
8108 |
return getConstantTripCount(MaxExitCount); |
0 |
| 8109 |
} |
--- |
8109 |
} |
--- |
| 8110 |
|
--- |
8110 |
|
--- |
| 8111 |
unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L) { |
0 |
8111 |
unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L) { |
0 |
| 8112 |
SmallVector ExitingBlocks; |
0 |
8112 |
SmallVector ExitingBlocks; |
0 |
| 8113 |
L->getExitingBlocks(ExitingBlocks); |
0 |
8113 |
L->getExitingBlocks(ExitingBlocks); |
0 |
| 8114 |
|
--- |
8114 |
|
--- |
| 8115 |
std::optional Res; |
0 |
8115 |
std::optional Res; |
0 |
| 8116 |
for (auto *ExitingBB : ExitingBlocks) { |
0 |
8116 |
for (auto *ExitingBB : ExitingBlocks) { |
0 |
| 8117 |
unsigned Multiple = getSmallConstantTripMultiple(L, ExitingBB); |
0 |
8117 |
unsigned Multiple = getSmallConstantTripMultiple(L, ExitingBB); |
0 |
| 8118 |
if (!Res) |
0 |
8118 |
if (!Res) |
0 |
| 8119 |
Res = Multiple; |
0 |
8119 |
Res = Multiple; |
0 |
| 8120 |
Res = (unsigned)std::gcd(*Res, Multiple); |
0 |
8120 |
Res = (unsigned)std::gcd(*Res, Multiple); |
0 |
| 8121 |
} |
--- |
8121 |
} |
--- |
| 8122 |
return Res.value_or(1); |
0 |
8122 |
return Res.value_or(1); |
0 |
| 8123 |
} |
0 |
8123 |
} |
0 |
| 8124 |
|
--- |
8124 |
|
--- |
| 8125 |
unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L, |
0 |
8125 |
unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L, |
0 |
| 8126 |
const SCEV *ExitCount) { |
--- |
8126 |
const SCEV *ExitCount) { |
--- |
| 8127 |
if (ExitCount == getCouldNotCompute()) |
0 |
8127 |
if (ExitCount == getCouldNotCompute()) |
0 |
| 8128 |
return 1; |
0 |
8128 |
return 1; |
0 |
| 8129 |
|
--- |
8129 |
|
--- |
| 8130 |
// Get the trip count |
--- |
8130 |
// Get the trip count |
--- |
| 8131 |
const SCEV *TCExpr = getTripCountFromExitCount(applyLoopGuards(ExitCount, L)); |
0 |
8131 |
const SCEV *TCExpr = getTripCountFromExitCount(applyLoopGuards(ExitCount, L)); |
0 |
| 8132 |
|
--- |
8132 |
|
--- |
| 8133 |
APInt Multiple = getNonZeroConstantMultiple(TCExpr); |
0 |
8133 |
APInt Multiple = getNonZeroConstantMultiple(TCExpr); |
0 |
| 8134 |
// If a trip multiple is huge (>=2^32), the trip count is still divisible by |
--- |
8134 |
// If a trip multiple is huge (>=2^32), the trip count is still divisible by |
--- |
| 8135 |
// the greatest power of 2 divisor less than 2^32. |
--- |
8135 |
// the greatest power of 2 divisor less than 2^32. |
--- |
| 8136 |
return Multiple.getActiveBits() > 32 |
0 |
8136 |
return Multiple.getActiveBits() > 32 |
0 |
| 8137 |
? 1U << std::min((unsigned)31, Multiple.countTrailingZeros()) |
0 |
8137 |
? 1U << std::min((unsigned)31, Multiple.countTrailingZeros()) |
0 |
| 8138 |
: (unsigned)Multiple.zextOrTrunc(32).getZExtValue(); |
0 |
8138 |
: (unsigned)Multiple.zextOrTrunc(32).getZExtValue(); |
0 |
| 8139 |
} |
0 |
8139 |
} |
0 |
| 8140 |
|
--- |
8140 |
|
--- |
| 8141 |
/// Returns the largest constant divisor of the trip count of this loop as a |
--- |
8141 |
/// Returns the largest constant divisor of the trip count of this loop as a |
--- |
| 8142 |
/// normal unsigned value, if possible. This means that the actual trip count is |
--- |
8142 |
/// normal unsigned value, if possible. This means that the actual trip count is |
--- |
| 8143 |
/// always a multiple of the returned value (don't forget the trip count could |
--- |
8143 |
/// always a multiple of the returned value (don't forget the trip count could |
--- |
| 8144 |
/// very well be zero as well!). |
--- |
8144 |
/// very well be zero as well!). |
--- |
| 8145 |
/// |
--- |
8145 |
/// |
--- |
| 8146 |
/// Returns 1 if the trip count is unknown or not guaranteed to be the |
--- |
8146 |
/// Returns 1 if the trip count is unknown or not guaranteed to be the |
--- |
| 8147 |
/// multiple of a constant (which is also the case if the trip count is simply |
--- |
8147 |
/// multiple of a constant (which is also the case if the trip count is simply |
--- |
| 8148 |
/// constant, use getSmallConstantTripCount for that case), Will also return 1 |
--- |
8148 |
/// constant, use getSmallConstantTripCount for that case), Will also return 1 |
--- |
| 8149 |
/// if the trip count is very large (>= 2^32). |
--- |
8149 |
/// if the trip count is very large (>= 2^32). |
--- |
| 8150 |
/// |
--- |
8150 |
/// |
--- |
| 8151 |
/// As explained in the comments for getSmallConstantTripCount, this assumes |
--- |
8151 |
/// As explained in the comments for getSmallConstantTripCount, this assumes |
--- |
| 8152 |
/// that control exits the loop via ExitingBlock. |
--- |
8152 |
/// that control exits the loop via ExitingBlock. |
--- |
| 8153 |
unsigned |
--- |
8153 |
unsigned |
--- |
| 8154 |
ScalarEvolution::getSmallConstantTripMultiple(const Loop *L, |
0 |
8154 |
ScalarEvolution::getSmallConstantTripMultiple(const Loop *L, |
0 |
| 8155 |
const BasicBlock *ExitingBlock) { |
--- |
8155 |
const BasicBlock *ExitingBlock) { |
--- |
| 8156 |
assert(ExitingBlock && "Must pass a non-null exiting block!"); |
0 |
8156 |
assert(ExitingBlock && "Must pass a non-null exiting block!"); |
0 |
| 8157 |
assert(L->isLoopExiting(ExitingBlock) && |
0 |
8157 |
assert(L->isLoopExiting(ExitingBlock) && |
0 |
| 8158 |
"Exiting block must actually branch out of the loop!"); |
--- |
8158 |
"Exiting block must actually branch out of the loop!"); |
--- |
| 8159 |
const SCEV *ExitCount = getExitCount(L, ExitingBlock); |
0 |
8159 |
const SCEV *ExitCount = getExitCount(L, ExitingBlock); |
0 |
| 8160 |
return getSmallConstantTripMultiple(L, ExitCount); |
0 |
8160 |
return getSmallConstantTripMultiple(L, ExitCount); |
0 |
| 8161 |
} |
--- |
8161 |
} |
--- |
| 8162 |
|
--- |
8162 |
|
--- |
| 8163 |
const SCEV *ScalarEvolution::getExitCount(const Loop *L, |
0 |
8163 |
const SCEV *ScalarEvolution::getExitCount(const Loop *L, |
0 |
| 8164 |
const BasicBlock *ExitingBlock, |
--- |
8164 |
const BasicBlock *ExitingBlock, |
--- |
| 8165 |
ExitCountKind Kind) { |
--- |
8165 |
ExitCountKind Kind) { |
--- |
| 8166 |
switch (Kind) { |
0 |
8166 |
switch (Kind) { |
0 |
| 8167 |
case Exact: |
0 |
8167 |
case Exact: |
0 |
| 8168 |
return getBackedgeTakenInfo(L).getExact(ExitingBlock, this); |
0 |
8168 |
return getBackedgeTakenInfo(L).getExact(ExitingBlock, this); |
0 |
| 8169 |
case SymbolicMaximum: |
0 |
8169 |
case SymbolicMaximum: |
0 |
| 8170 |
return getBackedgeTakenInfo(L).getSymbolicMax(ExitingBlock, this); |
0 |
8170 |
return getBackedgeTakenInfo(L).getSymbolicMax(ExitingBlock, this); |
0 |
| 8171 |
case ConstantMaximum: |
0 |
8171 |
case ConstantMaximum: |
0 |
| 8172 |
return getBackedgeTakenInfo(L).getConstantMax(ExitingBlock, this); |
0 |
8172 |
return getBackedgeTakenInfo(L).getConstantMax(ExitingBlock, this); |
0 |
| 8173 |
}; |
--- |
8173 |
}; |
--- |
| 8174 |
llvm_unreachable("Invalid ExitCountKind!"); |
0 |
8174 |
llvm_unreachable("Invalid ExitCountKind!"); |
0 |
| 8175 |
} |
--- |
8175 |
} |
--- |
| 8176 |
|
--- |
8176 |
|
--- |
| 8177 |
const SCEV * |
--- |
8177 |
const SCEV * |
--- |
| 8178 |
ScalarEvolution::getPredicatedBackedgeTakenCount(const Loop *L, |
0 |
8178 |
ScalarEvolution::getPredicatedBackedgeTakenCount(const Loop *L, |
0 |
| 8179 |
SmallVector &Preds) { |
--- |
8179 |
SmallVector &Preds) { |
--- |
| 8180 |
return getPredicatedBackedgeTakenInfo(L).getExact(L, this, &Preds); |
0 |
8180 |
return getPredicatedBackedgeTakenInfo(L).getExact(L, this, &Preds); |
0 |
| 8181 |
} |
--- |
8181 |
} |
--- |
| 8182 |
|
--- |
8182 |
|
--- |
| 8183 |
const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L, |
0 |
8183 |
const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L, |
0 |
| 8184 |
ExitCountKind Kind) { |
--- |
8184 |
ExitCountKind Kind) { |
--- |
| 8185 |
switch (Kind) { |
0 |
8185 |
switch (Kind) { |
0 |
| 8186 |
case Exact: |
0 |
8186 |
case Exact: |
0 |
| 8187 |
return getBackedgeTakenInfo(L).getExact(L, this); |
0 |
8187 |
return getBackedgeTakenInfo(L).getExact(L, this); |
0 |
| 8188 |
case ConstantMaximum: |
0 |
8188 |
case ConstantMaximum: |
0 |
| 8189 |
return getBackedgeTakenInfo(L).getConstantMax(this); |
0 |
8189 |
return getBackedgeTakenInfo(L).getConstantMax(this); |
0 |
| 8190 |
case SymbolicMaximum: |
0 |
8190 |
case SymbolicMaximum: |
0 |
| 8191 |
return getBackedgeTakenInfo(L).getSymbolicMax(L, this); |
0 |
8191 |
return getBackedgeTakenInfo(L).getSymbolicMax(L, this); |
0 |
| 8192 |
}; |
--- |
8192 |
}; |
--- |
| 8193 |
llvm_unreachable("Invalid ExitCountKind!"); |
0 |
8193 |
llvm_unreachable("Invalid ExitCountKind!"); |
0 |
| 8194 |
} |
--- |
8194 |
} |
--- |
| 8195 |
|
--- |
8195 |
|
--- |
| 8196 |
bool ScalarEvolution::isBackedgeTakenCountMaxOrZero(const Loop *L) { |
0 |
8196 |
bool ScalarEvolution::isBackedgeTakenCountMaxOrZero(const Loop *L) { |
0 |
| 8197 |
return getBackedgeTakenInfo(L).isConstantMaxOrZero(this); |
0 |
8197 |
return getBackedgeTakenInfo(L).isConstantMaxOrZero(this); |
0 |
| 8198 |
} |
--- |
8198 |
} |
--- |
| 8199 |
|
--- |
8199 |
|
--- |
| 8200 |
/// Push PHI nodes in the header of the given loop onto the given Worklist. |
--- |
8200 |
/// Push PHI nodes in the header of the given loop onto the given Worklist. |
--- |
| 8201 |
static void PushLoopPHIs(const Loop *L, |
0 |
8201 |
static void PushLoopPHIs(const Loop *L, |
0 |
| 8202 |
SmallVectorImpl &Worklist, |
--- |
8202 |
SmallVectorImpl &Worklist, |
--- |
| 8203 |
SmallPtrSetImpl &Visited) { |
--- |
8203 |
SmallPtrSetImpl &Visited) { |
--- |
| 8204 |
BasicBlock *Header = L->getHeader(); |
0 |
8204 |
BasicBlock *Header = L->getHeader(); |
0 |
| 8205 |
|
--- |
8205 |
|
--- |
| 8206 |
// Push all Loop-header PHIs onto the Worklist stack. |
--- |
8206 |
// Push all Loop-header PHIs onto the Worklist stack. |
--- |
| 8207 |
for (PHINode &PN : Header->phis()) |
0 |
8207 |
for (PHINode &PN : Header->phis()) |
0 |
| 8208 |
if (Visited.insert(&PN).second) |
0 |
8208 |
if (Visited.insert(&PN).second) |
0 |
| 8209 |
Worklist.push_back(&PN); |
0 |
8209 |
Worklist.push_back(&PN); |
0 |
| 8210 |
} |
0 |
8210 |
} |
0 |
| 8211 |
|
--- |
8211 |
|
--- |
| 8212 |
const ScalarEvolution::BackedgeTakenInfo & |
--- |
8212 |
const ScalarEvolution::BackedgeTakenInfo & |
--- |
| 8213 |
ScalarEvolution::getPredicatedBackedgeTakenInfo(const Loop *L) { |
0 |
8213 |
ScalarEvolution::getPredicatedBackedgeTakenInfo(const Loop *L) { |
0 |
| 8214 |
auto &BTI = getBackedgeTakenInfo(L); |
0 |
8214 |
auto &BTI = getBackedgeTakenInfo(L); |
0 |
| 8215 |
if (BTI.hasFullInfo()) |
0 |
8215 |
if (BTI.hasFullInfo()) |
0 |
| 8216 |
return BTI; |
0 |
8216 |
return BTI; |
0 |
| 8217 |
|
--- |
8217 |
|
--- |
| 8218 |
auto Pair = PredicatedBackedgeTakenCounts.insert({L, BackedgeTakenInfo()}); |
0 |
8218 |
auto Pair = PredicatedBackedgeTakenCounts.insert({L, BackedgeTakenInfo()}); |
0 |
| 8219 |
|
--- |
8219 |
|
--- |
| 8220 |
if (!Pair.second) |
0 |
8220 |
if (!Pair.second) |
0 |
| 8221 |
return Pair.first->second; |
0 |
8221 |
return Pair.first->second; |
0 |
| 8222 |
|
--- |
8222 |
|
--- |
| 8223 |
BackedgeTakenInfo Result = |
--- |
8223 |
BackedgeTakenInfo Result = |
--- |
| 8224 |
computeBackedgeTakenCount(L, /*AllowPredicates=*/true); |
0 |
8224 |
computeBackedgeTakenCount(L, /*AllowPredicates=*/true); |
0 |
| 8225 |
|
--- |
8225 |
|
--- |
| 8226 |
return PredicatedBackedgeTakenCounts.find(L)->second = std::move(Result); |
0 |
8226 |
return PredicatedBackedgeTakenCounts.find(L)->second = std::move(Result); |
0 |
| 8227 |
} |
0 |
8227 |
} |
0 |
| 8228 |
|
--- |
8228 |
|
--- |
| 8229 |
ScalarEvolution::BackedgeTakenInfo & |
--- |
8229 |
ScalarEvolution::BackedgeTakenInfo & |
--- |
| 8230 |
ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { |
0 |
8230 |
ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { |
0 |
| 8231 |
// Initially insert an invalid entry for this loop. If the insertion |
--- |
8231 |
// Initially insert an invalid entry for this loop. If the insertion |
--- |
| 8232 |
// succeeds, proceed to actually compute a backedge-taken count and |
--- |
8232 |
// succeeds, proceed to actually compute a backedge-taken count and |
--- |
| 8233 |
// update the value. The temporary CouldNotCompute value tells SCEV |
--- |
8233 |
// update the value. The temporary CouldNotCompute value tells SCEV |
--- |
| 8234 |
// code elsewhere that it shouldn't attempt to request a new |
--- |
8234 |
// code elsewhere that it shouldn't attempt to request a new |
--- |
| 8235 |
// backedge-taken count, which could result in infinite recursion. |
--- |
8235 |
// backedge-taken count, which could result in infinite recursion. |
--- |
| 8236 |
std::pair::iterator, bool> Pair = |
--- |
8236 |
std::pair::iterator, bool> Pair = |
--- |
| 8237 |
BackedgeTakenCounts.insert({L, BackedgeTakenInfo()}); |
0 |
8237 |
BackedgeTakenCounts.insert({L, BackedgeTakenInfo()}); |
0 |
| 8238 |
if (!Pair.second) |
0 |
8238 |
if (!Pair.second) |
0 |
| 8239 |
return Pair.first->second; |
0 |
8239 |
return Pair.first->second; |
0 |
| 8240 |
|
--- |
8240 |
|
--- |
| 8241 |
// computeBackedgeTakenCount may allocate memory for its result. Inserting it |
--- |
8241 |
// computeBackedgeTakenCount may allocate memory for its result. Inserting it |
--- |
| 8242 |
// into the BackedgeTakenCounts map transfers ownership. Otherwise, the result |
--- |
8242 |
// into the BackedgeTakenCounts map transfers ownership. Otherwise, the result |
--- |
| 8243 |
// must be cleared in this scope. |
--- |
8243 |
// must be cleared in this scope. |
--- |
| 8244 |
BackedgeTakenInfo Result = computeBackedgeTakenCount(L); |
0 |
8244 |
BackedgeTakenInfo Result = computeBackedgeTakenCount(L); |
0 |
| 8245 |
|
--- |
8245 |
|
--- |
| 8246 |
// Now that we know more about the trip count for this loop, forget any |
--- |
8246 |
// Now that we know more about the trip count for this loop, forget any |
--- |
| 8247 |
// existing SCEV values for PHI nodes in this loop since they are only |
--- |
8247 |
// existing SCEV values for PHI nodes in this loop since they are only |
--- |
| 8248 |
// conservative estimates made without the benefit of trip count |
--- |
8248 |
// conservative estimates made without the benefit of trip count |
--- |
| 8249 |
// information. This invalidation is not necessary for correctness, and is |
--- |
8249 |
// information. This invalidation is not necessary for correctness, and is |
--- |
| 8250 |
// only done to produce more precise results. |
--- |
8250 |
// only done to produce more precise results. |
--- |
| 8251 |
if (Result.hasAnyInfo()) { |
0 |
8251 |
if (Result.hasAnyInfo()) { |
0 |
| 8252 |
// Invalidate any expression using an addrec in this loop. |
--- |
8252 |
// Invalidate any expression using an addrec in this loop. |
--- |
| 8253 |
SmallVector ToForget; |
0 |
8253 |
SmallVector ToForget; |
0 |
| 8254 |
auto LoopUsersIt = LoopUsers.find(L); |
0 |
8254 |
auto LoopUsersIt = LoopUsers.find(L); |
0 |
| 8255 |
if (LoopUsersIt != LoopUsers.end()) |
0 |
8255 |
if (LoopUsersIt != LoopUsers.end()) |
0 |
| 8256 |
append_range(ToForget, LoopUsersIt->second); |
0 |
8256 |
append_range(ToForget, LoopUsersIt->second); |
0 |
| 8257 |
forgetMemoizedResults(ToForget); |
0 |
8257 |
forgetMemoizedResults(ToForget); |
0 |
| 8258 |
|
--- |
8258 |
|
--- |
| 8259 |
// Invalidate constant-evolved loop header phis. |
--- |
8259 |
// Invalidate constant-evolved loop header phis. |
--- |
| 8260 |
for (PHINode &PN : L->getHeader()->phis()) |
0 |
8260 |
for (PHINode &PN : L->getHeader()->phis()) |
0 |
| 8261 |
ConstantEvolutionLoopExitValue.erase(&PN); |
0 |
8261 |
ConstantEvolutionLoopExitValue.erase(&PN); |
0 |
| 8262 |
} |
0 |
8262 |
} |
0 |
| 8263 |
|
--- |
8263 |
|
--- |
| 8264 |
// Re-lookup the insert position, since the call to |
--- |
8264 |
// Re-lookup the insert position, since the call to |
--- |
| 8265 |
// computeBackedgeTakenCount above could result in a |
--- |
8265 |
// computeBackedgeTakenCount above could result in a |
--- |
| 8266 |
// recusive call to getBackedgeTakenInfo (on a different |
--- |
8266 |
// recusive call to getBackedgeTakenInfo (on a different |
--- |
| 8267 |
// loop), which would invalidate the iterator computed |
--- |
8267 |
// loop), which would invalidate the iterator computed |
--- |
| 8268 |
// earlier. |
--- |
8268 |
// earlier. |
--- |
| 8269 |
return BackedgeTakenCounts.find(L)->second = std::move(Result); |
0 |
8269 |
return BackedgeTakenCounts.find(L)->second = std::move(Result); |
0 |
| 8270 |
} |
0 |
8270 |
} |
0 |
| 8271 |
|
--- |
8271 |
|
--- |
| 8272 |
void ScalarEvolution::forgetAllLoops() { |
0 |
8272 |
void ScalarEvolution::forgetAllLoops() { |
0 |
| 8273 |
// This method is intended to forget all info about loops. It should |
--- |
8273 |
// This method is intended to forget all info about loops. It should |
--- |
| 8274 |
// invalidate caches as if the following happened: |
--- |
8274 |
// invalidate caches as if the following happened: |
--- |
| 8275 |
// - The trip counts of all loops have changed arbitrarily |
--- |
8275 |
// - The trip counts of all loops have changed arbitrarily |
--- |
| 8276 |
// - Every llvm::Value has been updated in place to produce a different |
--- |
8276 |
// - Every llvm::Value has been updated in place to produce a different |
--- |
| 8277 |
// result. |
--- |
8277 |
// result. |
--- |
| 8278 |
BackedgeTakenCounts.clear(); |
0 |
8278 |
BackedgeTakenCounts.clear(); |
0 |
| 8279 |
PredicatedBackedgeTakenCounts.clear(); |
0 |
8279 |
PredicatedBackedgeTakenCounts.clear(); |
0 |
| 8280 |
BECountUsers.clear(); |
0 |
8280 |
BECountUsers.clear(); |
0 |
| 8281 |
LoopPropertiesCache.clear(); |
0 |
8281 |
LoopPropertiesCache.clear(); |
0 |
| 8282 |
ConstantEvolutionLoopExitValue.clear(); |
0 |
8282 |
ConstantEvolutionLoopExitValue.clear(); |
0 |
| 8283 |
ValueExprMap.clear(); |
0 |
8283 |
ValueExprMap.clear(); |
0 |
| 8284 |
ValuesAtScopes.clear(); |
0 |
8284 |
ValuesAtScopes.clear(); |
0 |
| 8285 |
ValuesAtScopesUsers.clear(); |
0 |
8285 |
ValuesAtScopesUsers.clear(); |
0 |
| 8286 |
LoopDispositions.clear(); |
0 |
8286 |
LoopDispositions.clear(); |
0 |
| 8287 |
BlockDispositions.clear(); |
0 |
8287 |
BlockDispositions.clear(); |
0 |
| 8288 |
UnsignedRanges.clear(); |
0 |
8288 |
UnsignedRanges.clear(); |
0 |
| 8289 |
SignedRanges.clear(); |
0 |
8289 |
SignedRanges.clear(); |
0 |
| 8290 |
ExprValueMap.clear(); |
0 |
8290 |
ExprValueMap.clear(); |
0 |
| 8291 |
HasRecMap.clear(); |
0 |
8291 |
HasRecMap.clear(); |
0 |
| 8292 |
ConstantMultipleCache.clear(); |
0 |
8292 |
ConstantMultipleCache.clear(); |
0 |
| 8293 |
PredicatedSCEVRewrites.clear(); |
0 |
8293 |
PredicatedSCEVRewrites.clear(); |
0 |
| 8294 |
FoldCache.clear(); |
0 |
8294 |
FoldCache.clear(); |
0 |
| 8295 |
FoldCacheUser.clear(); |
0 |
8295 |
FoldCacheUser.clear(); |
0 |
| 8296 |
} |
0 |
8296 |
} |
0 |
| 8297 |
void ScalarEvolution::visitAndClearUsers( |
0 |
8297 |
void ScalarEvolution::visitAndClearUsers( |
0 |
| 8298 |
SmallVectorImpl &Worklist, |
--- |
8298 |
SmallVectorImpl &Worklist, |
--- |
| 8299 |
SmallPtrSetImpl &Visited, |
--- |
8299 |
SmallPtrSetImpl &Visited, |
--- |
| 8300 |
SmallVectorImpl &ToForget) { |
--- |
8300 |
SmallVectorImpl &ToForget) { |
--- |
| 8301 |
while (!Worklist.empty()) { |
0 |
8301 |
while (!Worklist.empty()) { |
0 |
| 8302 |
Instruction *I = Worklist.pop_back_val(); |
0 |
8302 |
Instruction *I = Worklist.pop_back_val(); |
0 |
| 8303 |
if (!isSCEVable(I->getType())) |
0 |
8303 |
if (!isSCEVable(I->getType())) |
0 |
| 8304 |
continue; |
0 |
8304 |
continue; |
0 |
| 8305 |
|
--- |
8305 |
|
--- |
| 8306 |
ValueExprMapType::iterator It = |
--- |
8306 |
ValueExprMapType::iterator It = |
--- |
| 8307 |
ValueExprMap.find_as(static_cast(I)); |
0 |
8307 |
ValueExprMap.find_as(static_cast(I)); |
0 |
| 8308 |
if (It != ValueExprMap.end()) { |
0 |
8308 |
if (It != ValueExprMap.end()) { |
0 |
| 8309 |
eraseValueFromMap(It->first); |
0 |
8309 |
eraseValueFromMap(It->first); |
0 |
| 8310 |
ToForget.push_back(It->second); |
0 |
8310 |
ToForget.push_back(It->second); |
0 |
| 8311 |
if (PHINode *PN = dyn_cast(I)) |
0 |
8311 |
if (PHINode *PN = dyn_cast(I)) |
0 |
| 8312 |
ConstantEvolutionLoopExitValue.erase(PN); |
0 |
8312 |
ConstantEvolutionLoopExitValue.erase(PN); |
0 |
| 8313 |
} |
--- |
8313 |
} |
--- |
| 8314 |
|
--- |
8314 |
|
--- |
| 8315 |
PushDefUseChildren(I, Worklist, Visited); |
0 |
8315 |
PushDefUseChildren(I, Worklist, Visited); |
0 |
| 8316 |
} |
--- |
8316 |
} |
--- |
| 8317 |
} |
0 |
8317 |
} |
0 |
| 8318 |
|
--- |
8318 |
|
--- |
| 8319 |
void ScalarEvolution::forgetLoop(const Loop *L) { |
0 |
8319 |
void ScalarEvolution::forgetLoop(const Loop *L) { |
0 |
| 8320 |
SmallVector LoopWorklist(1, L); |
0 |
8320 |
SmallVector LoopWorklist(1, L); |
0 |
| 8321 |
SmallVector Worklist; |
0 |
8321 |
SmallVector Worklist; |
0 |
| 8322 |
SmallPtrSet Visited; |
0 |
8322 |
SmallPtrSet Visited; |
0 |
| 8323 |
SmallVector ToForget; |
0 |
8323 |
SmallVector ToForget; |
0 |
| 8324 |
|
--- |
8324 |
|
--- |
| 8325 |
// Iterate over all the loops and sub-loops to drop SCEV information. |
--- |
8325 |
// Iterate over all the loops and sub-loops to drop SCEV information. |
--- |
| 8326 |
while (!LoopWorklist.empty()) { |
0 |
8326 |
while (!LoopWorklist.empty()) { |
0 |
| 8327 |
auto *CurrL = LoopWorklist.pop_back_val(); |
0 |
8327 |
auto *CurrL = LoopWorklist.pop_back_val(); |
0 |
| 8328 |
|
--- |
8328 |
|
--- |
| 8329 |
// Drop any stored trip count value. |
--- |
8329 |
// Drop any stored trip count value. |
--- |
| 8330 |
forgetBackedgeTakenCounts(CurrL, /* Predicated */ false); |
0 |
8330 |
forgetBackedgeTakenCounts(CurrL, /* Predicated */ false); |
0 |
| 8331 |
forgetBackedgeTakenCounts(CurrL, /* Predicated */ true); |
0 |
8331 |
forgetBackedgeTakenCounts(CurrL, /* Predicated */ true); |
0 |
| 8332 |
|
--- |
8332 |
|
--- |
| 8333 |
// Drop information about predicated SCEV rewrites for this loop. |
--- |
8333 |
// Drop information about predicated SCEV rewrites for this loop. |
--- |
| 8334 |
for (auto I = PredicatedSCEVRewrites.begin(); |
0 |
8334 |
for (auto I = PredicatedSCEVRewrites.begin(); |
0 |
| 8335 |
I != PredicatedSCEVRewrites.end();) { |
0 |
8335 |
I != PredicatedSCEVRewrites.end();) { |
0 |
| 8336 |
std::pair Entry = I->first; |
0 |
8336 |
std::pair Entry = I->first; |
0 |
| 8337 |
if (Entry.second == CurrL) |
0 |
8337 |
if (Entry.second == CurrL) |
0 |
| 8338 |
PredicatedSCEVRewrites.erase(I++); |
0 |
8338 |
PredicatedSCEVRewrites.erase(I++); |
0 |
| 8339 |
else |
--- |
8339 |
else |
--- |
| 8340 |
++I; |
0 |
8340 |
++I; |
0 |
| 8341 |
} |
--- |
8341 |
} |
--- |
| 8342 |
|
--- |
8342 |
|
--- |
| 8343 |
auto LoopUsersItr = LoopUsers.find(CurrL); |
0 |
8343 |
auto LoopUsersItr = LoopUsers.find(CurrL); |
0 |
| 8344 |
if (LoopUsersItr != LoopUsers.end()) { |
0 |
8344 |
if (LoopUsersItr != LoopUsers.end()) { |
0 |
| 8345 |
ToForget.insert(ToForget.end(), LoopUsersItr->second.begin(), |
0 |
8345 |
ToForget.insert(ToForget.end(), LoopUsersItr->second.begin(), |
0 |
| 8346 |
LoopUsersItr->second.end()); |
0 |
8346 |
LoopUsersItr->second.end()); |
0 |
| 8347 |
} |
--- |
8347 |
} |
--- |
| 8348 |
|
--- |
8348 |
|
--- |
| 8349 |
// Drop information about expressions based on loop-header PHIs. |
--- |
8349 |
// Drop information about expressions based on loop-header PHIs. |
--- |
| 8350 |
PushLoopPHIs(CurrL, Worklist, Visited); |
0 |
8350 |
PushLoopPHIs(CurrL, Worklist, Visited); |
0 |
| 8351 |
visitAndClearUsers(Worklist, Visited, ToForget); |
0 |
8351 |
visitAndClearUsers(Worklist, Visited, ToForget); |
0 |
| 8352 |
|
--- |
8352 |
|
--- |
| 8353 |
LoopPropertiesCache.erase(CurrL); |
0 |
8353 |
LoopPropertiesCache.erase(CurrL); |
0 |
| 8354 |
// Forget all contained loops too, to avoid dangling entries in the |
--- |
8354 |
// Forget all contained loops too, to avoid dangling entries in the |
--- |
| 8355 |
// ValuesAtScopes map. |
--- |
8355 |
// ValuesAtScopes map. |
--- |
| 8356 |
LoopWorklist.append(CurrL->begin(), CurrL->end()); |
0 |
8356 |
LoopWorklist.append(CurrL->begin(), CurrL->end()); |
0 |
| 8357 |
} |
--- |
8357 |
} |
--- |
| 8358 |
forgetMemoizedResults(ToForget); |
0 |
8358 |
forgetMemoizedResults(ToForget); |
0 |
| 8359 |
} |
0 |
8359 |
} |
0 |
| 8360 |
|
--- |
8360 |
|
--- |
| 8361 |
void ScalarEvolution::forgetTopmostLoop(const Loop *L) { |
0 |
8361 |
void ScalarEvolution::forgetTopmostLoop(const Loop *L) { |
0 |
| 8362 |
forgetLoop(L->getOutermostLoop()); |
0 |
8362 |
forgetLoop(L->getOutermostLoop()); |
0 |
| 8363 |
} |
0 |
8363 |
} |
0 |
| 8364 |
|
--- |
8364 |
|
--- |
| 8365 |
void ScalarEvolution::forgetValue(Value *V) { |
0 |
8365 |
void ScalarEvolution::forgetValue(Value *V) { |
0 |
| 8366 |
Instruction *I = dyn_cast(V); |
0 |
8366 |
Instruction *I = dyn_cast(V); |
0 |
| 8367 |
if (!I) return; |
0 |
8367 |
if (!I) return; |
0 |
| 8368 |
|
--- |
8368 |
|
--- |
| 8369 |
// Drop information about expressions based on loop-header PHIs. |
--- |
8369 |
// Drop information about expressions based on loop-header PHIs. |
--- |
| 8370 |
SmallVector Worklist; |
0 |
8370 |
SmallVector Worklist; |
0 |
| 8371 |
SmallPtrSet Visited; |
0 |
8371 |
SmallPtrSet Visited; |
0 |
| 8372 |
SmallVector ToForget; |
0 |
8372 |
SmallVector ToForget; |
0 |
| 8373 |
Worklist.push_back(I); |
0 |
8373 |
Worklist.push_back(I); |
0 |
| 8374 |
Visited.insert(I); |
0 |
8374 |
Visited.insert(I); |
0 |
| 8375 |
visitAndClearUsers(Worklist, Visited, ToForget); |
0 |
8375 |
visitAndClearUsers(Worklist, Visited, ToForget); |
0 |
| 8376 |
|
--- |
8376 |
|
--- |
| 8377 |
forgetMemoizedResults(ToForget); |
0 |
8377 |
forgetMemoizedResults(ToForget); |
0 |
| 8378 |
} |
0 |
8378 |
} |
0 |
| 8379 |
|
--- |
8379 |
|
--- |
| 8380 |
void ScalarEvolution::forgetLoopDispositions() { LoopDispositions.clear(); } |
0 |
8380 |
void ScalarEvolution::forgetLoopDispositions() { LoopDispositions.clear(); } |
0 |
| 8381 |
|
--- |
8381 |
|
--- |
| 8382 |
void ScalarEvolution::forgetBlockAndLoopDispositions(Value *V) { |
0 |
8382 |
void ScalarEvolution::forgetBlockAndLoopDispositions(Value *V) { |
0 |
| 8383 |
// Unless a specific value is passed to invalidation, completely clear both |
--- |
8383 |
// Unless a specific value is passed to invalidation, completely clear both |
--- |
| 8384 |
// caches. |
--- |
8384 |
// caches. |
--- |
| 8385 |
if (!V) { |
0 |
8385 |
if (!V) { |
0 |
| 8386 |
BlockDispositions.clear(); |
0 |
8386 |
BlockDispositions.clear(); |
0 |
| 8387 |
LoopDispositions.clear(); |
0 |
8387 |
LoopDispositions.clear(); |
0 |
| 8388 |
return; |
0 |
8388 |
return; |
0 |
| 8389 |
} |
--- |
8389 |
} |
--- |
| 8390 |
|
--- |
8390 |
|
--- |
| 8391 |
if (!isSCEVable(V->getType())) |
0 |
8391 |
if (!isSCEVable(V->getType())) |
0 |
| 8392 |
return; |
0 |
8392 |
return; |
0 |
| 8393 |
|
--- |
8393 |
|
--- |
| 8394 |
const SCEV *S = getExistingSCEV(V); |
0 |
8394 |
const SCEV *S = getExistingSCEV(V); |
0 |
| 8395 |
if (!S) |
0 |
8395 |
if (!S) |
0 |
| 8396 |
return; |
0 |
8396 |
return; |
0 |
| 8397 |
|
--- |
8397 |
|
--- |
| 8398 |
// Invalidate the block and loop dispositions cached for S. Dispositions of |
--- |
8398 |
// Invalidate the block and loop dispositions cached for S. Dispositions of |
--- |
| 8399 |
// S's users may change if S's disposition changes (i.e. a user may change to |
--- |
8399 |
// S's users may change if S's disposition changes (i.e. a user may change to |
--- |
| 8400 |
// loop-invariant, if S changes to loop invariant), so also invalidate |
--- |
8400 |
// loop-invariant, if S changes to loop invariant), so also invalidate |
--- |
| 8401 |
// dispositions of S's users recursively. |
--- |
8401 |
// dispositions of S's users recursively. |
--- |
| 8402 |
SmallVector Worklist = {S}; |
0 |
8402 |
SmallVector Worklist = {S}; |
0 |
| 8403 |
SmallPtrSet Seen = {S}; |
0 |
8403 |
SmallPtrSet Seen = {S}; |
0 |
| 8404 |
while (!Worklist.empty()) { |
0 |
8404 |
while (!Worklist.empty()) { |
0 |
| 8405 |
const SCEV *Curr = Worklist.pop_back_val(); |
0 |
8405 |
const SCEV *Curr = Worklist.pop_back_val(); |
0 |
| 8406 |
bool LoopDispoRemoved = LoopDispositions.erase(Curr); |
0 |
8406 |
bool LoopDispoRemoved = LoopDispositions.erase(Curr); |
0 |
| 8407 |
bool BlockDispoRemoved = BlockDispositions.erase(Curr); |
0 |
8407 |
bool BlockDispoRemoved = BlockDispositions.erase(Curr); |
0 |
| 8408 |
if (!LoopDispoRemoved && !BlockDispoRemoved) |
0 |
8408 |
if (!LoopDispoRemoved && !BlockDispoRemoved) |
0 |
| 8409 |
continue; |
0 |
8409 |
continue; |
0 |
| 8410 |
auto Users = SCEVUsers.find(Curr); |
0 |
8410 |
auto Users = SCEVUsers.find(Curr); |
0 |
| 8411 |
if (Users != SCEVUsers.end()) |
0 |
8411 |
if (Users != SCEVUsers.end()) |
0 |
| 8412 |
for (const auto *User : Users->second) |
0 |
8412 |
for (const auto *User : Users->second) |
0 |
| 8413 |
if (Seen.insert(User).second) |
0 |
8413 |
if (Seen.insert(User).second) |
0 |
| 8414 |
Worklist.push_back(User); |
0 |
8414 |
Worklist.push_back(User); |
0 |
| 8415 |
} |
--- |
8415 |
} |
--- |
| 8416 |
} |
0 |
8416 |
} |
0 |
| 8417 |
|
--- |
8417 |
|
--- |
| 8418 |
/// Get the exact loop backedge taken count considering all loop exits. A |
--- |
8418 |
/// Get the exact loop backedge taken count considering all loop exits. A |
--- |
| 8419 |
/// computable result can only be returned for loops with all exiting blocks |
--- |
8419 |
/// computable result can only be returned for loops with all exiting blocks |
--- |
| 8420 |
/// dominating the latch. howFarToZero assumes that the limit of each loop test |
--- |
8420 |
/// dominating the latch. howFarToZero assumes that the limit of each loop test |
--- |
| 8421 |
/// is never skipped. This is a valid assumption as long as the loop exits via |
--- |
8421 |
/// is never skipped. This is a valid assumption as long as the loop exits via |
--- |
| 8422 |
/// that test. For precise results, it is the caller's responsibility to specify |
--- |
8422 |
/// that test. For precise results, it is the caller's responsibility to specify |
--- |
| 8423 |
/// the relevant loop exiting block using getExact(ExitingBlock, SE). |
--- |
8423 |
/// the relevant loop exiting block using getExact(ExitingBlock, SE). |
--- |
| 8424 |
const SCEV * |
--- |
8424 |
const SCEV * |
--- |
| 8425 |
ScalarEvolution::BackedgeTakenInfo::getExact(const Loop *L, ScalarEvolution *SE, |
0 |
8425 |
ScalarEvolution::BackedgeTakenInfo::getExact(const Loop *L, ScalarEvolution *SE, |
0 |
| 8426 |
SmallVector *Preds) const { |
--- |
8426 |
SmallVector *Preds) const { |
--- |
| 8427 |
// If any exits were not computable, the loop is not computable. |
--- |
8427 |
// If any exits were not computable, the loop is not computable. |
--- |
| 8428 |
if (!isComplete() || ExitNotTaken.empty()) |
0 |
8428 |
if (!isComplete() || ExitNotTaken.empty()) |
0 |
| 8429 |
return SE->getCouldNotCompute(); |
0 |
8429 |
return SE->getCouldNotCompute(); |
0 |
| 8430 |
|
--- |
8430 |
|
--- |
| 8431 |
const BasicBlock *Latch = L->getLoopLatch(); |
0 |
8431 |
const BasicBlock *Latch = L->getLoopLatch(); |
0 |
| 8432 |
// All exiting blocks we have collected must dominate the only backedge. |
--- |
8432 |
// All exiting blocks we have collected must dominate the only backedge. |
--- |
| 8433 |
if (!Latch) |
0 |
8433 |
if (!Latch) |
0 |
| 8434 |
return SE->getCouldNotCompute(); |
0 |
8434 |
return SE->getCouldNotCompute(); |
0 |
| 8435 |
|
--- |
8435 |
|
--- |
| 8436 |
// All exiting blocks we have gathered dominate loop's latch, so exact trip |
--- |
8436 |
// All exiting blocks we have gathered dominate loop's latch, so exact trip |
--- |
| 8437 |
// count is simply a minimum out of all these calculated exit counts. |
--- |
8437 |
// count is simply a minimum out of all these calculated exit counts. |
--- |
| 8438 |
SmallVector Ops; |
0 |
8438 |
SmallVector Ops; |
0 |
| 8439 |
for (const auto &ENT : ExitNotTaken) { |
0 |
8439 |
for (const auto &ENT : ExitNotTaken) { |
0 |
| 8440 |
const SCEV *BECount = ENT.ExactNotTaken; |
0 |
8440 |
const SCEV *BECount = ENT.ExactNotTaken; |
0 |
| 8441 |
assert(BECount != SE->getCouldNotCompute() && "Bad exit SCEV!"); |
0 |
8441 |
assert(BECount != SE->getCouldNotCompute() && "Bad exit SCEV!"); |
0 |
| 8442 |
assert(SE->DT.dominates(ENT.ExitingBlock, Latch) && |
0 |
8442 |
assert(SE->DT.dominates(ENT.ExitingBlock, Latch) && |
0 |
| 8443 |
"We should only have known counts for exiting blocks that dominate " |
--- |
8443 |
"We should only have known counts for exiting blocks that dominate " |
--- |
| 8444 |
"latch!"); |
--- |
8444 |
"latch!"); |
--- |
| 8445 |
|
--- |
8445 |
|
--- |
| 8446 |
Ops.push_back(BECount); |
0 |
8446 |
Ops.push_back(BECount); |
0 |
| 8447 |
|
--- |
8447 |
|
--- |
| 8448 |
if (Preds) |
0 |
8448 |
if (Preds) |
0 |
| 8449 |
for (const auto *P : ENT.Predicates) |
0 |
8449 |
for (const auto *P : ENT.Predicates) |
0 |
| 8450 |
Preds->push_back(P); |
0 |
8450 |
Preds->push_back(P); |
0 |
| 8451 |
|
--- |
8451 |
|
--- |
| 8452 |
assert((Preds || ENT.hasAlwaysTruePredicate()) && |
0 |
8452 |
assert((Preds || ENT.hasAlwaysTruePredicate()) && |
0 |
| 8453 |
"Predicate should be always true!"); |
--- |
8453 |
"Predicate should be always true!"); |
--- |
| 8454 |
} |
--- |
8454 |
} |
--- |
| 8455 |
|
--- |
8455 |
|
--- |
| 8456 |
// If an earlier exit exits on the first iteration (exit count zero), then |
--- |
8456 |
// If an earlier exit exits on the first iteration (exit count zero), then |
--- |
| 8457 |
// a later poison exit count should not propagate into the result. This are |
--- |
8457 |
// a later poison exit count should not propagate into the result. This are |
--- |
| 8458 |
// exactly the semantics provided by umin_seq. |
--- |
8458 |
// exactly the semantics provided by umin_seq. |
--- |
| 8459 |
return SE->getUMinFromMismatchedTypes(Ops, /* Sequential */ true); |
0 |
8459 |
return SE->getUMinFromMismatchedTypes(Ops, /* Sequential */ true); |
0 |
| 8460 |
} |
0 |
8460 |
} |
0 |
| 8461 |
|
--- |
8461 |
|
--- |
| 8462 |
/// Get the exact not taken count for this loop exit. |
--- |
8462 |
/// Get the exact not taken count for this loop exit. |
--- |
| 8463 |
const SCEV * |
--- |
8463 |
const SCEV * |
--- |
| 8464 |
ScalarEvolution::BackedgeTakenInfo::getExact(const BasicBlock *ExitingBlock, |
0 |
8464 |
ScalarEvolution::BackedgeTakenInfo::getExact(const BasicBlock *ExitingBlock, |
0 |
| 8465 |
ScalarEvolution *SE) const { |
--- |
8465 |
ScalarEvolution *SE) const { |
--- |
| 8466 |
for (const auto &ENT : ExitNotTaken) |
0 |
8466 |
for (const auto &ENT : ExitNotTaken) |
0 |
| 8467 |
if (ENT.ExitingBlock == ExitingBlock && ENT.hasAlwaysTruePredicate()) |
0 |
8467 |
if (ENT.ExitingBlock == ExitingBlock && ENT.hasAlwaysTruePredicate()) |
0 |
| 8468 |
return ENT.ExactNotTaken; |
0 |
8468 |
return ENT.ExactNotTaken; |
0 |
| 8469 |
|
--- |
8469 |
|
--- |
| 8470 |
return SE->getCouldNotCompute(); |
0 |
8470 |
return SE->getCouldNotCompute(); |
0 |
| 8471 |
} |
--- |
8471 |
} |
--- |
| 8472 |
|
--- |
8472 |
|
--- |
| 8473 |
const SCEV *ScalarEvolution::BackedgeTakenInfo::getConstantMax( |
0 |
8473 |
const SCEV *ScalarEvolution::BackedgeTakenInfo::getConstantMax( |
0 |
| 8474 |
const BasicBlock *ExitingBlock, ScalarEvolution *SE) const { |
--- |
8474 |
const BasicBlock *ExitingBlock, ScalarEvolution *SE) const { |
--- |
| 8475 |
for (const auto &ENT : ExitNotTaken) |
0 |
8475 |
for (const auto &ENT : ExitNotTaken) |
0 |
| 8476 |
if (ENT.ExitingBlock == ExitingBlock && ENT.hasAlwaysTruePredicate()) |
0 |
8476 |
if (ENT.ExitingBlock == ExitingBlock && ENT.hasAlwaysTruePredicate()) |
0 |
| 8477 |
return ENT.ConstantMaxNotTaken; |
0 |
8477 |
return ENT.ConstantMaxNotTaken; |
0 |
| 8478 |
|
--- |
8478 |
|
--- |
| 8479 |
return SE->getCouldNotCompute(); |
0 |
8479 |
return SE->getCouldNotCompute(); |
0 |
| 8480 |
} |
--- |
8480 |
} |
--- |
| 8481 |
|
--- |
8481 |
|
--- |
| 8482 |
const SCEV *ScalarEvolution::BackedgeTakenInfo::getSymbolicMax( |
0 |
8482 |
const SCEV *ScalarEvolution::BackedgeTakenInfo::getSymbolicMax( |
0 |
| 8483 |
const BasicBlock *ExitingBlock, ScalarEvolution *SE) const { |
--- |
8483 |
const BasicBlock *ExitingBlock, ScalarEvolution *SE) const { |
--- |
| 8484 |
for (const auto &ENT : ExitNotTaken) |
0 |
8484 |
for (const auto &ENT : ExitNotTaken) |
0 |
| 8485 |
if (ENT.ExitingBlock == ExitingBlock && ENT.hasAlwaysTruePredicate()) |
0 |
8485 |
if (ENT.ExitingBlock == ExitingBlock && ENT.hasAlwaysTruePredicate()) |
0 |
| 8486 |
return ENT.SymbolicMaxNotTaken; |
0 |
8486 |
return ENT.SymbolicMaxNotTaken; |
0 |
| 8487 |
|
--- |
8487 |
|
--- |
| 8488 |
return SE->getCouldNotCompute(); |
0 |
8488 |
return SE->getCouldNotCompute(); |
0 |
| 8489 |
} |
--- |
8489 |
} |
--- |
| 8490 |
|
--- |
8490 |
|
--- |
| 8491 |
/// getConstantMax - Get the constant max backedge taken count for the loop. |
--- |
8491 |
/// getConstantMax - Get the constant max backedge taken count for the loop. |
--- |
| 8492 |
const SCEV * |
--- |
8492 |
const SCEV * |
--- |
| 8493 |
ScalarEvolution::BackedgeTakenInfo::getConstantMax(ScalarEvolution *SE) const { |
0 |
8493 |
ScalarEvolution::BackedgeTakenInfo::getConstantMax(ScalarEvolution *SE) const { |
0 |
| 8494 |
auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) { |
0 |
8494 |
auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) { |
0 |
| 8495 |
return !ENT.hasAlwaysTruePredicate(); |
0 |
8495 |
return !ENT.hasAlwaysTruePredicate(); |
0 |
| 8496 |
}; |
--- |
8496 |
}; |
--- |
| 8497 |
|
--- |
8497 |
|
--- |
| 8498 |
if (!getConstantMax() || any_of(ExitNotTaken, PredicateNotAlwaysTrue)) |
0 |
8498 |
if (!getConstantMax() || any_of(ExitNotTaken, PredicateNotAlwaysTrue)) |
0 |
| 8499 |
return SE->getCouldNotCompute(); |
0 |
8499 |
return SE->getCouldNotCompute(); |
0 |
| 8500 |
|
--- |
8500 |
|
--- |
| 8501 |
assert((isa(getConstantMax()) || |
0 |
8501 |
assert((isa(getConstantMax()) || |
0 |
| 8502 |
isa(getConstantMax())) && |
--- |
8502 |
isa(getConstantMax())) && |
--- |
| 8503 |
"No point in having a non-constant max backedge taken count!"); |
--- |
8503 |
"No point in having a non-constant max backedge taken count!"); |
--- |
| 8504 |
return getConstantMax(); |
0 |
8504 |
return getConstantMax(); |
0 |
| 8505 |
} |
--- |
8505 |
} |
--- |
| 8506 |
|
--- |
8506 |
|
--- |
| 8507 |
const SCEV * |
--- |
8507 |
const SCEV * |
--- |
| 8508 |
ScalarEvolution::BackedgeTakenInfo::getSymbolicMax(const Loop *L, |
0 |
8508 |
ScalarEvolution::BackedgeTakenInfo::getSymbolicMax(const Loop *L, |
0 |
| 8509 |
ScalarEvolution *SE) { |
--- |
8509 |
ScalarEvolution *SE) { |
--- |
| 8510 |
if (!SymbolicMax) |
0 |
8510 |
if (!SymbolicMax) |
0 |
| 8511 |
SymbolicMax = SE->computeSymbolicMaxBackedgeTakenCount(L); |
0 |
8511 |
SymbolicMax = SE->computeSymbolicMaxBackedgeTakenCount(L); |
0 |
| 8512 |
return SymbolicMax; |
0 |
8512 |
return SymbolicMax; |
0 |
| 8513 |
} |
--- |
8513 |
} |
--- |
| 8514 |
|
--- |
8514 |
|
--- |
| 8515 |
bool ScalarEvolution::BackedgeTakenInfo::isConstantMaxOrZero( |
0 |
8515 |
bool ScalarEvolution::BackedgeTakenInfo::isConstantMaxOrZero( |
0 |
| 8516 |
ScalarEvolution *SE) const { |
--- |
8516 |
ScalarEvolution *SE) const { |
--- |
| 8517 |
auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) { |
0 |
8517 |
auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) { |
0 |
| 8518 |
return !ENT.hasAlwaysTruePredicate(); |
0 |
8518 |
return !ENT.hasAlwaysTruePredicate(); |
0 |
| 8519 |
}; |
--- |
8519 |
}; |
--- |
| 8520 |
return MaxOrZero && !any_of(ExitNotTaken, PredicateNotAlwaysTrue); |
0 |
8520 |
return MaxOrZero && !any_of(ExitNotTaken, PredicateNotAlwaysTrue); |
0 |
| 8521 |
} |
--- |
8521 |
} |
--- |
| 8522 |
|
--- |
8522 |
|
--- |
| 8523 |
ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E) |
0 |
8523 |
ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E) |
0 |
| 8524 |
: ExitLimit(E, E, E, false, std::nullopt) {} |
0 |
8524 |
: ExitLimit(E, E, E, false, std::nullopt) {} |
0 |
| 8525 |
|
--- |
8525 |
|
--- |
| 8526 |
ScalarEvolution::ExitLimit::ExitLimit( |
0 |
8526 |
ScalarEvolution::ExitLimit::ExitLimit( |
0 |
| 8527 |
const SCEV *E, const SCEV *ConstantMaxNotTaken, |
--- |
8527 |
const SCEV *E, const SCEV *ConstantMaxNotTaken, |
--- |
| 8528 |
const SCEV *SymbolicMaxNotTaken, bool MaxOrZero, |
--- |
8528 |
const SCEV *SymbolicMaxNotTaken, bool MaxOrZero, |
--- |
| 8529 |
ArrayRef *> PredSetList) |
--- |
8529 |
ArrayRef *> PredSetList) |
--- |
| 8530 |
: ExactNotTaken(E), ConstantMaxNotTaken(ConstantMaxNotTaken), |
0 |
8530 |
: ExactNotTaken(E), ConstantMaxNotTaken(ConstantMaxNotTaken), |
0 |
| 8531 |
SymbolicMaxNotTaken(SymbolicMaxNotTaken), MaxOrZero(MaxOrZero) { |
0 |
8531 |
SymbolicMaxNotTaken(SymbolicMaxNotTaken), MaxOrZero(MaxOrZero) { |
0 |
| 8532 |
// If we prove the max count is zero, so is the symbolic bound. This happens |
--- |
8532 |
// If we prove the max count is zero, so is the symbolic bound. This happens |
--- |
| 8533 |
// in practice due to differences in a) how context sensitive we've chosen |
--- |
8533 |
// in practice due to differences in a) how context sensitive we've chosen |
--- |
| 8534 |
// to be and b) how we reason about bounds implied by UB. |
--- |
8534 |
// to be and b) how we reason about bounds implied by UB. |
--- |
| 8535 |
if (ConstantMaxNotTaken->isZero()) { |
0 |
8535 |
if (ConstantMaxNotTaken->isZero()) { |
0 |
| 8536 |
this->ExactNotTaken = E = ConstantMaxNotTaken; |
0 |
8536 |
this->ExactNotTaken = E = ConstantMaxNotTaken; |
0 |
| 8537 |
this->SymbolicMaxNotTaken = SymbolicMaxNotTaken = ConstantMaxNotTaken; |
0 |
8537 |
this->SymbolicMaxNotTaken = SymbolicMaxNotTaken = ConstantMaxNotTaken; |
0 |
| 8538 |
} |
--- |
8538 |
} |
--- |
| 8539 |
|
--- |
8539 |
|
--- |
| 8540 |
assert((isa(ExactNotTaken) || |
0 |
8540 |
assert((isa(ExactNotTaken) || |
0 |
| 8541 |
!isa(ConstantMaxNotTaken)) && |
--- |
8541 |
!isa(ConstantMaxNotTaken)) && |
--- |
| 8542 |
"Exact is not allowed to be less precise than Constant Max"); |
--- |
8542 |
"Exact is not allowed to be less precise than Constant Max"); |
--- |
| 8543 |
assert((isa(ExactNotTaken) || |
0 |
8543 |
assert((isa(ExactNotTaken) || |
0 |
| 8544 |
!isa(SymbolicMaxNotTaken)) && |
--- |
8544 |
!isa(SymbolicMaxNotTaken)) && |
--- |
| 8545 |
"Exact is not allowed to be less precise than Symbolic Max"); |
--- |
8545 |
"Exact is not allowed to be less precise than Symbolic Max"); |
--- |
| 8546 |
assert((isa(SymbolicMaxNotTaken) || |
0 |
8546 |
assert((isa(SymbolicMaxNotTaken) || |
0 |
| 8547 |
!isa(ConstantMaxNotTaken)) && |
--- |
8547 |
!isa(ConstantMaxNotTaken)) && |
--- |
| 8548 |
"Symbolic Max is not allowed to be less precise than Constant Max"); |
--- |
8548 |
"Symbolic Max is not allowed to be less precise than Constant Max"); |
--- |
| 8549 |
assert((isa(ConstantMaxNotTaken) || |
0 |
8549 |
assert((isa(ConstantMaxNotTaken) || |
0 |
| 8550 |
isa(ConstantMaxNotTaken)) && |
--- |
8550 |
isa(ConstantMaxNotTaken)) && |
--- |
| 8551 |
"No point in having a non-constant max backedge taken count!"); |
--- |
8551 |
"No point in having a non-constant max backedge taken count!"); |
--- |
| 8552 |
for (const auto *PredSet : PredSetList) |
0 |
8552 |
for (const auto *PredSet : PredSetList) |
0 |
| 8553 |
for (const auto *P : *PredSet) |
0 |
8553 |
for (const auto *P : *PredSet) |
0 |
| 8554 |
addPredicate(P); |
0 |
8554 |
addPredicate(P); |
0 |
| 8555 |
assert((isa(E) || !E->getType()->isPointerTy()) && |
0 |
8555 |
assert((isa(E) || !E->getType()->isPointerTy()) && |
0 |
| 8556 |
"Backedge count should be int"); |
--- |
8556 |
"Backedge count should be int"); |
--- |
| 8557 |
assert((isa(ConstantMaxNotTaken) || |
0 |
8557 |
assert((isa(ConstantMaxNotTaken) || |
0 |
| 8558 |
!ConstantMaxNotTaken->getType()->isPointerTy()) && |
--- |
8558 |
!ConstantMaxNotTaken->getType()->isPointerTy()) && |
--- |
| 8559 |
"Max backedge count should be int"); |
--- |
8559 |
"Max backedge count should be int"); |
--- |
| 8560 |
} |
0 |
8560 |
} |
0 |
| 8561 |
|
--- |
8561 |
|
--- |
| 8562 |
ScalarEvolution::ExitLimit::ExitLimit( |
0 |
8562 |
ScalarEvolution::ExitLimit::ExitLimit( |
0 |
| 8563 |
const SCEV *E, const SCEV *ConstantMaxNotTaken, |
--- |
8563 |
const SCEV *E, const SCEV *ConstantMaxNotTaken, |
--- |
| 8564 |
const SCEV *SymbolicMaxNotTaken, bool MaxOrZero, |
--- |
8564 |
const SCEV *SymbolicMaxNotTaken, bool MaxOrZero, |
--- |
| 8565 |
const SmallPtrSetImpl &PredSet) |
--- |
8565 |
const SmallPtrSetImpl &PredSet) |
--- |
| 8566 |
: ExitLimit(E, ConstantMaxNotTaken, SymbolicMaxNotTaken, MaxOrZero, |
--- |
8566 |
: ExitLimit(E, ConstantMaxNotTaken, SymbolicMaxNotTaken, MaxOrZero, |
--- |
| 8567 |
{ &PredSet }) {} |
0 |
8567 |
{ &PredSet }) {} |
0 |
| 8568 |
|
--- |
8568 |
|
--- |
| 8569 |
/// Allocate memory for BackedgeTakenInfo and copy the not-taken count of each |
--- |
8569 |
/// Allocate memory for BackedgeTakenInfo and copy the not-taken count of each |
--- |
| 8570 |
/// computable exit into a persistent ExitNotTakenInfo array. |
--- |
8570 |
/// computable exit into a persistent ExitNotTakenInfo array. |
--- |
| 8571 |
ScalarEvolution::BackedgeTakenInfo::BackedgeTakenInfo( |
0 |
8571 |
ScalarEvolution::BackedgeTakenInfo::BackedgeTakenInfo( |
0 |
| 8572 |
ArrayRef ExitCounts, |
--- |
8572 |
ArrayRef ExitCounts, |
--- |
| 8573 |
bool IsComplete, const SCEV *ConstantMax, bool MaxOrZero) |
--- |
8573 |
bool IsComplete, const SCEV *ConstantMax, bool MaxOrZero) |
--- |
| 8574 |
: ConstantMax(ConstantMax), IsComplete(IsComplete), MaxOrZero(MaxOrZero) { |
0 |
8574 |
: ConstantMax(ConstantMax), IsComplete(IsComplete), MaxOrZero(MaxOrZero) { |
0 |
| 8575 |
using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo; |
--- |
8575 |
using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo; |
--- |
| 8576 |
|
--- |
8576 |
|
--- |
| 8577 |
ExitNotTaken.reserve(ExitCounts.size()); |
0 |
8577 |
ExitNotTaken.reserve(ExitCounts.size()); |
0 |
| 8578 |
std::transform(ExitCounts.begin(), ExitCounts.end(), |
0 |
8578 |
std::transform(ExitCounts.begin(), ExitCounts.end(), |
0 |
| 8579 |
std::back_inserter(ExitNotTaken), |
0 |
8579 |
std::back_inserter(ExitNotTaken), |
0 |
| 8580 |
[&](const EdgeExitInfo &EEI) { |
0 |
8580 |
[&](const EdgeExitInfo &EEI) { |
0 |
| 8581 |
BasicBlock *ExitBB = EEI.first; |
0 |
8581 |
BasicBlock *ExitBB = EEI.first; |
0 |
| 8582 |
const ExitLimit &EL = EEI.second; |
0 |
8582 |
const ExitLimit &EL = EEI.second; |
0 |
| 8583 |
return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken, |
0 |
8583 |
return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken, |
0 |
| 8584 |
EL.ConstantMaxNotTaken, EL.SymbolicMaxNotTaken, |
0 |
8584 |
EL.ConstantMaxNotTaken, EL.SymbolicMaxNotTaken, |
0 |
| 8585 |
EL.Predicates); |
0 |
8585 |
EL.Predicates); |
0 |
| 8586 |
}); |
--- |
8586 |
}); |
--- |
| 8587 |
assert((isa(ConstantMax) || |
0 |
8587 |
assert((isa(ConstantMax) || |
0 |
| 8588 |
isa(ConstantMax)) && |
--- |
8588 |
isa(ConstantMax)) && |
--- |
| 8589 |
"No point in having a non-constant max backedge taken count!"); |
--- |
8589 |
"No point in having a non-constant max backedge taken count!"); |
--- |
| 8590 |
} |
0 |
8590 |
} |
0 |
| 8591 |
|
--- |
8591 |
|
--- |
| 8592 |
/// Compute the number of times the backedge of the specified loop will execute. |
--- |
8592 |
/// Compute the number of times the backedge of the specified loop will execute. |
--- |
| 8593 |
ScalarEvolution::BackedgeTakenInfo |
--- |
8593 |
ScalarEvolution::BackedgeTakenInfo |
--- |
| 8594 |
ScalarEvolution::computeBackedgeTakenCount(const Loop *L, |
0 |
8594 |
ScalarEvolution::computeBackedgeTakenCount(const Loop *L, |
0 |
| 8595 |
bool AllowPredicates) { |
--- |
8595 |
bool AllowPredicates) { |
--- |
| 8596 |
SmallVector ExitingBlocks; |
0 |
8596 |
SmallVector ExitingBlocks; |
0 |
| 8597 |
L->getExitingBlocks(ExitingBlocks); |
0 |
8597 |
L->getExitingBlocks(ExitingBlocks); |
0 |
| 8598 |
|
--- |
8598 |
|
--- |
| 8599 |
using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo; |
--- |
8599 |
using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo; |
--- |
| 8600 |
|
--- |
8600 |
|
--- |
| 8601 |
SmallVector ExitCounts; |
0 |
8601 |
SmallVector ExitCounts; |
0 |
| 8602 |
bool CouldComputeBECount = true; |
0 |
8602 |
bool CouldComputeBECount = true; |
0 |
| 8603 |
BasicBlock *Latch = L->getLoopLatch(); // may be NULL. |
0 |
8603 |
BasicBlock *Latch = L->getLoopLatch(); // may be NULL. |
0 |
| 8604 |
const SCEV *MustExitMaxBECount = nullptr; |
0 |
8604 |
const SCEV *MustExitMaxBECount = nullptr; |
0 |
| 8605 |
const SCEV *MayExitMaxBECount = nullptr; |
0 |
8605 |
const SCEV *MayExitMaxBECount = nullptr; |
0 |
| 8606 |
bool MustExitMaxOrZero = false; |
0 |
8606 |
bool MustExitMaxOrZero = false; |
0 |
| 8607 |
|
--- |
8607 |
|
--- |
| 8608 |
// Compute the ExitLimit for each loop exit. Use this to populate ExitCounts |
--- |
8608 |
// Compute the ExitLimit for each loop exit. Use this to populate ExitCounts |
--- |
| 8609 |
// and compute maxBECount. |
--- |
8609 |
// and compute maxBECount. |
--- |
| 8610 |
// Do a union of all the predicates here. |
--- |
8610 |
// Do a union of all the predicates here. |
--- |
| 8611 |
for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { |
0 |
8611 |
for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { |
0 |
| 8612 |
BasicBlock *ExitBB = ExitingBlocks[i]; |
0 |
8612 |
BasicBlock *ExitBB = ExitingBlocks[i]; |
0 |
| 8613 |
|
--- |
8613 |
|
--- |
| 8614 |
// We canonicalize untaken exits to br (constant), ignore them so that |
--- |
8614 |
// We canonicalize untaken exits to br (constant), ignore them so that |
--- |
| 8615 |
// proving an exit untaken doesn't negatively impact our ability to reason |
--- |
8615 |
// proving an exit untaken doesn't negatively impact our ability to reason |
--- |
| 8616 |
// about the loop as whole. |
--- |
8616 |
// about the loop as whole. |
--- |
| 8617 |
if (auto *BI = dyn_cast(ExitBB->getTerminator())) |
0 |
8617 |
if (auto *BI = dyn_cast(ExitBB->getTerminator())) |
0 |
| 8618 |
if (auto *CI = dyn_cast(BI->getCondition())) { |
0 |
8618 |
if (auto *CI = dyn_cast(BI->getCondition())) { |
0 |
| 8619 |
bool ExitIfTrue = !L->contains(BI->getSuccessor(0)); |
0 |
8619 |
bool ExitIfTrue = !L->contains(BI->getSuccessor(0)); |
0 |
| 8620 |
if (ExitIfTrue == CI->isZero()) |
0 |
8620 |
if (ExitIfTrue == CI->isZero()) |
0 |
| 8621 |
continue; |
0 |
8621 |
continue; |
0 |
| 8622 |
} |
--- |
8622 |
} |
--- |
| 8623 |
|
--- |
8623 |
|
--- |
| 8624 |
ExitLimit EL = computeExitLimit(L, ExitBB, AllowPredicates); |
0 |
8624 |
ExitLimit EL = computeExitLimit(L, ExitBB, AllowPredicates); |
0 |
| 8625 |
|
--- |
8625 |
|
--- |
| 8626 |
assert((AllowPredicates || EL.Predicates.empty()) && |
0 |
8626 |
assert((AllowPredicates || EL.Predicates.empty()) && |
0 |
| 8627 |
"Predicated exit limit when predicates are not allowed!"); |
--- |
8627 |
"Predicated exit limit when predicates are not allowed!"); |
--- |
| 8628 |
|
--- |
8628 |
|
--- |
| 8629 |
// 1. For each exit that can be computed, add an entry to ExitCounts. |
--- |
8629 |
// 1. For each exit that can be computed, add an entry to ExitCounts. |
--- |
| 8630 |
// CouldComputeBECount is true only if all exits can be computed. |
--- |
8630 |
// CouldComputeBECount is true only if all exits can be computed. |
--- |
| 8631 |
if (EL.ExactNotTaken != getCouldNotCompute()) |
0 |
8631 |
if (EL.ExactNotTaken != getCouldNotCompute()) |
0 |
| 8632 |
++NumExitCountsComputed; |
0 |
8632 |
++NumExitCountsComputed; |
0 |
| 8633 |
else |
--- |
8633 |
else |
--- |
| 8634 |
// We couldn't compute an exact value for this exit, so |
--- |
8634 |
// We couldn't compute an exact value for this exit, so |
--- |
| 8635 |
// we won't be able to compute an exact value for the loop. |
--- |
8635 |
// we won't be able to compute an exact value for the loop. |
--- |
| 8636 |
CouldComputeBECount = false; |
0 |
8636 |
CouldComputeBECount = false; |
0 |
| 8637 |
// Remember exit count if either exact or symbolic is known. Because |
--- |
8637 |
// Remember exit count if either exact or symbolic is known. Because |
--- |
| 8638 |
// Exact always implies symbolic, only check symbolic. |
--- |
8638 |
// Exact always implies symbolic, only check symbolic. |
--- |
| 8639 |
if (EL.SymbolicMaxNotTaken != getCouldNotCompute()) |
0 |
8639 |
if (EL.SymbolicMaxNotTaken != getCouldNotCompute()) |
0 |
| 8640 |
ExitCounts.emplace_back(ExitBB, EL); |
0 |
8640 |
ExitCounts.emplace_back(ExitBB, EL); |
0 |
| 8641 |
else { |
--- |
8641 |
else { |
--- |
| 8642 |
assert(EL.ExactNotTaken == getCouldNotCompute() && |
0 |
8642 |
assert(EL.ExactNotTaken == getCouldNotCompute() && |
0 |
| 8643 |
"Exact is known but symbolic isn't?"); |
--- |
8643 |
"Exact is known but symbolic isn't?"); |
--- |
| 8644 |
++NumExitCountsNotComputed; |
0 |
8644 |
++NumExitCountsNotComputed; |
0 |
| 8645 |
} |
--- |
8645 |
} |
--- |
| 8646 |
|
--- |
8646 |
|
--- |
| 8647 |
// 2. Derive the loop's MaxBECount from each exit's max number of |
--- |
8647 |
// 2. Derive the loop's MaxBECount from each exit's max number of |
--- |
| 8648 |
// non-exiting iterations. Partition the loop exits into two kinds: |
--- |
8648 |
// non-exiting iterations. Partition the loop exits into two kinds: |
--- |
| 8649 |
// LoopMustExits and LoopMayExits. |
--- |
8649 |
// LoopMustExits and LoopMayExits. |
--- |
| 8650 |
// |
--- |
8650 |
// |
--- |
| 8651 |
// If the exit dominates the loop latch, it is a LoopMustExit otherwise it |
--- |
8651 |
// If the exit dominates the loop latch, it is a LoopMustExit otherwise it |
--- |
| 8652 |
// is a LoopMayExit. If any computable LoopMustExit is found, then |
--- |
8652 |
// is a LoopMayExit. If any computable LoopMustExit is found, then |
--- |
| 8653 |
// MaxBECount is the minimum EL.ConstantMaxNotTaken of computable |
--- |
8653 |
// MaxBECount is the minimum EL.ConstantMaxNotTaken of computable |
--- |
| 8654 |
// LoopMustExits. Otherwise, MaxBECount is conservatively the maximum |
--- |
8654 |
// LoopMustExits. Otherwise, MaxBECount is conservatively the maximum |
--- |
| 8655 |
// EL.ConstantMaxNotTaken, where CouldNotCompute is considered greater than |
--- |
8655 |
// EL.ConstantMaxNotTaken, where CouldNotCompute is considered greater than |
--- |
| 8656 |
// any |
--- |
8656 |
// any |
--- |
| 8657 |
// computable EL.ConstantMaxNotTaken. |
--- |
8657 |
// computable EL.ConstantMaxNotTaken. |
--- |
| 8658 |
if (EL.ConstantMaxNotTaken != getCouldNotCompute() && Latch && |
0 |
8658 |
if (EL.ConstantMaxNotTaken != getCouldNotCompute() && Latch && |
0 |
| 8659 |
DT.dominates(ExitBB, Latch)) { |
0 |
8659 |
DT.dominates(ExitBB, Latch)) { |
0 |
| 8660 |
if (!MustExitMaxBECount) { |
0 |
8660 |
if (!MustExitMaxBECount) { |
0 |
| 8661 |
MustExitMaxBECount = EL.ConstantMaxNotTaken; |
0 |
8661 |
MustExitMaxBECount = EL.ConstantMaxNotTaken; |
0 |
| 8662 |
MustExitMaxOrZero = EL.MaxOrZero; |
0 |
8662 |
MustExitMaxOrZero = EL.MaxOrZero; |
0 |
| 8663 |
} else { |
--- |
8663 |
} else { |
--- |
| 8664 |
MustExitMaxBECount = getUMinFromMismatchedTypes(MustExitMaxBECount, |
0 |
8664 |
MustExitMaxBECount = getUMinFromMismatchedTypes(MustExitMaxBECount, |
0 |
| 8665 |
EL.ConstantMaxNotTaken); |
--- |
8665 |
EL.ConstantMaxNotTaken); |
--- |
| 8666 |
} |
--- |
8666 |
} |
--- |
| 8667 |
} else if (MayExitMaxBECount != getCouldNotCompute()) { |
0 |
8667 |
} else if (MayExitMaxBECount != getCouldNotCompute()) { |
0 |
| 8668 |
if (!MayExitMaxBECount || EL.ConstantMaxNotTaken == getCouldNotCompute()) |
0 |
8668 |
if (!MayExitMaxBECount || EL.ConstantMaxNotTaken == getCouldNotCompute()) |
0 |
| 8669 |
MayExitMaxBECount = EL.ConstantMaxNotTaken; |
0 |
8669 |
MayExitMaxBECount = EL.ConstantMaxNotTaken; |
0 |
| 8670 |
else { |
--- |
8670 |
else { |
--- |
| 8671 |
MayExitMaxBECount = getUMaxFromMismatchedTypes(MayExitMaxBECount, |
0 |
8671 |
MayExitMaxBECount = getUMaxFromMismatchedTypes(MayExitMaxBECount, |
0 |
| 8672 |
EL.ConstantMaxNotTaken); |
--- |
8672 |
EL.ConstantMaxNotTaken); |
--- |
| 8673 |
} |
--- |
8673 |
} |
--- |
| 8674 |
} |
--- |
8674 |
} |
--- |
| 8675 |
} |
0 |
8675 |
} |
0 |
| 8676 |
const SCEV *MaxBECount = MustExitMaxBECount ? MustExitMaxBECount : |
0 |
8676 |
const SCEV *MaxBECount = MustExitMaxBECount ? MustExitMaxBECount : |
0 |
| 8677 |
(MayExitMaxBECount ? MayExitMaxBECount : getCouldNotCompute()); |
0 |
8677 |
(MayExitMaxBECount ? MayExitMaxBECount : getCouldNotCompute()); |
0 |
| 8678 |
// The loop backedge will be taken the maximum or zero times if there's |
--- |
8678 |
// The loop backedge will be taken the maximum or zero times if there's |
--- |
| 8679 |
// a single exit that must be taken the maximum or zero times. |
--- |
8679 |
// a single exit that must be taken the maximum or zero times. |
--- |
| 8680 |
bool MaxOrZero = (MustExitMaxOrZero && ExitingBlocks.size() == 1); |
0 |
8680 |
bool MaxOrZero = (MustExitMaxOrZero && ExitingBlocks.size() == 1); |
0 |
| 8681 |
|
--- |
8681 |
|
--- |
| 8682 |
// Remember which SCEVs are used in exit limits for invalidation purposes. |
--- |
8682 |
// Remember which SCEVs are used in exit limits for invalidation purposes. |
--- |
| 8683 |
// We only care about non-constant SCEVs here, so we can ignore |
--- |
8683 |
// We only care about non-constant SCEVs here, so we can ignore |
--- |
| 8684 |
// EL.ConstantMaxNotTaken |
--- |
8684 |
// EL.ConstantMaxNotTaken |
--- |
| 8685 |
// and MaxBECount, which must be SCEVConstant. |
--- |
8685 |
// and MaxBECount, which must be SCEVConstant. |
--- |
| 8686 |
for (const auto &Pair : ExitCounts) { |
0 |
8686 |
for (const auto &Pair : ExitCounts) { |
0 |
| 8687 |
if (!isa(Pair.second.ExactNotTaken)) |
0 |
8687 |
if (!isa(Pair.second.ExactNotTaken)) |
0 |
| 8688 |
BECountUsers[Pair.second.ExactNotTaken].insert({L, AllowPredicates}); |
0 |
8688 |
BECountUsers[Pair.second.ExactNotTaken].insert({L, AllowPredicates}); |
0 |
| 8689 |
if (!isa(Pair.second.SymbolicMaxNotTaken)) |
0 |
8689 |
if (!isa(Pair.second.SymbolicMaxNotTaken)) |
0 |
| 8690 |
BECountUsers[Pair.second.SymbolicMaxNotTaken].insert( |
0 |
8690 |
BECountUsers[Pair.second.SymbolicMaxNotTaken].insert( |
0 |
| 8691 |
{L, AllowPredicates}); |
--- |
8691 |
{L, AllowPredicates}); |
--- |
| 8692 |
} |
--- |
8692 |
} |
--- |
| 8693 |
return BackedgeTakenInfo(std::move(ExitCounts), CouldComputeBECount, |
0 |
8693 |
return BackedgeTakenInfo(std::move(ExitCounts), CouldComputeBECount, |
0 |
| 8694 |
MaxBECount, MaxOrZero); |
0 |
8694 |
MaxBECount, MaxOrZero); |
0 |
| 8695 |
} |
0 |
8695 |
} |
0 |
| 8696 |
|
--- |
8696 |
|
--- |
| 8697 |
ScalarEvolution::ExitLimit |
--- |
8697 |
ScalarEvolution::ExitLimit |
--- |
| 8698 |
ScalarEvolution::computeExitLimit(const Loop *L, BasicBlock *ExitingBlock, |
0 |
8698 |
ScalarEvolution::computeExitLimit(const Loop *L, BasicBlock *ExitingBlock, |
0 |
| 8699 |
bool AllowPredicates) { |
--- |
8699 |
bool AllowPredicates) { |
--- |
| 8700 |
assert(L->contains(ExitingBlock) && "Exit count for non-loop block?"); |
0 |
8700 |
assert(L->contains(ExitingBlock) && "Exit count for non-loop block?"); |
0 |
| 8701 |
// If our exiting block does not dominate the latch, then its connection with |
--- |
8701 |
// If our exiting block does not dominate the latch, then its connection with |
--- |
| 8702 |
// loop's exit limit may be far from trivial. |
--- |
8702 |
// loop's exit limit may be far from trivial. |
--- |
| 8703 |
const BasicBlock *Latch = L->getLoopLatch(); |
0 |
8703 |
const BasicBlock *Latch = L->getLoopLatch(); |
0 |
| 8704 |
if (!Latch || !DT.dominates(ExitingBlock, Latch)) |
0 |
8704 |
if (!Latch || !DT.dominates(ExitingBlock, Latch)) |
0 |
| 8705 |
return getCouldNotCompute(); |
0 |
8705 |
return getCouldNotCompute(); |
0 |
| 8706 |
|
--- |
8706 |
|
--- |
| 8707 |
bool IsOnlyExit = (L->getExitingBlock() != nullptr); |
0 |
8707 |
bool IsOnlyExit = (L->getExitingBlock() != nullptr); |
0 |
| 8708 |
Instruction *Term = ExitingBlock->getTerminator(); |
0 |
8708 |
Instruction *Term = ExitingBlock->getTerminator(); |
0 |
| 8709 |
if (BranchInst *BI = dyn_cast(Term)) { |
0 |
8709 |
if (BranchInst *BI = dyn_cast(Term)) { |
0 |
| 8710 |
assert(BI->isConditional() && "If unconditional, it can't be in loop!"); |
0 |
8710 |
assert(BI->isConditional() && "If unconditional, it can't be in loop!"); |
0 |
| 8711 |
bool ExitIfTrue = !L->contains(BI->getSuccessor(0)); |
0 |
8711 |
bool ExitIfTrue = !L->contains(BI->getSuccessor(0)); |
0 |
| 8712 |
assert(ExitIfTrue == L->contains(BI->getSuccessor(1)) && |
0 |
8712 |
assert(ExitIfTrue == L->contains(BI->getSuccessor(1)) && |
0 |
| 8713 |
"It should have one successor in loop and one exit block!"); |
--- |
8713 |
"It should have one successor in loop and one exit block!"); |
--- |
| 8714 |
// Proceed to the next level to examine the exit condition expression. |
--- |
8714 |
// Proceed to the next level to examine the exit condition expression. |
--- |
| 8715 |
return computeExitLimitFromCond(L, BI->getCondition(), ExitIfTrue, |
--- |
8715 |
return computeExitLimitFromCond(L, BI->getCondition(), ExitIfTrue, |
--- |
| 8716 |
/*ControlsOnlyExit=*/IsOnlyExit, |
--- |
8716 |
/*ControlsOnlyExit=*/IsOnlyExit, |
--- |
| 8717 |
AllowPredicates); |
0 |
8717 |
AllowPredicates); |
0 |
| 8718 |
} |
--- |
8718 |
} |
--- |
| 8719 |
|
--- |
8719 |
|
--- |
| 8720 |
if (SwitchInst *SI = dyn_cast(Term)) { |
0 |
8720 |
if (SwitchInst *SI = dyn_cast(Term)) { |
0 |
| 8721 |
// For switch, make sure that there is a single exit from the loop. |
--- |
8721 |
// For switch, make sure that there is a single exit from the loop. |
--- |
| 8722 |
BasicBlock *Exit = nullptr; |
0 |
8722 |
BasicBlock *Exit = nullptr; |
0 |
| 8723 |
for (auto *SBB : successors(ExitingBlock)) |
0 |
8723 |
for (auto *SBB : successors(ExitingBlock)) |
0 |
| 8724 |
if (!L->contains(SBB)) { |
0 |
8724 |
if (!L->contains(SBB)) { |
0 |
| 8725 |
if (Exit) // Multiple exit successors. |
0 |
8725 |
if (Exit) // Multiple exit successors. |
0 |
| 8726 |
return getCouldNotCompute(); |
0 |
8726 |
return getCouldNotCompute(); |
0 |
| 8727 |
Exit = SBB; |
0 |
8727 |
Exit = SBB; |
0 |
| 8728 |
} |
--- |
8728 |
} |
--- |
| 8729 |
assert(Exit && "Exiting block must have at least one exit"); |
0 |
8729 |
assert(Exit && "Exiting block must have at least one exit"); |
0 |
| 8730 |
return computeExitLimitFromSingleExitSwitch( |
--- |
8730 |
return computeExitLimitFromSingleExitSwitch( |
--- |
| 8731 |
L, SI, Exit, |
--- |
8731 |
L, SI, Exit, |
--- |
| 8732 |
/*ControlsOnlyExit=*/IsOnlyExit); |
0 |
8732 |
/*ControlsOnlyExit=*/IsOnlyExit); |
0 |
| 8733 |
} |
--- |
8733 |
} |
--- |
| 8734 |
|
--- |
8734 |
|
--- |
| 8735 |
return getCouldNotCompute(); |
0 |
8735 |
return getCouldNotCompute(); |
0 |
| 8736 |
} |
--- |
8736 |
} |
--- |
| 8737 |
|
--- |
8737 |
|
--- |
| 8738 |
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCond( |
0 |
8738 |
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCond( |
0 |
| 8739 |
const Loop *L, Value *ExitCond, bool ExitIfTrue, bool ControlsOnlyExit, |
--- |
8739 |
const Loop *L, Value *ExitCond, bool ExitIfTrue, bool ControlsOnlyExit, |
--- |
| 8740 |
bool AllowPredicates) { |
--- |
8740 |
bool AllowPredicates) { |
--- |
| 8741 |
ScalarEvolution::ExitLimitCacheTy Cache(L, ExitIfTrue, AllowPredicates); |
0 |
8741 |
ScalarEvolution::ExitLimitCacheTy Cache(L, ExitIfTrue, AllowPredicates); |
0 |
| 8742 |
return computeExitLimitFromCondCached(Cache, L, ExitCond, ExitIfTrue, |
--- |
8742 |
return computeExitLimitFromCondCached(Cache, L, ExitCond, ExitIfTrue, |
--- |
| 8743 |
ControlsOnlyExit, AllowPredicates); |
0 |
8743 |
ControlsOnlyExit, AllowPredicates); |
0 |
| 8744 |
} |
0 |
8744 |
} |
0 |
| 8745 |
|
--- |
8745 |
|
--- |
| 8746 |
std::optional |
--- |
8746 |
std::optional |
--- |
| 8747 |
ScalarEvolution::ExitLimitCache::find(const Loop *L, Value *ExitCond, |
0 |
8747 |
ScalarEvolution::ExitLimitCache::find(const Loop *L, Value *ExitCond, |
0 |
| 8748 |
bool ExitIfTrue, bool ControlsOnlyExit, |
--- |
8748 |
bool ExitIfTrue, bool ControlsOnlyExit, |
--- |
| 8749 |
bool AllowPredicates) { |
--- |
8749 |
bool AllowPredicates) { |
--- |
| 8750 |
(void)this->L; |
--- |
8750 |
(void)this->L; |
--- |
| 8751 |
(void)this->ExitIfTrue; |
--- |
8751 |
(void)this->ExitIfTrue; |
--- |
| 8752 |
(void)this->AllowPredicates; |
--- |
8752 |
(void)this->AllowPredicates; |
--- |
| 8753 |
|
--- |
8753 |
|
--- |
| 8754 |
assert(this->L == L && this->ExitIfTrue == ExitIfTrue && |
0 |
8754 |
assert(this->L == L && this->ExitIfTrue == ExitIfTrue && |
0 |
| 8755 |
this->AllowPredicates == AllowPredicates && |
--- |
8755 |
this->AllowPredicates == AllowPredicates && |
--- |
| 8756 |
"Variance in assumed invariant key components!"); |
--- |
8756 |
"Variance in assumed invariant key components!"); |
--- |
| 8757 |
auto Itr = TripCountMap.find({ExitCond, ControlsOnlyExit}); |
0 |
8757 |
auto Itr = TripCountMap.find({ExitCond, ControlsOnlyExit}); |
0 |
| 8758 |
if (Itr == TripCountMap.end()) |
0 |
8758 |
if (Itr == TripCountMap.end()) |
0 |
| 8759 |
return std::nullopt; |
0 |
8759 |
return std::nullopt; |
0 |
| 8760 |
return Itr->second; |
0 |
8760 |
return Itr->second; |
0 |
| 8761 |
} |
--- |
8761 |
} |
--- |
| 8762 |
|
--- |
8762 |
|
--- |
| 8763 |
void ScalarEvolution::ExitLimitCache::insert(const Loop *L, Value *ExitCond, |
0 |
8763 |
void ScalarEvolution::ExitLimitCache::insert(const Loop *L, Value *ExitCond, |
0 |
| 8764 |
bool ExitIfTrue, |
--- |
8764 |
bool ExitIfTrue, |
--- |
| 8765 |
bool ControlsOnlyExit, |
--- |
8765 |
bool ControlsOnlyExit, |
--- |
| 8766 |
bool AllowPredicates, |
--- |
8766 |
bool AllowPredicates, |
--- |
| 8767 |
const ExitLimit &EL) { |
--- |
8767 |
const ExitLimit &EL) { |
--- |
| 8768 |
assert(this->L == L && this->ExitIfTrue == ExitIfTrue && |
0 |
8768 |
assert(this->L == L && this->ExitIfTrue == ExitIfTrue && |
0 |
| 8769 |
this->AllowPredicates == AllowPredicates && |
--- |
8769 |
this->AllowPredicates == AllowPredicates && |
--- |
| 8770 |
"Variance in assumed invariant key components!"); |
--- |
8770 |
"Variance in assumed invariant key components!"); |
--- |
| 8771 |
|
--- |
8771 |
|
--- |
| 8772 |
auto InsertResult = TripCountMap.insert({{ExitCond, ControlsOnlyExit}, EL}); |
0 |
8772 |
auto InsertResult = TripCountMap.insert({{ExitCond, ControlsOnlyExit}, EL}); |
0 |
| 8773 |
assert(InsertResult.second && "Expected successful insertion!"); |
0 |
8773 |
assert(InsertResult.second && "Expected successful insertion!"); |
0 |
| 8774 |
(void)InsertResult; |
--- |
8774 |
(void)InsertResult; |
--- |
| 8775 |
(void)ExitIfTrue; |
--- |
8775 |
(void)ExitIfTrue; |
--- |
| 8776 |
} |
0 |
8776 |
} |
0 |
| 8777 |
|
--- |
8777 |
|
--- |
| 8778 |
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondCached( |
0 |
8778 |
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondCached( |
0 |
| 8779 |
ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue, |
--- |
8779 |
ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue, |
--- |
| 8780 |
bool ControlsOnlyExit, bool AllowPredicates) { |
--- |
8780 |
bool ControlsOnlyExit, bool AllowPredicates) { |
--- |
| 8781 |
|
--- |
8781 |
|
--- |
| 8782 |
if (auto MaybeEL = Cache.find(L, ExitCond, ExitIfTrue, ControlsOnlyExit, |
0 |
8782 |
if (auto MaybeEL = Cache.find(L, ExitCond, ExitIfTrue, ControlsOnlyExit, |
0 |
| 8783 |
AllowPredicates)) |
0 |
8783 |
AllowPredicates)) |
0 |
| 8784 |
return *MaybeEL; |
0 |
8784 |
return *MaybeEL; |
0 |
| 8785 |
|
--- |
8785 |
|
--- |
| 8786 |
ExitLimit EL = computeExitLimitFromCondImpl( |
--- |
8786 |
ExitLimit EL = computeExitLimitFromCondImpl( |
--- |
| 8787 |
Cache, L, ExitCond, ExitIfTrue, ControlsOnlyExit, AllowPredicates); |
0 |
8787 |
Cache, L, ExitCond, ExitIfTrue, ControlsOnlyExit, AllowPredicates); |
0 |
| 8788 |
Cache.insert(L, ExitCond, ExitIfTrue, ControlsOnlyExit, AllowPredicates, EL); |
0 |
8788 |
Cache.insert(L, ExitCond, ExitIfTrue, ControlsOnlyExit, AllowPredicates, EL); |
0 |
| 8789 |
return EL; |
0 |
8789 |
return EL; |
0 |
| 8790 |
} |
0 |
8790 |
} |
0 |
| 8791 |
|
--- |
8791 |
|
--- |
| 8792 |
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondImpl( |
0 |
8792 |
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondImpl( |
0 |
| 8793 |
ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue, |
--- |
8793 |
ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue, |
--- |
| 8794 |
bool ControlsOnlyExit, bool AllowPredicates) { |
--- |
8794 |
bool ControlsOnlyExit, bool AllowPredicates) { |
--- |
| 8795 |
// Handle BinOp conditions (And, Or). |
--- |
8795 |
// Handle BinOp conditions (And, Or). |
--- |
| 8796 |
if (auto LimitFromBinOp = computeExitLimitFromCondFromBinOp( |
0 |
8796 |
if (auto LimitFromBinOp = computeExitLimitFromCondFromBinOp( |
0 |
| 8797 |
Cache, L, ExitCond, ExitIfTrue, ControlsOnlyExit, AllowPredicates)) |
0 |
8797 |
Cache, L, ExitCond, ExitIfTrue, ControlsOnlyExit, AllowPredicates)) |
0 |
| 8798 |
return *LimitFromBinOp; |
0 |
8798 |
return *LimitFromBinOp; |
0 |
| 8799 |
|
--- |
8799 |
|
--- |
| 8800 |
// With an icmp, it may be feasible to compute an exact backedge-taken count. |
--- |
8800 |
// With an icmp, it may be feasible to compute an exact backedge-taken count. |
--- |
| 8801 |
// Proceed to the next level to examine the icmp. |
--- |
8801 |
// Proceed to the next level to examine the icmp. |
--- |
| 8802 |
if (ICmpInst *ExitCondICmp = dyn_cast(ExitCond)) { |
0 |
8802 |
if (ICmpInst *ExitCondICmp = dyn_cast(ExitCond)) { |
0 |
| 8803 |
ExitLimit EL = |
--- |
8803 |
ExitLimit EL = |
--- |
| 8804 |
computeExitLimitFromICmp(L, ExitCondICmp, ExitIfTrue, ControlsOnlyExit); |
0 |
8804 |
computeExitLimitFromICmp(L, ExitCondICmp, ExitIfTrue, ControlsOnlyExit); |
0 |
| 8805 |
if (EL.hasFullInfo() || !AllowPredicates) |
0 |
8805 |
if (EL.hasFullInfo() || !AllowPredicates) |
0 |
| 8806 |
return EL; |
0 |
8806 |
return EL; |
0 |
| 8807 |
|
--- |
8807 |
|
--- |
| 8808 |
// Try again, but use SCEV predicates this time. |
--- |
8808 |
// Try again, but use SCEV predicates this time. |
--- |
| 8809 |
return computeExitLimitFromICmp(L, ExitCondICmp, ExitIfTrue, |
--- |
8809 |
return computeExitLimitFromICmp(L, ExitCondICmp, ExitIfTrue, |
--- |
| 8810 |
ControlsOnlyExit, |
--- |
8810 |
ControlsOnlyExit, |
--- |
| 8811 |
/*AllowPredicates=*/true); |
0 |
8811 |
/*AllowPredicates=*/true); |
0 |
| 8812 |
} |
0 |
8812 |
} |
0 |
| 8813 |
|
--- |
8813 |
|
--- |
| 8814 |
// Check for a constant condition. These are normally stripped out by |
--- |
8814 |
// Check for a constant condition. These are normally stripped out by |
--- |
| 8815 |
// SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to |
--- |
8815 |
// SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to |
--- |
| 8816 |
// preserve the CFG and is temporarily leaving constant conditions |
--- |
8816 |
// preserve the CFG and is temporarily leaving constant conditions |
--- |
| 8817 |
// in place. |
--- |
8817 |
// in place. |
--- |
| 8818 |
if (ConstantInt *CI = dyn_cast(ExitCond)) { |
0 |
8818 |
if (ConstantInt *CI = dyn_cast(ExitCond)) { |
0 |
| 8819 |
if (ExitIfTrue == !CI->getZExtValue()) |
0 |
8819 |
if (ExitIfTrue == !CI->getZExtValue()) |
0 |
| 8820 |
// The backedge is always taken. |
--- |
8820 |
// The backedge is always taken. |
--- |
| 8821 |
return getCouldNotCompute(); |
0 |
8821 |
return getCouldNotCompute(); |
0 |
| 8822 |
// The backedge is never taken. |
--- |
8822 |
// The backedge is never taken. |
--- |
| 8823 |
return getZero(CI->getType()); |
0 |
8823 |
return getZero(CI->getType()); |
0 |
| 8824 |
} |
--- |
8824 |
} |
--- |
| 8825 |
|
--- |
8825 |
|
--- |
| 8826 |
// If we're exiting based on the overflow flag of an x.with.overflow intrinsic |
--- |
8826 |
// If we're exiting based on the overflow flag of an x.with.overflow intrinsic |
--- |
| 8827 |
// with a constant step, we can form an equivalent icmp predicate and figure |
--- |
8827 |
// with a constant step, we can form an equivalent icmp predicate and figure |
--- |
| 8828 |
// out how many iterations will be taken before we exit. |
--- |
8828 |
// out how many iterations will be taken before we exit. |
--- |
| 8829 |
const WithOverflowInst *WO; |
--- |
8829 |
const WithOverflowInst *WO; |
--- |
| 8830 |
const APInt *C; |
--- |
8830 |
const APInt *C; |
--- |
| 8831 |
if (match(ExitCond, m_ExtractValue<1>(m_WithOverflowInst(WO))) && |
0 |
8831 |
if (match(ExitCond, m_ExtractValue<1>(m_WithOverflowInst(WO))) && |
0 |
| 8832 |
match(WO->getRHS(), m_APInt(C))) { |
0 |
8832 |
match(WO->getRHS(), m_APInt(C))) { |
0 |
| 8833 |
ConstantRange NWR = |
--- |
8833 |
ConstantRange NWR = |
--- |
| 8834 |
ConstantRange::makeExactNoWrapRegion(WO->getBinaryOp(), *C, |
--- |
8834 |
ConstantRange::makeExactNoWrapRegion(WO->getBinaryOp(), *C, |
--- |
| 8835 |
WO->getNoWrapKind()); |
0 |
8835 |
WO->getNoWrapKind()); |
0 |
| 8836 |
CmpInst::Predicate Pred; |
--- |
8836 |
CmpInst::Predicate Pred; |
--- |
| 8837 |
APInt NewRHSC, Offset; |
0 |
8837 |
APInt NewRHSC, Offset; |
0 |
| 8838 |
NWR.getEquivalentICmp(Pred, NewRHSC, Offset); |
0 |
8838 |
NWR.getEquivalentICmp(Pred, NewRHSC, Offset); |
0 |
| 8839 |
if (!ExitIfTrue) |
0 |
8839 |
if (!ExitIfTrue) |
0 |
| 8840 |
Pred = ICmpInst::getInversePredicate(Pred); |
0 |
8840 |
Pred = ICmpInst::getInversePredicate(Pred); |
0 |
| 8841 |
auto *LHS = getSCEV(WO->getLHS()); |
0 |
8841 |
auto *LHS = getSCEV(WO->getLHS()); |
0 |
| 8842 |
if (Offset != 0) |
0 |
8842 |
if (Offset != 0) |
0 |
| 8843 |
LHS = getAddExpr(LHS, getConstant(Offset)); |
0 |
8843 |
LHS = getAddExpr(LHS, getConstant(Offset)); |
0 |
| 8844 |
auto EL = computeExitLimitFromICmp(L, Pred, LHS, getConstant(NewRHSC), |
--- |
8844 |
auto EL = computeExitLimitFromICmp(L, Pred, LHS, getConstant(NewRHSC), |
--- |
| 8845 |
ControlsOnlyExit, AllowPredicates); |
0 |
8845 |
ControlsOnlyExit, AllowPredicates); |
0 |
| 8846 |
if (EL.hasAnyInfo()) |
0 |
8846 |
if (EL.hasAnyInfo()) |
0 |
| 8847 |
return EL; |
0 |
8847 |
return EL; |
0 |
| 8848 |
} |
0 |
8848 |
} |
0 |
| 8849 |
|
--- |
8849 |
|
--- |
| 8850 |
// If it's not an integer or pointer comparison then compute it the hard way. |
--- |
8850 |
// If it's not an integer or pointer comparison then compute it the hard way. |
--- |
| 8851 |
return computeExitCountExhaustively(L, ExitCond, ExitIfTrue); |
0 |
8851 |
return computeExitCountExhaustively(L, ExitCond, ExitIfTrue); |
0 |
| 8852 |
} |
--- |
8852 |
} |
--- |
| 8853 |
|
--- |
8853 |
|
--- |
| 8854 |
std::optional |
--- |
8854 |
std::optional |
--- |
| 8855 |
ScalarEvolution::computeExitLimitFromCondFromBinOp( |
0 |
8855 |
ScalarEvolution::computeExitLimitFromCondFromBinOp( |
0 |
| 8856 |
ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue, |
--- |
8856 |
ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue, |
--- |
| 8857 |
bool ControlsOnlyExit, bool AllowPredicates) { |
--- |
8857 |
bool ControlsOnlyExit, bool AllowPredicates) { |
--- |
| 8858 |
// Check if the controlling expression for this loop is an And or Or. |
--- |
8858 |
// Check if the controlling expression for this loop is an And or Or. |
--- |
| 8859 |
Value *Op0, *Op1; |
--- |
8859 |
Value *Op0, *Op1; |
--- |
| 8860 |
bool IsAnd = false; |
0 |
8860 |
bool IsAnd = false; |
0 |
| 8861 |
if (match(ExitCond, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))) |
0 |
8861 |
if (match(ExitCond, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))) |
0 |
| 8862 |
IsAnd = true; |
0 |
8862 |
IsAnd = true; |
0 |
| 8863 |
else if (match(ExitCond, m_LogicalOr(m_Value(Op0), m_Value(Op1)))) |
0 |
8863 |
else if (match(ExitCond, m_LogicalOr(m_Value(Op0), m_Value(Op1)))) |
0 |
| 8864 |
IsAnd = false; |
0 |
8864 |
IsAnd = false; |
0 |
| 8865 |
else |
--- |
8865 |
else |
--- |
| 8866 |
return std::nullopt; |
0 |
8866 |
return std::nullopt; |
0 |
| 8867 |
|
--- |
8867 |
|
--- |
| 8868 |
// EitherMayExit is true in these two cases: |
--- |
8868 |
// EitherMayExit is true in these two cases: |
--- |
| 8869 |
// br (and Op0 Op1), loop, exit |
--- |
8869 |
// br (and Op0 Op1), loop, exit |
--- |
| 8870 |
// br (or Op0 Op1), exit, loop |
--- |
8870 |
// br (or Op0 Op1), exit, loop |
--- |
| 8871 |
bool EitherMayExit = IsAnd ^ ExitIfTrue; |
0 |
8871 |
bool EitherMayExit = IsAnd ^ ExitIfTrue; |
0 |
| 8872 |
ExitLimit EL0 = computeExitLimitFromCondCached( |
--- |
8872 |
ExitLimit EL0 = computeExitLimitFromCondCached( |
--- |
| 8873 |
Cache, L, Op0, ExitIfTrue, ControlsOnlyExit && !EitherMayExit, |
0 |
8873 |
Cache, L, Op0, ExitIfTrue, ControlsOnlyExit && !EitherMayExit, |
0 |
| 8874 |
AllowPredicates); |
0 |
8874 |
AllowPredicates); |
0 |
| 8875 |
ExitLimit EL1 = computeExitLimitFromCondCached( |
--- |
8875 |
ExitLimit EL1 = computeExitLimitFromCondCached( |
--- |
| 8876 |
Cache, L, Op1, ExitIfTrue, ControlsOnlyExit && !EitherMayExit, |
0 |
8876 |
Cache, L, Op1, ExitIfTrue, ControlsOnlyExit && !EitherMayExit, |
0 |
| 8877 |
AllowPredicates); |
0 |
8877 |
AllowPredicates); |
0 |
| 8878 |
|
--- |
8878 |
|
--- |
| 8879 |
// Be robust against unsimplified IR for the form "op i1 X, NeutralElement" |
--- |
8879 |
// Be robust against unsimplified IR for the form "op i1 X, NeutralElement" |
--- |
| 8880 |
const Constant *NeutralElement = ConstantInt::get(ExitCond->getType(), IsAnd); |
0 |
8880 |
const Constant *NeutralElement = ConstantInt::get(ExitCond->getType(), IsAnd); |
0 |
| 8881 |
if (isa(Op1)) |
0 |
8881 |
if (isa(Op1)) |
0 |
| 8882 |
return Op1 == NeutralElement ? EL0 : EL1; |
0 |
8882 |
return Op1 == NeutralElement ? EL0 : EL1; |
0 |
| 8883 |
if (isa(Op0)) |
0 |
8883 |
if (isa(Op0)) |
0 |
| 8884 |
return Op0 == NeutralElement ? EL1 : EL0; |
0 |
8884 |
return Op0 == NeutralElement ? EL1 : EL0; |
0 |
| 8885 |
|
--- |
8885 |
|
--- |
| 8886 |
const SCEV *BECount = getCouldNotCompute(); |
0 |
8886 |
const SCEV *BECount = getCouldNotCompute(); |
0 |
| 8887 |
const SCEV *ConstantMaxBECount = getCouldNotCompute(); |
0 |
8887 |
const SCEV *ConstantMaxBECount = getCouldNotCompute(); |
0 |
| 8888 |
const SCEV *SymbolicMaxBECount = getCouldNotCompute(); |
0 |
8888 |
const SCEV *SymbolicMaxBECount = getCouldNotCompute(); |
0 |
| 8889 |
if (EitherMayExit) { |
0 |
8889 |
if (EitherMayExit) { |
0 |
| 8890 |
bool UseSequentialUMin = !isa(ExitCond); |
0 |
8890 |
bool UseSequentialUMin = !isa(ExitCond); |
0 |
| 8891 |
// Both conditions must be same for the loop to continue executing. |
--- |
8891 |
// Both conditions must be same for the loop to continue executing. |
--- |
| 8892 |
// Choose the less conservative count. |
--- |
8892 |
// Choose the less conservative count. |
--- |
| 8893 |
if (EL0.ExactNotTaken != getCouldNotCompute() && |
0 |
8893 |
if (EL0.ExactNotTaken != getCouldNotCompute() && |
0 |
| 8894 |
EL1.ExactNotTaken != getCouldNotCompute()) { |
0 |
8894 |
EL1.ExactNotTaken != getCouldNotCompute()) { |
0 |
| 8895 |
BECount = getUMinFromMismatchedTypes(EL0.ExactNotTaken, EL1.ExactNotTaken, |
0 |
8895 |
BECount = getUMinFromMismatchedTypes(EL0.ExactNotTaken, EL1.ExactNotTaken, |
0 |
| 8896 |
UseSequentialUMin); |
--- |
8896 |
UseSequentialUMin); |
--- |
| 8897 |
} |
--- |
8897 |
} |
--- |
| 8898 |
if (EL0.ConstantMaxNotTaken == getCouldNotCompute()) |
0 |
8898 |
if (EL0.ConstantMaxNotTaken == getCouldNotCompute()) |
0 |
| 8899 |
ConstantMaxBECount = EL1.ConstantMaxNotTaken; |
0 |
8899 |
ConstantMaxBECount = EL1.ConstantMaxNotTaken; |
0 |
| 8900 |
else if (EL1.ConstantMaxNotTaken == getCouldNotCompute()) |
0 |
8900 |
else if (EL1.ConstantMaxNotTaken == getCouldNotCompute()) |
0 |
| 8901 |
ConstantMaxBECount = EL0.ConstantMaxNotTaken; |
0 |
8901 |
ConstantMaxBECount = EL0.ConstantMaxNotTaken; |
0 |
| 8902 |
else |
--- |
8902 |
else |
--- |
| 8903 |
ConstantMaxBECount = getUMinFromMismatchedTypes(EL0.ConstantMaxNotTaken, |
0 |
8903 |
ConstantMaxBECount = getUMinFromMismatchedTypes(EL0.ConstantMaxNotTaken, |
0 |
| 8904 |
EL1.ConstantMaxNotTaken); |
--- |
8904 |
EL1.ConstantMaxNotTaken); |
--- |
| 8905 |
if (EL0.SymbolicMaxNotTaken == getCouldNotCompute()) |
0 |
8905 |
if (EL0.SymbolicMaxNotTaken == getCouldNotCompute()) |
0 |
| 8906 |
SymbolicMaxBECount = EL1.SymbolicMaxNotTaken; |
0 |
8906 |
SymbolicMaxBECount = EL1.SymbolicMaxNotTaken; |
0 |
| 8907 |
else if (EL1.SymbolicMaxNotTaken == getCouldNotCompute()) |
0 |
8907 |
else if (EL1.SymbolicMaxNotTaken == getCouldNotCompute()) |
0 |
| 8908 |
SymbolicMaxBECount = EL0.SymbolicMaxNotTaken; |
0 |
8908 |
SymbolicMaxBECount = EL0.SymbolicMaxNotTaken; |
0 |
| 8909 |
else |
--- |
8909 |
else |
--- |
| 8910 |
SymbolicMaxBECount = getUMinFromMismatchedTypes( |
0 |
8910 |
SymbolicMaxBECount = getUMinFromMismatchedTypes( |
0 |
| 8911 |
EL0.SymbolicMaxNotTaken, EL1.SymbolicMaxNotTaken, UseSequentialUMin); |
--- |
8911 |
EL0.SymbolicMaxNotTaken, EL1.SymbolicMaxNotTaken, UseSequentialUMin); |
--- |
| 8912 |
} else { |
--- |
8912 |
} else { |
--- |
| 8913 |
// Both conditions must be same at the same time for the loop to exit. |
--- |
8913 |
// Both conditions must be same at the same time for the loop to exit. |
--- |
| 8914 |
// For now, be conservative. |
--- |
8914 |
// For now, be conservative. |
--- |
| 8915 |
if (EL0.ExactNotTaken == EL1.ExactNotTaken) |
0 |
8915 |
if (EL0.ExactNotTaken == EL1.ExactNotTaken) |
0 |
| 8916 |
BECount = EL0.ExactNotTaken; |
0 |
8916 |
BECount = EL0.ExactNotTaken; |
0 |
| 8917 |
} |
--- |
8917 |
} |
--- |
| 8918 |
|
--- |
8918 |
|
--- |
| 8919 |
// There are cases (e.g. PR26207) where computeExitLimitFromCond is able |
--- |
8919 |
// There are cases (e.g. PR26207) where computeExitLimitFromCond is able |
--- |
| 8920 |
// to be more aggressive when computing BECount than when computing |
--- |
8920 |
// to be more aggressive when computing BECount than when computing |
--- |
| 8921 |
// ConstantMaxBECount. In these cases it is possible for EL0.ExactNotTaken |
--- |
8921 |
// ConstantMaxBECount. In these cases it is possible for EL0.ExactNotTaken |
--- |
| 8922 |
// and |
--- |
8922 |
// and |
--- |
| 8923 |
// EL1.ExactNotTaken to match, but for EL0.ConstantMaxNotTaken and |
--- |
8923 |
// EL1.ExactNotTaken to match, but for EL0.ConstantMaxNotTaken and |
--- |
| 8924 |
// EL1.ConstantMaxNotTaken to not. |
--- |
8924 |
// EL1.ConstantMaxNotTaken to not. |
--- |
| 8925 |
if (isa(ConstantMaxBECount) && |
0 |
8925 |
if (isa(ConstantMaxBECount) && |
0 |
| 8926 |
!isa(BECount)) |
0 |
8926 |
!isa(BECount)) |
0 |
| 8927 |
ConstantMaxBECount = getConstant(getUnsignedRangeMax(BECount)); |
0 |
8927 |
ConstantMaxBECount = getConstant(getUnsignedRangeMax(BECount)); |
0 |
| 8928 |
if (isa(SymbolicMaxBECount)) |
0 |
8928 |
if (isa(SymbolicMaxBECount)) |
0 |
| 8929 |
SymbolicMaxBECount = |
0 |
8929 |
SymbolicMaxBECount = |
0 |
| 8930 |
isa(BECount) ? ConstantMaxBECount : BECount; |
0 |
8930 |
isa(BECount) ? ConstantMaxBECount : BECount; |
0 |
| 8931 |
return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount, false, |
0 |
8931 |
return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount, false, |
0 |
| 8932 |
{ &EL0.Predicates, &EL1.Predicates }); |
0 |
8932 |
{ &EL0.Predicates, &EL1.Predicates }); |
0 |
| 8933 |
} |
0 |
8933 |
} |
0 |
| 8934 |
|
--- |
8934 |
|
--- |
| 8935 |
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp( |
0 |
8935 |
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp( |
0 |
| 8936 |
const Loop *L, ICmpInst *ExitCond, bool ExitIfTrue, bool ControlsOnlyExit, |
--- |
8936 |
const Loop *L, ICmpInst *ExitCond, bool ExitIfTrue, bool ControlsOnlyExit, |
--- |
| 8937 |
bool AllowPredicates) { |
--- |
8937 |
bool AllowPredicates) { |
--- |
| 8938 |
// If the condition was exit on true, convert the condition to exit on false |
--- |
8938 |
// If the condition was exit on true, convert the condition to exit on false |
--- |
| 8939 |
ICmpInst::Predicate Pred; |
--- |
8939 |
ICmpInst::Predicate Pred; |
--- |
| 8940 |
if (!ExitIfTrue) |
0 |
8940 |
if (!ExitIfTrue) |
0 |
| 8941 |
Pred = ExitCond->getPredicate(); |
0 |
8941 |
Pred = ExitCond->getPredicate(); |
0 |
| 8942 |
else |
--- |
8942 |
else |
--- |
| 8943 |
Pred = ExitCond->getInversePredicate(); |
0 |
8943 |
Pred = ExitCond->getInversePredicate(); |
0 |
| 8944 |
const ICmpInst::Predicate OriginalPred = Pred; |
0 |
8944 |
const ICmpInst::Predicate OriginalPred = Pred; |
0 |
| 8945 |
|
--- |
8945 |
|
--- |
| 8946 |
const SCEV *LHS = getSCEV(ExitCond->getOperand(0)); |
0 |
8946 |
const SCEV *LHS = getSCEV(ExitCond->getOperand(0)); |
0 |
| 8947 |
const SCEV *RHS = getSCEV(ExitCond->getOperand(1)); |
0 |
8947 |
const SCEV *RHS = getSCEV(ExitCond->getOperand(1)); |
0 |
| 8948 |
|
--- |
8948 |
|
--- |
| 8949 |
ExitLimit EL = computeExitLimitFromICmp(L, Pred, LHS, RHS, ControlsOnlyExit, |
--- |
8949 |
ExitLimit EL = computeExitLimitFromICmp(L, Pred, LHS, RHS, ControlsOnlyExit, |
--- |
| 8950 |
AllowPredicates); |
0 |
8950 |
AllowPredicates); |
0 |
| 8951 |
if (EL.hasAnyInfo()) |
0 |
8951 |
if (EL.hasAnyInfo()) |
0 |
| 8952 |
return EL; |
0 |
8952 |
return EL; |
0 |
| 8953 |
|
--- |
8953 |
|
--- |
| 8954 |
auto *ExhaustiveCount = |
--- |
8954 |
auto *ExhaustiveCount = |
--- |
| 8955 |
computeExitCountExhaustively(L, ExitCond, ExitIfTrue); |
0 |
8955 |
computeExitCountExhaustively(L, ExitCond, ExitIfTrue); |
0 |
| 8956 |
|
--- |
8956 |
|
--- |
| 8957 |
if (!isa(ExhaustiveCount)) |
0 |
8957 |
if (!isa(ExhaustiveCount)) |
0 |
| 8958 |
return ExhaustiveCount; |
0 |
8958 |
return ExhaustiveCount; |
0 |
| 8959 |
|
--- |
8959 |
|
--- |
| 8960 |
return computeShiftCompareExitLimit(ExitCond->getOperand(0), |
--- |
8960 |
return computeShiftCompareExitLimit(ExitCond->getOperand(0), |
--- |
| 8961 |
ExitCond->getOperand(1), L, OriginalPred); |
0 |
8961 |
ExitCond->getOperand(1), L, OriginalPred); |
0 |
| 8962 |
} |
0 |
8962 |
} |
0 |
| 8963 |
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp( |
0 |
8963 |
ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp( |
0 |
| 8964 |
const Loop *L, ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, |
--- |
8964 |
const Loop *L, ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, |
--- |
| 8965 |
bool ControlsOnlyExit, bool AllowPredicates) { |
--- |
8965 |
bool ControlsOnlyExit, bool AllowPredicates) { |
--- |
| 8966 |
|
--- |
8966 |
|
--- |
| 8967 |
// Try to evaluate any dependencies out of the loop. |
--- |
8967 |
// Try to evaluate any dependencies out of the loop. |
--- |
| 8968 |
LHS = getSCEVAtScope(LHS, L); |
0 |
8968 |
LHS = getSCEVAtScope(LHS, L); |
0 |
| 8969 |
RHS = getSCEVAtScope(RHS, L); |
0 |
8969 |
RHS = getSCEVAtScope(RHS, L); |
0 |
| 8970 |
|
--- |
8970 |
|
--- |
| 8971 |
// At this point, we would like to compute how many iterations of the |
--- |
8971 |
// At this point, we would like to compute how many iterations of the |
--- |
| 8972 |
// loop the predicate will return true for these inputs. |
--- |
8972 |
// loop the predicate will return true for these inputs. |
--- |
| 8973 |
if (isLoopInvariant(LHS, L) && !isLoopInvariant(RHS, L)) { |
0 |
8973 |
if (isLoopInvariant(LHS, L) && !isLoopInvariant(RHS, L)) { |
0 |
| 8974 |
// If there is a loop-invariant, force it into the RHS. |
--- |
8974 |
// If there is a loop-invariant, force it into the RHS. |
--- |
| 8975 |
std::swap(LHS, RHS); |
0 |
8975 |
std::swap(LHS, RHS); |
0 |
| 8976 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
8976 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
| 8977 |
} |
--- |
8977 |
} |
--- |
| 8978 |
|
--- |
8978 |
|
--- |
| 8979 |
bool ControllingFiniteLoop = ControlsOnlyExit && loopHasNoAbnormalExits(L) && |
0 |
8979 |
bool ControllingFiniteLoop = ControlsOnlyExit && loopHasNoAbnormalExits(L) && |
0 |
| 8980 |
loopIsFiniteByAssumption(L); |
0 |
8980 |
loopIsFiniteByAssumption(L); |
0 |
| 8981 |
// Simplify the operands before analyzing them. |
--- |
8981 |
// Simplify the operands before analyzing them. |
--- |
| 8982 |
(void)SimplifyICmpOperands(Pred, LHS, RHS, /*Depth=*/0); |
0 |
8982 |
(void)SimplifyICmpOperands(Pred, LHS, RHS, /*Depth=*/0); |
0 |
| 8983 |
|
--- |
8983 |
|
--- |
| 8984 |
// If we have a comparison of a chrec against a constant, try to use value |
--- |
8984 |
// If we have a comparison of a chrec against a constant, try to use value |
--- |
| 8985 |
// ranges to answer this query. |
--- |
8985 |
// ranges to answer this query. |
--- |
| 8986 |
if (const SCEVConstant *RHSC = dyn_cast(RHS)) |
0 |
8986 |
if (const SCEVConstant *RHSC = dyn_cast(RHS)) |
0 |
| 8987 |
if (const SCEVAddRecExpr *AddRec = dyn_cast(LHS)) |
0 |
8987 |
if (const SCEVAddRecExpr *AddRec = dyn_cast(LHS)) |
0 |
| 8988 |
if (AddRec->getLoop() == L) { |
0 |
8988 |
if (AddRec->getLoop() == L) { |
0 |
| 8989 |
// Form the constant range. |
--- |
8989 |
// Form the constant range. |
--- |
| 8990 |
ConstantRange CompRange = |
--- |
8990 |
ConstantRange CompRange = |
--- |
| 8991 |
ConstantRange::makeExactICmpRegion(Pred, RHSC->getAPInt()); |
0 |
8991 |
ConstantRange::makeExactICmpRegion(Pred, RHSC->getAPInt()); |
0 |
| 8992 |
|
--- |
8992 |
|
--- |
| 8993 |
const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this); |
0 |
8993 |
const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this); |
0 |
| 8994 |
if (!isa(Ret)) return Ret; |
0 |
8994 |
if (!isa(Ret)) return Ret; |
0 |
| 8995 |
} |
0 |
8995 |
} |
0 |
| 8996 |
|
--- |
8996 |
|
--- |
| 8997 |
// If this loop must exit based on this condition (or execute undefined |
--- |
8997 |
// If this loop must exit based on this condition (or execute undefined |
--- |
| 8998 |
// behaviour), and we can prove the test sequence produced must repeat |
--- |
8998 |
// behaviour), and we can prove the test sequence produced must repeat |
--- |
| 8999 |
// the same values on self-wrap of the IV, then we can infer that IV |
--- |
8999 |
// the same values on self-wrap of the IV, then we can infer that IV |
--- |
| 9000 |
// doesn't self wrap because if it did, we'd have an infinite (undefined) |
--- |
9000 |
// doesn't self wrap because if it did, we'd have an infinite (undefined) |
--- |
| 9001 |
// loop. |
--- |
9001 |
// loop. |
--- |
| 9002 |
if (ControllingFiniteLoop && isLoopInvariant(RHS, L)) { |
0 |
9002 |
if (ControllingFiniteLoop && isLoopInvariant(RHS, L)) { |
0 |
| 9003 |
// TODO: We can peel off any functions which are invertible *in L*. Loop |
--- |
9003 |
// TODO: We can peel off any functions which are invertible *in L*. Loop |
--- |
| 9004 |
// invariant terms are effectively constants for our purposes here. |
--- |
9004 |
// invariant terms are effectively constants for our purposes here. |
--- |
| 9005 |
auto *InnerLHS = LHS; |
0 |
9005 |
auto *InnerLHS = LHS; |
0 |
| 9006 |
if (auto *ZExt = dyn_cast(LHS)) |
0 |
9006 |
if (auto *ZExt = dyn_cast(LHS)) |
0 |
| 9007 |
InnerLHS = ZExt->getOperand(); |
0 |
9007 |
InnerLHS = ZExt->getOperand(); |
0 |
| 9008 |
if (const SCEVAddRecExpr *AR = dyn_cast(InnerLHS)) { |
0 |
9008 |
if (const SCEVAddRecExpr *AR = dyn_cast(InnerLHS)) { |
0 |
| 9009 |
auto *StrideC = dyn_cast(AR->getStepRecurrence(*this)); |
0 |
9009 |
auto *StrideC = dyn_cast(AR->getStepRecurrence(*this)); |
0 |
| 9010 |
if (!AR->hasNoSelfWrap() && AR->getLoop() == L && AR->isAffine() && |
0 |
9010 |
if (!AR->hasNoSelfWrap() && AR->getLoop() == L && AR->isAffine() && |
0 |
| 9011 |
StrideC && StrideC->getAPInt().isPowerOf2()) { |
0 |
9011 |
StrideC && StrideC->getAPInt().isPowerOf2()) { |
0 |
| 9012 |
auto Flags = AR->getNoWrapFlags(); |
0 |
9012 |
auto Flags = AR->getNoWrapFlags(); |
0 |
| 9013 |
Flags = setFlags(Flags, SCEV::FlagNW); |
0 |
9013 |
Flags = setFlags(Flags, SCEV::FlagNW); |
0 |
| 9014 |
SmallVector Operands{AR->operands()}; |
0 |
9014 |
SmallVector Operands{AR->operands()}; |
0 |
| 9015 |
Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags); |
0 |
9015 |
Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags); |
0 |
| 9016 |
setNoWrapFlags(const_cast(AR), Flags); |
0 |
9016 |
setNoWrapFlags(const_cast(AR), Flags); |
0 |
| 9017 |
} |
0 |
9017 |
} |
0 |
| 9018 |
} |
--- |
9018 |
} |
--- |
| 9019 |
} |
--- |
9019 |
} |
--- |
| 9020 |
|
--- |
9020 |
|
--- |
| 9021 |
switch (Pred) { |
0 |
9021 |
switch (Pred) { |
0 |
| 9022 |
case ICmpInst::ICMP_NE: { // while (X != Y) |
0 |
9022 |
case ICmpInst::ICMP_NE: { // while (X != Y) |
0 |
| 9023 |
// Convert to: while (X-Y != 0) |
--- |
9023 |
// Convert to: while (X-Y != 0) |
--- |
| 9024 |
if (LHS->getType()->isPointerTy()) { |
0 |
9024 |
if (LHS->getType()->isPointerTy()) { |
0 |
| 9025 |
LHS = getLosslessPtrToIntExpr(LHS); |
0 |
9025 |
LHS = getLosslessPtrToIntExpr(LHS); |
0 |
| 9026 |
if (isa(LHS)) |
0 |
9026 |
if (isa(LHS)) |
0 |
| 9027 |
return LHS; |
0 |
9027 |
return LHS; |
0 |
| 9028 |
} |
--- |
9028 |
} |
--- |
| 9029 |
if (RHS->getType()->isPointerTy()) { |
0 |
9029 |
if (RHS->getType()->isPointerTy()) { |
0 |
| 9030 |
RHS = getLosslessPtrToIntExpr(RHS); |
0 |
9030 |
RHS = getLosslessPtrToIntExpr(RHS); |
0 |
| 9031 |
if (isa(RHS)) |
0 |
9031 |
if (isa(RHS)) |
0 |
| 9032 |
return RHS; |
0 |
9032 |
return RHS; |
0 |
| 9033 |
} |
--- |
9033 |
} |
--- |
| 9034 |
ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsOnlyExit, |
--- |
9034 |
ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsOnlyExit, |
--- |
| 9035 |
AllowPredicates); |
0 |
9035 |
AllowPredicates); |
0 |
| 9036 |
if (EL.hasAnyInfo()) |
0 |
9036 |
if (EL.hasAnyInfo()) |
0 |
| 9037 |
return EL; |
0 |
9037 |
return EL; |
0 |
| 9038 |
break; |
0 |
9038 |
break; |
0 |
| 9039 |
} |
0 |
9039 |
} |
0 |
| 9040 |
case ICmpInst::ICMP_EQ: { // while (X == Y) |
0 |
9040 |
case ICmpInst::ICMP_EQ: { // while (X == Y) |
0 |
| 9041 |
// Convert to: while (X-Y == 0) |
--- |
9041 |
// Convert to: while (X-Y == 0) |
--- |
| 9042 |
if (LHS->getType()->isPointerTy()) { |
0 |
9042 |
if (LHS->getType()->isPointerTy()) { |
0 |
| 9043 |
LHS = getLosslessPtrToIntExpr(LHS); |
0 |
9043 |
LHS = getLosslessPtrToIntExpr(LHS); |
0 |
| 9044 |
if (isa(LHS)) |
0 |
9044 |
if (isa(LHS)) |
0 |
| 9045 |
return LHS; |
0 |
9045 |
return LHS; |
0 |
| 9046 |
} |
--- |
9046 |
} |
--- |
| 9047 |
if (RHS->getType()->isPointerTy()) { |
0 |
9047 |
if (RHS->getType()->isPointerTy()) { |
0 |
| 9048 |
RHS = getLosslessPtrToIntExpr(RHS); |
0 |
9048 |
RHS = getLosslessPtrToIntExpr(RHS); |
0 |
| 9049 |
if (isa(RHS)) |
0 |
9049 |
if (isa(RHS)) |
0 |
| 9050 |
return RHS; |
0 |
9050 |
return RHS; |
0 |
| 9051 |
} |
--- |
9051 |
} |
--- |
| 9052 |
ExitLimit EL = howFarToNonZero(getMinusSCEV(LHS, RHS), L); |
0 |
9052 |
ExitLimit EL = howFarToNonZero(getMinusSCEV(LHS, RHS), L); |
0 |
| 9053 |
if (EL.hasAnyInfo()) return EL; |
0 |
9053 |
if (EL.hasAnyInfo()) return EL; |
0 |
| 9054 |
break; |
0 |
9054 |
break; |
0 |
| 9055 |
} |
0 |
9055 |
} |
0 |
| 9056 |
case ICmpInst::ICMP_SLE: |
0 |
9056 |
case ICmpInst::ICMP_SLE: |
0 |
| 9057 |
case ICmpInst::ICMP_ULE: |
--- |
9057 |
case ICmpInst::ICMP_ULE: |
--- |
| 9058 |
// Since the loop is finite, an invariant RHS cannot include the boundary |
--- |
9058 |
// Since the loop is finite, an invariant RHS cannot include the boundary |
--- |
| 9059 |
// value, otherwise it would loop forever. |
--- |
9059 |
// value, otherwise it would loop forever. |
--- |
| 9060 |
if (!EnableFiniteLoopControl || !ControllingFiniteLoop || |
0 |
9060 |
if (!EnableFiniteLoopControl || !ControllingFiniteLoop || |
0 |
| 9061 |
!isLoopInvariant(RHS, L)) |
0 |
9061 |
!isLoopInvariant(RHS, L)) |
0 |
| 9062 |
break; |
0 |
9062 |
break; |
0 |
| 9063 |
RHS = getAddExpr(getOne(RHS->getType()), RHS); |
0 |
9063 |
RHS = getAddExpr(getOne(RHS->getType()), RHS); |
0 |
| 9064 |
[[fallthrough]]; |
--- |
9064 |
[[fallthrough]]; |
--- |
| 9065 |
case ICmpInst::ICMP_SLT: |
0 |
9065 |
case ICmpInst::ICMP_SLT: |
0 |
| 9066 |
case ICmpInst::ICMP_ULT: { // while (X < Y) |
--- |
9066 |
case ICmpInst::ICMP_ULT: { // while (X < Y) |
--- |
| 9067 |
bool IsSigned = ICmpInst::isSigned(Pred); |
0 |
9067 |
bool IsSigned = ICmpInst::isSigned(Pred); |
0 |
| 9068 |
ExitLimit EL = howManyLessThans(LHS, RHS, L, IsSigned, ControlsOnlyExit, |
--- |
9068 |
ExitLimit EL = howManyLessThans(LHS, RHS, L, IsSigned, ControlsOnlyExit, |
--- |
| 9069 |
AllowPredicates); |
0 |
9069 |
AllowPredicates); |
0 |
| 9070 |
if (EL.hasAnyInfo()) |
0 |
9070 |
if (EL.hasAnyInfo()) |
0 |
| 9071 |
return EL; |
0 |
9071 |
return EL; |
0 |
| 9072 |
break; |
0 |
9072 |
break; |
0 |
| 9073 |
} |
0 |
9073 |
} |
0 |
| 9074 |
case ICmpInst::ICMP_SGE: |
0 |
9074 |
case ICmpInst::ICMP_SGE: |
0 |
| 9075 |
case ICmpInst::ICMP_UGE: |
--- |
9075 |
case ICmpInst::ICMP_UGE: |
--- |
| 9076 |
// Since the loop is finite, an invariant RHS cannot include the boundary |
--- |
9076 |
// Since the loop is finite, an invariant RHS cannot include the boundary |
--- |
| 9077 |
// value, otherwise it would loop forever. |
--- |
9077 |
// value, otherwise it would loop forever. |
--- |
| 9078 |
if (!EnableFiniteLoopControl || !ControllingFiniteLoop || |
0 |
9078 |
if (!EnableFiniteLoopControl || !ControllingFiniteLoop || |
0 |
| 9079 |
!isLoopInvariant(RHS, L)) |
0 |
9079 |
!isLoopInvariant(RHS, L)) |
0 |
| 9080 |
break; |
0 |
9080 |
break; |
0 |
| 9081 |
RHS = getAddExpr(getMinusOne(RHS->getType()), RHS); |
0 |
9081 |
RHS = getAddExpr(getMinusOne(RHS->getType()), RHS); |
0 |
| 9082 |
[[fallthrough]]; |
--- |
9082 |
[[fallthrough]]; |
--- |
| 9083 |
case ICmpInst::ICMP_SGT: |
0 |
9083 |
case ICmpInst::ICMP_SGT: |
0 |
| 9084 |
case ICmpInst::ICMP_UGT: { // while (X > Y) |
--- |
9084 |
case ICmpInst::ICMP_UGT: { // while (X > Y) |
--- |
| 9085 |
bool IsSigned = ICmpInst::isSigned(Pred); |
0 |
9085 |
bool IsSigned = ICmpInst::isSigned(Pred); |
0 |
| 9086 |
ExitLimit EL = howManyGreaterThans(LHS, RHS, L, IsSigned, ControlsOnlyExit, |
--- |
9086 |
ExitLimit EL = howManyGreaterThans(LHS, RHS, L, IsSigned, ControlsOnlyExit, |
--- |
| 9087 |
AllowPredicates); |
0 |
9087 |
AllowPredicates); |
0 |
| 9088 |
if (EL.hasAnyInfo()) |
0 |
9088 |
if (EL.hasAnyInfo()) |
0 |
| 9089 |
return EL; |
0 |
9089 |
return EL; |
0 |
| 9090 |
break; |
0 |
9090 |
break; |
0 |
| 9091 |
} |
0 |
9091 |
} |
0 |
| 9092 |
default: |
0 |
9092 |
default: |
0 |
| 9093 |
break; |
0 |
9093 |
break; |
0 |
| 9094 |
} |
--- |
9094 |
} |
--- |
| 9095 |
|
--- |
9095 |
|
--- |
| 9096 |
return getCouldNotCompute(); |
0 |
9096 |
return getCouldNotCompute(); |
0 |
| 9097 |
} |
--- |
9097 |
} |
--- |
| 9098 |
|
--- |
9098 |
|
--- |
| 9099 |
ScalarEvolution::ExitLimit |
--- |
9099 |
ScalarEvolution::ExitLimit |
--- |
| 9100 |
ScalarEvolution::computeExitLimitFromSingleExitSwitch(const Loop *L, |
0 |
9100 |
ScalarEvolution::computeExitLimitFromSingleExitSwitch(const Loop *L, |
0 |
| 9101 |
SwitchInst *Switch, |
--- |
9101 |
SwitchInst *Switch, |
--- |
| 9102 |
BasicBlock *ExitingBlock, |
--- |
9102 |
BasicBlock *ExitingBlock, |
--- |
| 9103 |
bool ControlsOnlyExit) { |
--- |
9103 |
bool ControlsOnlyExit) { |
--- |
| 9104 |
assert(!L->contains(ExitingBlock) && "Not an exiting block!"); |
0 |
9104 |
assert(!L->contains(ExitingBlock) && "Not an exiting block!"); |
0 |
| 9105 |
|
--- |
9105 |
|
--- |
| 9106 |
// Give up if the exit is the default dest of a switch. |
--- |
9106 |
// Give up if the exit is the default dest of a switch. |
--- |
| 9107 |
if (Switch->getDefaultDest() == ExitingBlock) |
0 |
9107 |
if (Switch->getDefaultDest() == ExitingBlock) |
0 |
| 9108 |
return getCouldNotCompute(); |
0 |
9108 |
return getCouldNotCompute(); |
0 |
| 9109 |
|
--- |
9109 |
|
--- |
| 9110 |
assert(L->contains(Switch->getDefaultDest()) && |
0 |
9110 |
assert(L->contains(Switch->getDefaultDest()) && |
0 |
| 9111 |
"Default case must not exit the loop!"); |
--- |
9111 |
"Default case must not exit the loop!"); |
--- |
| 9112 |
const SCEV *LHS = getSCEVAtScope(Switch->getCondition(), L); |
0 |
9112 |
const SCEV *LHS = getSCEVAtScope(Switch->getCondition(), L); |
0 |
| 9113 |
const SCEV *RHS = getConstant(Switch->findCaseDest(ExitingBlock)); |
0 |
9113 |
const SCEV *RHS = getConstant(Switch->findCaseDest(ExitingBlock)); |
0 |
| 9114 |
|
--- |
9114 |
|
--- |
| 9115 |
// while (X != Y) --> while (X-Y != 0) |
--- |
9115 |
// while (X != Y) --> while (X-Y != 0) |
--- |
| 9116 |
ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsOnlyExit); |
0 |
9116 |
ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsOnlyExit); |
0 |
| 9117 |
if (EL.hasAnyInfo()) |
0 |
9117 |
if (EL.hasAnyInfo()) |
0 |
| 9118 |
return EL; |
0 |
9118 |
return EL; |
0 |
| 9119 |
|
--- |
9119 |
|
--- |
| 9120 |
return getCouldNotCompute(); |
0 |
9120 |
return getCouldNotCompute(); |
0 |
| 9121 |
} |
0 |
9121 |
} |
0 |
| 9122 |
|
--- |
9122 |
|
--- |
| 9123 |
static ConstantInt * |
--- |
9123 |
static ConstantInt * |
--- |
| 9124 |
EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, |
0 |
9124 |
EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, |
0 |
| 9125 |
ScalarEvolution &SE) { |
--- |
9125 |
ScalarEvolution &SE) { |
--- |
| 9126 |
const SCEV *InVal = SE.getConstant(C); |
0 |
9126 |
const SCEV *InVal = SE.getConstant(C); |
0 |
| 9127 |
const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE); |
0 |
9127 |
const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE); |
0 |
| 9128 |
assert(isa(Val) && |
0 |
9128 |
assert(isa(Val) && |
0 |
| 9129 |
"Evaluation of SCEV at constant didn't fold correctly?"); |
--- |
9129 |
"Evaluation of SCEV at constant didn't fold correctly?"); |
--- |
| 9130 |
return cast(Val)->getValue(); |
0 |
9130 |
return cast(Val)->getValue(); |
0 |
| 9131 |
} |
--- |
9131 |
} |
--- |
| 9132 |
|
--- |
9132 |
|
--- |
| 9133 |
ScalarEvolution::ExitLimit ScalarEvolution::computeShiftCompareExitLimit( |
0 |
9133 |
ScalarEvolution::ExitLimit ScalarEvolution::computeShiftCompareExitLimit( |
0 |
| 9134 |
Value *LHS, Value *RHSV, const Loop *L, ICmpInst::Predicate Pred) { |
--- |
9134 |
Value *LHS, Value *RHSV, const Loop *L, ICmpInst::Predicate Pred) { |
--- |
| 9135 |
ConstantInt *RHS = dyn_cast(RHSV); |
0 |
9135 |
ConstantInt *RHS = dyn_cast(RHSV); |
0 |
| 9136 |
if (!RHS) |
0 |
9136 |
if (!RHS) |
0 |
| 9137 |
return getCouldNotCompute(); |
0 |
9137 |
return getCouldNotCompute(); |
0 |
| 9138 |
|
--- |
9138 |
|
--- |
| 9139 |
const BasicBlock *Latch = L->getLoopLatch(); |
0 |
9139 |
const BasicBlock *Latch = L->getLoopLatch(); |
0 |
| 9140 |
if (!Latch) |
0 |
9140 |
if (!Latch) |
0 |
| 9141 |
return getCouldNotCompute(); |
0 |
9141 |
return getCouldNotCompute(); |
0 |
| 9142 |
|
--- |
9142 |
|
--- |
| 9143 |
const BasicBlock *Predecessor = L->getLoopPredecessor(); |
0 |
9143 |
const BasicBlock *Predecessor = L->getLoopPredecessor(); |
0 |
| 9144 |
if (!Predecessor) |
0 |
9144 |
if (!Predecessor) |
0 |
| 9145 |
return getCouldNotCompute(); |
0 |
9145 |
return getCouldNotCompute(); |
0 |
| 9146 |
|
--- |
9146 |
|
--- |
| 9147 |
// Return true if V is of the form "LHS `shift_op` ". |
--- |
9147 |
// Return true if V is of the form "LHS `shift_op` ". |
--- |
| 9148 |
// Return LHS in OutLHS and shift_opt in OutOpCode. |
--- |
9148 |
// Return LHS in OutLHS and shift_opt in OutOpCode. |
--- |
| 9149 |
auto MatchPositiveShift = |
--- |
9149 |
auto MatchPositiveShift = |
--- |
| 9150 |
[](Value *V, Value *&OutLHS, Instruction::BinaryOps &OutOpCode) { |
0 |
9150 |
[](Value *V, Value *&OutLHS, Instruction::BinaryOps &OutOpCode) { |
0 |
| 9151 |
|
--- |
9151 |
|
--- |
| 9152 |
using namespace PatternMatch; |
--- |
9152 |
using namespace PatternMatch; |
--- |
| 9153 |
|
--- |
9153 |
|
--- |
| 9154 |
ConstantInt *ShiftAmt; |
--- |
9154 |
ConstantInt *ShiftAmt; |
--- |
| 9155 |
if (match(V, m_LShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt)))) |
0 |
9155 |
if (match(V, m_LShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt)))) |
0 |
| 9156 |
OutOpCode = Instruction::LShr; |
0 |
9156 |
OutOpCode = Instruction::LShr; |
0 |
| 9157 |
else if (match(V, m_AShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt)))) |
0 |
9157 |
else if (match(V, m_AShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt)))) |
0 |
| 9158 |
OutOpCode = Instruction::AShr; |
0 |
9158 |
OutOpCode = Instruction::AShr; |
0 |
| 9159 |
else if (match(V, m_Shl(m_Value(OutLHS), m_ConstantInt(ShiftAmt)))) |
0 |
9159 |
else if (match(V, m_Shl(m_Value(OutLHS), m_ConstantInt(ShiftAmt)))) |
0 |
| 9160 |
OutOpCode = Instruction::Shl; |
0 |
9160 |
OutOpCode = Instruction::Shl; |
0 |
| 9161 |
else |
--- |
9161 |
else |
--- |
| 9162 |
return false; |
0 |
9162 |
return false; |
0 |
| 9163 |
|
--- |
9163 |
|
--- |
| 9164 |
return ShiftAmt->getValue().isStrictlyPositive(); |
0 |
9164 |
return ShiftAmt->getValue().isStrictlyPositive(); |
0 |
| 9165 |
}; |
--- |
9165 |
}; |
--- |
| 9166 |
|
--- |
9166 |
|
--- |
| 9167 |
// Recognize a "shift recurrence" either of the form %iv or of %iv.shifted in |
--- |
9167 |
// Recognize a "shift recurrence" either of the form %iv or of %iv.shifted in |
--- |
| 9168 |
// |
--- |
9168 |
// |
--- |
| 9169 |
// loop: |
--- |
9169 |
// loop: |
--- |
| 9170 |
// %iv = phi i32 [ %iv.shifted, %loop ], [ %val, %preheader ] |
--- |
9170 |
// %iv = phi i32 [ %iv.shifted, %loop ], [ %val, %preheader ] |
--- |
| 9171 |
// %iv.shifted = lshr i32 %iv, |
--- |
9171 |
// %iv.shifted = lshr i32 %iv, |
--- |
| 9172 |
// |
--- |
9172 |
// |
--- |
| 9173 |
// Return true on a successful match. Return the corresponding PHI node (%iv |
--- |
9173 |
// Return true on a successful match. Return the corresponding PHI node (%iv |
--- |
| 9174 |
// above) in PNOut and the opcode of the shift operation in OpCodeOut. |
--- |
9174 |
// above) in PNOut and the opcode of the shift operation in OpCodeOut. |
--- |
| 9175 |
auto MatchShiftRecurrence = |
--- |
9175 |
auto MatchShiftRecurrence = |
--- |
| 9176 |
[&](Value *V, PHINode *&PNOut, Instruction::BinaryOps &OpCodeOut) { |
0 |
9176 |
[&](Value *V, PHINode *&PNOut, Instruction::BinaryOps &OpCodeOut) { |
0 |
| 9177 |
std::optional PostShiftOpCode; |
0 |
9177 |
std::optional PostShiftOpCode; |
0 |
| 9178 |
|
--- |
9178 |
|
--- |
| 9179 |
{ |
--- |
9179 |
{ |
--- |
| 9180 |
Instruction::BinaryOps OpC; |
--- |
9180 |
Instruction::BinaryOps OpC; |
--- |
| 9181 |
Value *V; |
--- |
9181 |
Value *V; |
--- |
| 9182 |
|
--- |
9182 |
|
--- |
| 9183 |
// If we encounter a shift instruction, "peel off" the shift operation, |
--- |
9183 |
// If we encounter a shift instruction, "peel off" the shift operation, |
--- |
| 9184 |
// and remember that we did so. Later when we inspect %iv's backedge |
--- |
9184 |
// and remember that we did so. Later when we inspect %iv's backedge |
--- |
| 9185 |
// value, we will make sure that the backedge value uses the same |
--- |
9185 |
// value, we will make sure that the backedge value uses the same |
--- |
| 9186 |
// operation. |
--- |
9186 |
// operation. |
--- |
| 9187 |
// |
--- |
9187 |
// |
--- |
| 9188 |
// Note: the peeled shift operation does not have to be the same |
--- |
9188 |
// Note: the peeled shift operation does not have to be the same |
--- |
| 9189 |
// instruction as the one feeding into the PHI's backedge value. We only |
--- |
9189 |
// instruction as the one feeding into the PHI's backedge value. We only |
--- |
| 9190 |
// really care about it being the same *kind* of shift instruction -- |
--- |
9190 |
// really care about it being the same *kind* of shift instruction -- |
--- |
| 9191 |
// that's all that is required for our later inferences to hold. |
--- |
9191 |
// that's all that is required for our later inferences to hold. |
--- |
| 9192 |
if (MatchPositiveShift(LHS, V, OpC)) { |
0 |
9192 |
if (MatchPositiveShift(LHS, V, OpC)) { |
0 |
| 9193 |
PostShiftOpCode = OpC; |
0 |
9193 |
PostShiftOpCode = OpC; |
0 |
| 9194 |
LHS = V; |
0 |
9194 |
LHS = V; |
0 |
| 9195 |
} |
--- |
9195 |
} |
--- |
| 9196 |
} |
--- |
9196 |
} |
--- |
| 9197 |
|
--- |
9197 |
|
--- |
| 9198 |
PNOut = dyn_cast(LHS); |
0 |
9198 |
PNOut = dyn_cast(LHS); |
0 |
| 9199 |
if (!PNOut || PNOut->getParent() != L->getHeader()) |
0 |
9199 |
if (!PNOut || PNOut->getParent() != L->getHeader()) |
0 |
| 9200 |
return false; |
0 |
9200 |
return false; |
0 |
| 9201 |
|
--- |
9201 |
|
--- |
| 9202 |
Value *BEValue = PNOut->getIncomingValueForBlock(Latch); |
0 |
9202 |
Value *BEValue = PNOut->getIncomingValueForBlock(Latch); |
0 |
| 9203 |
Value *OpLHS; |
--- |
9203 |
Value *OpLHS; |
--- |
| 9204 |
|
--- |
9204 |
|
--- |
| 9205 |
return |
--- |
9205 |
return |
--- |
| 9206 |
// The backedge value for the PHI node must be a shift by a positive |
--- |
9206 |
// The backedge value for the PHI node must be a shift by a positive |
--- |
| 9207 |
// amount |
--- |
9207 |
// amount |
--- |
| 9208 |
MatchPositiveShift(BEValue, OpLHS, OpCodeOut) && |
0 |
9208 |
MatchPositiveShift(BEValue, OpLHS, OpCodeOut) && |
0 |
| 9209 |
|
--- |
9209 |
|
--- |
| 9210 |
// of the PHI node itself |
--- |
9210 |
// of the PHI node itself |
--- |
| 9211 |
OpLHS == PNOut && |
0 |
9211 |
OpLHS == PNOut && |
0 |
| 9212 |
|
--- |
9212 |
|
--- |
| 9213 |
// and the kind of shift should be match the kind of shift we peeled |
--- |
9213 |
// and the kind of shift should be match the kind of shift we peeled |
--- |
| 9214 |
// off, if any. |
--- |
9214 |
// off, if any. |
--- |
| 9215 |
(!PostShiftOpCode || *PostShiftOpCode == OpCodeOut); |
0 |
9215 |
(!PostShiftOpCode || *PostShiftOpCode == OpCodeOut); |
0 |
| 9216 |
}; |
0 |
9216 |
}; |
0 |
| 9217 |
|
--- |
9217 |
|
--- |
| 9218 |
PHINode *PN; |
--- |
9218 |
PHINode *PN; |
--- |
| 9219 |
Instruction::BinaryOps OpCode; |
--- |
9219 |
Instruction::BinaryOps OpCode; |
--- |
| 9220 |
if (!MatchShiftRecurrence(LHS, PN, OpCode)) |
0 |
9220 |
if (!MatchShiftRecurrence(LHS, PN, OpCode)) |
0 |
| 9221 |
return getCouldNotCompute(); |
0 |
9221 |
return getCouldNotCompute(); |
0 |
| 9222 |
|
--- |
9222 |
|
--- |
| 9223 |
const DataLayout &DL = getDataLayout(); |
0 |
9223 |
const DataLayout &DL = getDataLayout(); |
0 |
| 9224 |
|
--- |
9224 |
|
--- |
| 9225 |
// The key rationale for this optimization is that for some kinds of shift |
--- |
9225 |
// The key rationale for this optimization is that for some kinds of shift |
--- |
| 9226 |
// recurrences, the value of the recurrence "stabilizes" to either 0 or -1 |
--- |
9226 |
// recurrences, the value of the recurrence "stabilizes" to either 0 or -1 |
--- |
| 9227 |
// within a finite number of iterations. If the condition guarding the |
--- |
9227 |
// within a finite number of iterations. If the condition guarding the |
--- |
| 9228 |
// backedge (in the sense that the backedge is taken if the condition is true) |
--- |
9228 |
// backedge (in the sense that the backedge is taken if the condition is true) |
--- |
| 9229 |
// is false for the value the shift recurrence stabilizes to, then we know |
--- |
9229 |
// is false for the value the shift recurrence stabilizes to, then we know |
--- |
| 9230 |
// that the backedge is taken only a finite number of times. |
--- |
9230 |
// that the backedge is taken only a finite number of times. |
--- |
| 9231 |
|
--- |
9231 |
|
--- |
| 9232 |
ConstantInt *StableValue = nullptr; |
0 |
9232 |
ConstantInt *StableValue = nullptr; |
0 |
| 9233 |
switch (OpCode) { |
0 |
9233 |
switch (OpCode) { |
0 |
| 9234 |
default: |
0 |
9234 |
default: |
0 |
| 9235 |
llvm_unreachable("Impossible case!"); |
0 |
9235 |
llvm_unreachable("Impossible case!"); |
0 |
| 9236 |
|
--- |
9236 |
|
--- |
| 9237 |
case Instruction::AShr: { |
0 |
9237 |
case Instruction::AShr: { |
0 |
| 9238 |
// {K,ashr,} stabilizes to signum(K) in at most |
--- |
9238 |
// {K,ashr,} stabilizes to signum(K) in at most |
--- |
| 9239 |
// bitwidth(K) iterations. |
--- |
9239 |
// bitwidth(K) iterations. |
--- |
| 9240 |
Value *FirstValue = PN->getIncomingValueForBlock(Predecessor); |
0 |
9240 |
Value *FirstValue = PN->getIncomingValueForBlock(Predecessor); |
0 |
| 9241 |
KnownBits Known = computeKnownBits(FirstValue, DL, 0, &AC, |
0 |
9241 |
KnownBits Known = computeKnownBits(FirstValue, DL, 0, &AC, |
0 |
| 9242 |
Predecessor->getTerminator(), &DT); |
0 |
9242 |
Predecessor->getTerminator(), &DT); |
0 |
| 9243 |
auto *Ty = cast(RHS->getType()); |
0 |
9243 |
auto *Ty = cast(RHS->getType()); |
0 |
| 9244 |
if (Known.isNonNegative()) |
0 |
9244 |
if (Known.isNonNegative()) |
0 |
| 9245 |
StableValue = ConstantInt::get(Ty, 0); |
0 |
9245 |
StableValue = ConstantInt::get(Ty, 0); |
0 |
| 9246 |
else if (Known.isNegative()) |
0 |
9246 |
else if (Known.isNegative()) |
0 |
| 9247 |
StableValue = ConstantInt::get(Ty, -1, true); |
0 |
9247 |
StableValue = ConstantInt::get(Ty, -1, true); |
0 |
| 9248 |
else |
--- |
9248 |
else |
--- |
| 9249 |
return getCouldNotCompute(); |
0 |
9249 |
return getCouldNotCompute(); |
0 |
| 9250 |
|
--- |
9250 |
|
--- |
| 9251 |
break; |
0 |
9251 |
break; |
0 |
| 9252 |
} |
0 |
9252 |
} |
0 |
| 9253 |
case Instruction::LShr: |
0 |
9253 |
case Instruction::LShr: |
0 |
| 9254 |
case Instruction::Shl: |
--- |
9254 |
case Instruction::Shl: |
--- |
| 9255 |
// Both {K,lshr,} and {K,shl,} |
--- |
9255 |
// Both {K,lshr,} and {K,shl,} |
--- |
| 9256 |
// stabilize to 0 in at most bitwidth(K) iterations. |
--- |
9256 |
// stabilize to 0 in at most bitwidth(K) iterations. |
--- |
| 9257 |
StableValue = ConstantInt::get(cast(RHS->getType()), 0); |
0 |
9257 |
StableValue = ConstantInt::get(cast(RHS->getType()), 0); |
0 |
| 9258 |
break; |
0 |
9258 |
break; |
0 |
| 9259 |
} |
--- |
9259 |
} |
--- |
| 9260 |
|
--- |
9260 |
|
--- |
| 9261 |
auto *Result = |
--- |
9261 |
auto *Result = |
--- |
| 9262 |
ConstantFoldCompareInstOperands(Pred, StableValue, RHS, DL, &TLI); |
0 |
9262 |
ConstantFoldCompareInstOperands(Pred, StableValue, RHS, DL, &TLI); |
0 |
| 9263 |
assert(Result->getType()->isIntegerTy(1) && |
0 |
9263 |
assert(Result->getType()->isIntegerTy(1) && |
0 |
| 9264 |
"Otherwise cannot be an operand to a branch instruction"); |
--- |
9264 |
"Otherwise cannot be an operand to a branch instruction"); |
--- |
| 9265 |
|
--- |
9265 |
|
--- |
| 9266 |
if (Result->isZeroValue()) { |
0 |
9266 |
if (Result->isZeroValue()) { |
0 |
| 9267 |
unsigned BitWidth = getTypeSizeInBits(RHS->getType()); |
0 |
9267 |
unsigned BitWidth = getTypeSizeInBits(RHS->getType()); |
0 |
| 9268 |
const SCEV *UpperBound = |
--- |
9268 |
const SCEV *UpperBound = |
--- |
| 9269 |
getConstant(getEffectiveSCEVType(RHS->getType()), BitWidth); |
0 |
9269 |
getConstant(getEffectiveSCEVType(RHS->getType()), BitWidth); |
0 |
| 9270 |
return ExitLimit(getCouldNotCompute(), UpperBound, UpperBound, false); |
0 |
9270 |
return ExitLimit(getCouldNotCompute(), UpperBound, UpperBound, false); |
0 |
| 9271 |
} |
--- |
9271 |
} |
--- |
| 9272 |
|
--- |
9272 |
|
--- |
| 9273 |
return getCouldNotCompute(); |
0 |
9273 |
return getCouldNotCompute(); |
0 |
| 9274 |
} |
--- |
9274 |
} |
--- |
| 9275 |
|
--- |
9275 |
|
--- |
| 9276 |
/// Return true if we can constant fold an instruction of the specified type, |
--- |
9276 |
/// Return true if we can constant fold an instruction of the specified type, |
--- |
| 9277 |
/// assuming that all operands were constants. |
--- |
9277 |
/// assuming that all operands were constants. |
--- |
| 9278 |
static bool CanConstantFold(const Instruction *I) { |
0 |
9278 |
static bool CanConstantFold(const Instruction *I) { |
0 |
| 9279 |
if (isa(I) || isa(I) || |
0 |
9279 |
if (isa(I) || isa(I) || |
0 |
| 9280 |
isa(I) || isa(I) || isa(I) || |
0 |
9280 |
isa(I) || isa(I) || isa(I) || |
0 |
| 9281 |
isa(I) || isa(I)) |
0 |
9281 |
isa(I) || isa(I)) |
0 |
| 9282 |
return true; |
0 |
9282 |
return true; |
0 |
| 9283 |
|
--- |
9283 |
|
--- |
| 9284 |
if (const CallInst *CI = dyn_cast(I)) |
0 |
9284 |
if (const CallInst *CI = dyn_cast(I)) |
0 |
| 9285 |
if (const Function *F = CI->getCalledFunction()) |
0 |
9285 |
if (const Function *F = CI->getCalledFunction()) |
0 |
| 9286 |
return canConstantFoldCallTo(CI, F); |
0 |
9286 |
return canConstantFoldCallTo(CI, F); |
0 |
| 9287 |
return false; |
0 |
9287 |
return false; |
0 |
| 9288 |
} |
--- |
9288 |
} |
--- |
| 9289 |
|
--- |
9289 |
|
--- |
| 9290 |
/// Determine whether this instruction can constant evolve within this loop |
--- |
9290 |
/// Determine whether this instruction can constant evolve within this loop |
--- |
| 9291 |
/// assuming its operands can all constant evolve. |
--- |
9291 |
/// assuming its operands can all constant evolve. |
--- |
| 9292 |
static bool canConstantEvolve(Instruction *I, const Loop *L) { |
0 |
9292 |
static bool canConstantEvolve(Instruction *I, const Loop *L) { |
0 |
| 9293 |
// An instruction outside of the loop can't be derived from a loop PHI. |
--- |
9293 |
// An instruction outside of the loop can't be derived from a loop PHI. |
--- |
| 9294 |
if (!L->contains(I)) return false; |
0 |
9294 |
if (!L->contains(I)) return false; |
0 |
| 9295 |
|
--- |
9295 |
|
--- |
| 9296 |
if (isa(I)) { |
0 |
9296 |
if (isa(I)) { |
0 |
| 9297 |
// We don't currently keep track of the control flow needed to evaluate |
--- |
9297 |
// We don't currently keep track of the control flow needed to evaluate |
--- |
| 9298 |
// PHIs, so we cannot handle PHIs inside of loops. |
--- |
9298 |
// PHIs, so we cannot handle PHIs inside of loops. |
--- |
| 9299 |
return L->getHeader() == I->getParent(); |
0 |
9299 |
return L->getHeader() == I->getParent(); |
0 |
| 9300 |
} |
--- |
9300 |
} |
--- |
| 9301 |
|
--- |
9301 |
|
--- |
| 9302 |
// If we won't be able to constant fold this expression even if the operands |
--- |
9302 |
// If we won't be able to constant fold this expression even if the operands |
--- |
| 9303 |
// are constants, bail early. |
--- |
9303 |
// are constants, bail early. |
--- |
| 9304 |
return CanConstantFold(I); |
0 |
9304 |
return CanConstantFold(I); |
0 |
| 9305 |
} |
--- |
9305 |
} |
--- |
| 9306 |
|
--- |
9306 |
|
--- |
| 9307 |
/// getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by |
--- |
9307 |
/// getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by |
--- |
| 9308 |
/// recursing through each instruction operand until reaching a loop header phi. |
--- |
9308 |
/// recursing through each instruction operand until reaching a loop header phi. |
--- |
| 9309 |
static PHINode * |
--- |
9309 |
static PHINode * |
--- |
| 9310 |
getConstantEvolvingPHIOperands(Instruction *UseInst, const Loop *L, |
0 |
9310 |
getConstantEvolvingPHIOperands(Instruction *UseInst, const Loop *L, |
0 |
| 9311 |
DenseMap &PHIMap, |
--- |
9311 |
DenseMap &PHIMap, |
--- |
| 9312 |
unsigned Depth) { |
--- |
9312 |
unsigned Depth) { |
--- |
| 9313 |
if (Depth > MaxConstantEvolvingDepth) |
0 |
9313 |
if (Depth > MaxConstantEvolvingDepth) |
0 |
| 9314 |
return nullptr; |
0 |
9314 |
return nullptr; |
0 |
| 9315 |
|
--- |
9315 |
|
--- |
| 9316 |
// Otherwise, we can evaluate this instruction if all of its operands are |
--- |
9316 |
// Otherwise, we can evaluate this instruction if all of its operands are |
--- |
| 9317 |
// constant or derived from a PHI node themselves. |
--- |
9317 |
// constant or derived from a PHI node themselves. |
--- |
| 9318 |
PHINode *PHI = nullptr; |
0 |
9318 |
PHINode *PHI = nullptr; |
0 |
| 9319 |
for (Value *Op : UseInst->operands()) { |
0 |
9319 |
for (Value *Op : UseInst->operands()) { |
0 |
| 9320 |
if (isa(Op)) continue; |
0 |
9320 |
if (isa(Op)) continue; |
0 |
| 9321 |
|
--- |
9321 |
|
--- |
| 9322 |
Instruction *OpInst = dyn_cast(Op); |
0 |
9322 |
Instruction *OpInst = dyn_cast(Op); |
0 |
| 9323 |
if (!OpInst || !canConstantEvolve(OpInst, L)) return nullptr; |
0 |
9323 |
if (!OpInst || !canConstantEvolve(OpInst, L)) return nullptr; |
0 |
| 9324 |
|
--- |
9324 |
|
--- |
| 9325 |
PHINode *P = dyn_cast(OpInst); |
0 |
9325 |
PHINode *P = dyn_cast(OpInst); |
0 |
| 9326 |
if (!P) |
0 |
9326 |
if (!P) |
0 |
| 9327 |
// If this operand is already visited, reuse the prior result. |
--- |
9327 |
// If this operand is already visited, reuse the prior result. |
--- |
| 9328 |
// We may have P != PHI if this is the deepest point at which the |
--- |
9328 |
// We may have P != PHI if this is the deepest point at which the |
--- |
| 9329 |
// inconsistent paths meet. |
--- |
9329 |
// inconsistent paths meet. |
--- |
| 9330 |
P = PHIMap.lookup(OpInst); |
0 |
9330 |
P = PHIMap.lookup(OpInst); |
0 |
| 9331 |
if (!P) { |
0 |
9331 |
if (!P) { |
0 |
| 9332 |
// Recurse and memoize the results, whether a phi is found or not. |
--- |
9332 |
// Recurse and memoize the results, whether a phi is found or not. |
--- |
| 9333 |
// This recursive call invalidates pointers into PHIMap. |
--- |
9333 |
// This recursive call invalidates pointers into PHIMap. |
--- |
| 9334 |
P = getConstantEvolvingPHIOperands(OpInst, L, PHIMap, Depth + 1); |
0 |
9334 |
P = getConstantEvolvingPHIOperands(OpInst, L, PHIMap, Depth + 1); |
0 |
| 9335 |
PHIMap[OpInst] = P; |
0 |
9335 |
PHIMap[OpInst] = P; |
0 |
| 9336 |
} |
--- |
9336 |
} |
--- |
| 9337 |
if (!P) |
0 |
9337 |
if (!P) |
0 |
| 9338 |
return nullptr; // Not evolving from PHI |
0 |
9338 |
return nullptr; // Not evolving from PHI |
0 |
| 9339 |
if (PHI && PHI != P) |
0 |
9339 |
if (PHI && PHI != P) |
0 |
| 9340 |
return nullptr; // Evolving from multiple different PHIs. |
0 |
9340 |
return nullptr; // Evolving from multiple different PHIs. |
0 |
| 9341 |
PHI = P; |
0 |
9341 |
PHI = P; |
0 |
| 9342 |
} |
--- |
9342 |
} |
--- |
| 9343 |
// This is a expression evolving from a constant PHI! |
--- |
9343 |
// This is a expression evolving from a constant PHI! |
--- |
| 9344 |
return PHI; |
0 |
9344 |
return PHI; |
0 |
| 9345 |
} |
--- |
9345 |
} |
--- |
| 9346 |
|
--- |
9346 |
|
--- |
| 9347 |
/// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node |
--- |
9347 |
/// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node |
--- |
| 9348 |
/// in the loop that V is derived from. We allow arbitrary operations along the |
--- |
9348 |
/// in the loop that V is derived from. We allow arbitrary operations along the |
--- |
| 9349 |
/// way, but the operands of an operation must either be constants or a value |
--- |
9349 |
/// way, but the operands of an operation must either be constants or a value |
--- |
| 9350 |
/// derived from a constant PHI. If this expression does not fit with these |
--- |
9350 |
/// derived from a constant PHI. If this expression does not fit with these |
--- |
| 9351 |
/// constraints, return null. |
--- |
9351 |
/// constraints, return null. |
--- |
| 9352 |
static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { |
0 |
9352 |
static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { |
0 |
| 9353 |
Instruction *I = dyn_cast(V); |
0 |
9353 |
Instruction *I = dyn_cast(V); |
0 |
| 9354 |
if (!I || !canConstantEvolve(I, L)) return nullptr; |
0 |
9354 |
if (!I || !canConstantEvolve(I, L)) return nullptr; |
0 |
| 9355 |
|
--- |
9355 |
|
--- |
| 9356 |
if (PHINode *PN = dyn_cast(I)) |
0 |
9356 |
if (PHINode *PN = dyn_cast(I)) |
0 |
| 9357 |
return PN; |
0 |
9357 |
return PN; |
0 |
| 9358 |
|
--- |
9358 |
|
--- |
| 9359 |
// Record non-constant instructions contained by the loop. |
--- |
9359 |
// Record non-constant instructions contained by the loop. |
--- |
| 9360 |
DenseMap PHIMap; |
0 |
9360 |
DenseMap PHIMap; |
0 |
| 9361 |
return getConstantEvolvingPHIOperands(I, L, PHIMap, 0); |
0 |
9361 |
return getConstantEvolvingPHIOperands(I, L, PHIMap, 0); |
0 |
| 9362 |
} |
0 |
9362 |
} |
0 |
| 9363 |
|
--- |
9363 |
|
--- |
| 9364 |
/// EvaluateExpression - Given an expression that passes the |
--- |
9364 |
/// EvaluateExpression - Given an expression that passes the |
--- |
| 9365 |
/// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node |
--- |
9365 |
/// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node |
--- |
| 9366 |
/// in the loop has the value PHIVal. If we can't fold this expression for some |
--- |
9366 |
/// in the loop has the value PHIVal. If we can't fold this expression for some |
--- |
| 9367 |
/// reason, return null. |
--- |
9367 |
/// reason, return null. |
--- |
| 9368 |
static Constant *EvaluateExpression(Value *V, const Loop *L, |
0 |
9368 |
static Constant *EvaluateExpression(Value *V, const Loop *L, |
0 |
| 9369 |
DenseMap &Vals, |
--- |
9369 |
DenseMap &Vals, |
--- |
| 9370 |
const DataLayout &DL, |
--- |
9370 |
const DataLayout &DL, |
--- |
| 9371 |
const TargetLibraryInfo *TLI) { |
--- |
9371 |
const TargetLibraryInfo *TLI) { |
--- |
| 9372 |
// Convenient constant check, but redundant for recursive calls. |
--- |
9372 |
// Convenient constant check, but redundant for recursive calls. |
--- |
| 9373 |
if (Constant *C = dyn_cast(V)) return C; |
0 |
9373 |
if (Constant *C = dyn_cast(V)) return C; |
0 |
| 9374 |
Instruction *I = dyn_cast(V); |
0 |
9374 |
Instruction *I = dyn_cast(V); |
0 |
| 9375 |
if (!I) return nullptr; |
0 |
9375 |
if (!I) return nullptr; |
0 |
| 9376 |
|
--- |
9376 |
|
--- |
| 9377 |
if (Constant *C = Vals.lookup(I)) return C; |
0 |
9377 |
if (Constant *C = Vals.lookup(I)) return C; |
0 |
| 9378 |
|
--- |
9378 |
|
--- |
| 9379 |
// An instruction inside the loop depends on a value outside the loop that we |
--- |
9379 |
// An instruction inside the loop depends on a value outside the loop that we |
--- |
| 9380 |
// weren't given a mapping for, or a value such as a call inside the loop. |
--- |
9380 |
// weren't given a mapping for, or a value such as a call inside the loop. |
--- |
| 9381 |
if (!canConstantEvolve(I, L)) return nullptr; |
0 |
9381 |
if (!canConstantEvolve(I, L)) return nullptr; |
0 |
| 9382 |
|
--- |
9382 |
|
--- |
| 9383 |
// An unmapped PHI can be due to a branch or another loop inside this loop, |
--- |
9383 |
// An unmapped PHI can be due to a branch or another loop inside this loop, |
--- |
| 9384 |
// or due to this not being the initial iteration through a loop where we |
--- |
9384 |
// or due to this not being the initial iteration through a loop where we |
--- |
| 9385 |
// couldn't compute the evolution of this particular PHI last time. |
--- |
9385 |
// couldn't compute the evolution of this particular PHI last time. |
--- |
| 9386 |
if (isa(I)) return nullptr; |
0 |
9386 |
if (isa(I)) return nullptr; |
0 |
| 9387 |
|
--- |
9387 |
|
--- |
| 9388 |
std::vector Operands(I->getNumOperands()); |
0 |
9388 |
std::vector Operands(I->getNumOperands()); |
0 |
| 9389 |
|
--- |
9389 |
|
--- |
| 9390 |
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
0 |
9390 |
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
0 |
| 9391 |
Instruction *Operand = dyn_cast(I->getOperand(i)); |
0 |
9391 |
Instruction *Operand = dyn_cast(I->getOperand(i)); |
0 |
| 9392 |
if (!Operand) { |
0 |
9392 |
if (!Operand) { |
0 |
| 9393 |
Operands[i] = dyn_cast(I->getOperand(i)); |
0 |
9393 |
Operands[i] = dyn_cast(I->getOperand(i)); |
0 |
| 9394 |
if (!Operands[i]) return nullptr; |
0 |
9394 |
if (!Operands[i]) return nullptr; |
0 |
| 9395 |
continue; |
0 |
9395 |
continue; |
0 |
| 9396 |
} |
--- |
9396 |
} |
--- |
| 9397 |
Constant *C = EvaluateExpression(Operand, L, Vals, DL, TLI); |
0 |
9397 |
Constant *C = EvaluateExpression(Operand, L, Vals, DL, TLI); |
0 |
| 9398 |
Vals[Operand] = C; |
0 |
9398 |
Vals[Operand] = C; |
0 |
| 9399 |
if (!C) return nullptr; |
0 |
9399 |
if (!C) return nullptr; |
0 |
| 9400 |
Operands[i] = C; |
0 |
9400 |
Operands[i] = C; |
0 |
| 9401 |
} |
--- |
9401 |
} |
--- |
| 9402 |
|
--- |
9402 |
|
--- |
| 9403 |
return ConstantFoldInstOperands(I, Operands, DL, TLI); |
0 |
9403 |
return ConstantFoldInstOperands(I, Operands, DL, TLI); |
0 |
| 9404 |
} |
0 |
9404 |
} |
0 |
| 9405 |
|
--- |
9405 |
|
--- |
| 9406 |
|
--- |
9406 |
|
--- |
| 9407 |
// If every incoming value to PN except the one for BB is a specific Constant, |
--- |
9407 |
// If every incoming value to PN except the one for BB is a specific Constant, |
--- |
| 9408 |
// return that, else return nullptr. |
--- |
9408 |
// return that, else return nullptr. |
--- |
| 9409 |
static Constant *getOtherIncomingValue(PHINode *PN, BasicBlock *BB) { |
0 |
9409 |
static Constant *getOtherIncomingValue(PHINode *PN, BasicBlock *BB) { |
0 |
| 9410 |
Constant *IncomingVal = nullptr; |
0 |
9410 |
Constant *IncomingVal = nullptr; |
0 |
| 9411 |
|
--- |
9411 |
|
--- |
| 9412 |
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
0 |
9412 |
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
0 |
| 9413 |
if (PN->getIncomingBlock(i) == BB) |
0 |
9413 |
if (PN->getIncomingBlock(i) == BB) |
0 |
| 9414 |
continue; |
0 |
9414 |
continue; |
0 |
| 9415 |
|
--- |
9415 |
|
--- |
| 9416 |
auto *CurrentVal = dyn_cast(PN->getIncomingValue(i)); |
0 |
9416 |
auto *CurrentVal = dyn_cast(PN->getIncomingValue(i)); |
0 |
| 9417 |
if (!CurrentVal) |
0 |
9417 |
if (!CurrentVal) |
0 |
| 9418 |
return nullptr; |
0 |
9418 |
return nullptr; |
0 |
| 9419 |
|
--- |
9419 |
|
--- |
| 9420 |
if (IncomingVal != CurrentVal) { |
0 |
9420 |
if (IncomingVal != CurrentVal) { |
0 |
| 9421 |
if (IncomingVal) |
0 |
9421 |
if (IncomingVal) |
0 |
| 9422 |
return nullptr; |
0 |
9422 |
return nullptr; |
0 |
| 9423 |
IncomingVal = CurrentVal; |
0 |
9423 |
IncomingVal = CurrentVal; |
0 |
| 9424 |
} |
--- |
9424 |
} |
--- |
| 9425 |
} |
--- |
9425 |
} |
--- |
| 9426 |
|
--- |
9426 |
|
--- |
| 9427 |
return IncomingVal; |
0 |
9427 |
return IncomingVal; |
0 |
| 9428 |
} |
--- |
9428 |
} |
--- |
| 9429 |
|
--- |
9429 |
|
--- |
| 9430 |
/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is |
--- |
9430 |
/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is |
--- |
| 9431 |
/// in the header of its containing loop, we know the loop executes a |
--- |
9431 |
/// in the header of its containing loop, we know the loop executes a |
--- |
| 9432 |
/// constant number of times, and the PHI node is just a recurrence |
--- |
9432 |
/// constant number of times, and the PHI node is just a recurrence |
--- |
| 9433 |
/// involving constants, fold it. |
--- |
9433 |
/// involving constants, fold it. |
--- |
| 9434 |
Constant * |
--- |
9434 |
Constant * |
--- |
| 9435 |
ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, |
0 |
9435 |
ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, |
0 |
| 9436 |
const APInt &BEs, |
--- |
9436 |
const APInt &BEs, |
--- |
| 9437 |
const Loop *L) { |
--- |
9437 |
const Loop *L) { |
--- |
| 9438 |
auto I = ConstantEvolutionLoopExitValue.find(PN); |
0 |
9438 |
auto I = ConstantEvolutionLoopExitValue.find(PN); |
0 |
| 9439 |
if (I != ConstantEvolutionLoopExitValue.end()) |
0 |
9439 |
if (I != ConstantEvolutionLoopExitValue.end()) |
0 |
| 9440 |
return I->second; |
0 |
9440 |
return I->second; |
0 |
| 9441 |
|
--- |
9441 |
|
--- |
| 9442 |
if (BEs.ugt(MaxBruteForceIterations)) |
0 |
9442 |
if (BEs.ugt(MaxBruteForceIterations)) |
0 |
| 9443 |
return ConstantEvolutionLoopExitValue[PN] = nullptr; // Not going to evaluate it. |
0 |
9443 |
return ConstantEvolutionLoopExitValue[PN] = nullptr; // Not going to evaluate it. |
0 |
| 9444 |
|
--- |
9444 |
|
--- |
| 9445 |
Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; |
0 |
9445 |
Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; |
0 |
| 9446 |
|
--- |
9446 |
|
--- |
| 9447 |
DenseMap CurrentIterVals; |
0 |
9447 |
DenseMap CurrentIterVals; |
0 |
| 9448 |
BasicBlock *Header = L->getHeader(); |
0 |
9448 |
BasicBlock *Header = L->getHeader(); |
0 |
| 9449 |
assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!"); |
0 |
9449 |
assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!"); |
0 |
| 9450 |
|
--- |
9450 |
|
--- |
| 9451 |
BasicBlock *Latch = L->getLoopLatch(); |
0 |
9451 |
BasicBlock *Latch = L->getLoopLatch(); |
0 |
| 9452 |
if (!Latch) |
0 |
9452 |
if (!Latch) |
0 |
| 9453 |
return nullptr; |
0 |
9453 |
return nullptr; |
0 |
| 9454 |
|
--- |
9454 |
|
--- |
| 9455 |
for (PHINode &PHI : Header->phis()) { |
0 |
9455 |
for (PHINode &PHI : Header->phis()) { |
0 |
| 9456 |
if (auto *StartCST = getOtherIncomingValue(&PHI, Latch)) |
0 |
9456 |
if (auto *StartCST = getOtherIncomingValue(&PHI, Latch)) |
0 |
| 9457 |
CurrentIterVals[&PHI] = StartCST; |
0 |
9457 |
CurrentIterVals[&PHI] = StartCST; |
0 |
| 9458 |
} |
--- |
9458 |
} |
--- |
| 9459 |
if (!CurrentIterVals.count(PN)) |
0 |
9459 |
if (!CurrentIterVals.count(PN)) |
0 |
| 9460 |
return RetVal = nullptr; |
0 |
9460 |
return RetVal = nullptr; |
0 |
| 9461 |
|
--- |
9461 |
|
--- |
| 9462 |
Value *BEValue = PN->getIncomingValueForBlock(Latch); |
0 |
9462 |
Value *BEValue = PN->getIncomingValueForBlock(Latch); |
0 |
| 9463 |
|
--- |
9463 |
|
--- |
| 9464 |
// Execute the loop symbolically to determine the exit value. |
--- |
9464 |
// Execute the loop symbolically to determine the exit value. |
--- |
| 9465 |
assert(BEs.getActiveBits() < CHAR_BIT * sizeof(unsigned) && |
0 |
9465 |
assert(BEs.getActiveBits() < CHAR_BIT * sizeof(unsigned) && |
0 |
| 9466 |
"BEs is <= MaxBruteForceIterations which is an 'unsigned'!"); |
--- |
9466 |
"BEs is <= MaxBruteForceIterations which is an 'unsigned'!"); |
--- |
| 9467 |
|
--- |
9467 |
|
--- |
| 9468 |
unsigned NumIterations = BEs.getZExtValue(); // must be in range |
0 |
9468 |
unsigned NumIterations = BEs.getZExtValue(); // must be in range |
0 |
| 9469 |
unsigned IterationNum = 0; |
0 |
9469 |
unsigned IterationNum = 0; |
0 |
| 9470 |
const DataLayout &DL = getDataLayout(); |
0 |
9470 |
const DataLayout &DL = getDataLayout(); |
0 |
| 9471 |
for (; ; ++IterationNum) { |
0 |
9471 |
for (; ; ++IterationNum) { |
0 |
| 9472 |
if (IterationNum == NumIterations) |
0 |
9472 |
if (IterationNum == NumIterations) |
0 |
| 9473 |
return RetVal = CurrentIterVals[PN]; // Got exit value! |
0 |
9473 |
return RetVal = CurrentIterVals[PN]; // Got exit value! |
0 |
| 9474 |
|
--- |
9474 |
|
--- |
| 9475 |
// Compute the value of the PHIs for the next iteration. |
--- |
9475 |
// Compute the value of the PHIs for the next iteration. |
--- |
| 9476 |
// EvaluateExpression adds non-phi values to the CurrentIterVals map. |
--- |
9476 |
// EvaluateExpression adds non-phi values to the CurrentIterVals map. |
--- |
| 9477 |
DenseMap NextIterVals; |
0 |
9477 |
DenseMap NextIterVals; |
0 |
| 9478 |
Constant *NextPHI = |
--- |
9478 |
Constant *NextPHI = |
--- |
| 9479 |
EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI); |
0 |
9479 |
EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI); |
0 |
| 9480 |
if (!NextPHI) |
0 |
9480 |
if (!NextPHI) |
0 |
| 9481 |
return nullptr; // Couldn't evaluate! |
0 |
9481 |
return nullptr; // Couldn't evaluate! |
0 |
| 9482 |
NextIterVals[PN] = NextPHI; |
0 |
9482 |
NextIterVals[PN] = NextPHI; |
0 |
| 9483 |
|
--- |
9483 |
|
--- |
| 9484 |
bool StoppedEvolving = NextPHI == CurrentIterVals[PN]; |
0 |
9484 |
bool StoppedEvolving = NextPHI == CurrentIterVals[PN]; |
0 |
| 9485 |
|
--- |
9485 |
|
--- |
| 9486 |
// Also evaluate the other PHI nodes. However, we don't get to stop if we |
--- |
9486 |
// Also evaluate the other PHI nodes. However, we don't get to stop if we |
--- |
| 9487 |
// cease to be able to evaluate one of them or if they stop evolving, |
--- |
9487 |
// cease to be able to evaluate one of them or if they stop evolving, |
--- |
| 9488 |
// because that doesn't necessarily prevent us from computing PN. |
--- |
9488 |
// because that doesn't necessarily prevent us from computing PN. |
--- |
| 9489 |
SmallVector, 8> PHIsToCompute; |
0 |
9489 |
SmallVector, 8> PHIsToCompute; |
0 |
| 9490 |
for (const auto &I : CurrentIterVals) { |
0 |
9490 |
for (const auto &I : CurrentIterVals) { |
0 |
| 9491 |
PHINode *PHI = dyn_cast(I.first); |
0 |
9491 |
PHINode *PHI = dyn_cast(I.first); |
0 |
| 9492 |
if (!PHI || PHI == PN || PHI->getParent() != Header) continue; |
0 |
9492 |
if (!PHI || PHI == PN || PHI->getParent() != Header) continue; |
0 |
| 9493 |
PHIsToCompute.emplace_back(PHI, I.second); |
0 |
9493 |
PHIsToCompute.emplace_back(PHI, I.second); |
0 |
| 9494 |
} |
--- |
9494 |
} |
--- |
| 9495 |
// We use two distinct loops because EvaluateExpression may invalidate any |
--- |
9495 |
// We use two distinct loops because EvaluateExpression may invalidate any |
--- |
| 9496 |
// iterators into CurrentIterVals. |
--- |
9496 |
// iterators into CurrentIterVals. |
--- |
| 9497 |
for (const auto &I : PHIsToCompute) { |
0 |
9497 |
for (const auto &I : PHIsToCompute) { |
0 |
| 9498 |
PHINode *PHI = I.first; |
0 |
9498 |
PHINode *PHI = I.first; |
0 |
| 9499 |
Constant *&NextPHI = NextIterVals[PHI]; |
0 |
9499 |
Constant *&NextPHI = NextIterVals[PHI]; |
0 |
| 9500 |
if (!NextPHI) { // Not already computed. |
0 |
9500 |
if (!NextPHI) { // Not already computed. |
0 |
| 9501 |
Value *BEValue = PHI->getIncomingValueForBlock(Latch); |
0 |
9501 |
Value *BEValue = PHI->getIncomingValueForBlock(Latch); |
0 |
| 9502 |
NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI); |
0 |
9502 |
NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI); |
0 |
| 9503 |
} |
--- |
9503 |
} |
--- |
| 9504 |
if (NextPHI != I.second) |
0 |
9504 |
if (NextPHI != I.second) |
0 |
| 9505 |
StoppedEvolving = false; |
0 |
9505 |
StoppedEvolving = false; |
0 |
| 9506 |
} |
--- |
9506 |
} |
--- |
| 9507 |
|
--- |
9507 |
|
--- |
| 9508 |
// If all entries in CurrentIterVals == NextIterVals then we can stop |
--- |
9508 |
// If all entries in CurrentIterVals == NextIterVals then we can stop |
--- |
| 9509 |
// iterating, the loop can't continue to change. |
--- |
9509 |
// iterating, the loop can't continue to change. |
--- |
| 9510 |
if (StoppedEvolving) |
0 |
9510 |
if (StoppedEvolving) |
0 |
| 9511 |
return RetVal = CurrentIterVals[PN]; |
0 |
9511 |
return RetVal = CurrentIterVals[PN]; |
0 |
| 9512 |
|
--- |
9512 |
|
--- |
| 9513 |
CurrentIterVals.swap(NextIterVals); |
0 |
9513 |
CurrentIterVals.swap(NextIterVals); |
0 |
| 9514 |
} |
0 |
9514 |
} |
0 |
| 9515 |
} |
0 |
9515 |
} |
0 |
| 9516 |
|
--- |
9516 |
|
--- |
| 9517 |
const SCEV *ScalarEvolution::computeExitCountExhaustively(const Loop *L, |
0 |
9517 |
const SCEV *ScalarEvolution::computeExitCountExhaustively(const Loop *L, |
0 |
| 9518 |
Value *Cond, |
--- |
9518 |
Value *Cond, |
--- |
| 9519 |
bool ExitWhen) { |
--- |
9519 |
bool ExitWhen) { |
--- |
| 9520 |
PHINode *PN = getConstantEvolvingPHI(Cond, L); |
0 |
9520 |
PHINode *PN = getConstantEvolvingPHI(Cond, L); |
0 |
| 9521 |
if (!PN) return getCouldNotCompute(); |
0 |
9521 |
if (!PN) return getCouldNotCompute(); |
0 |
| 9522 |
|
--- |
9522 |
|
--- |
| 9523 |
// If the loop is canonicalized, the PHI will have exactly two entries. |
--- |
9523 |
// If the loop is canonicalized, the PHI will have exactly two entries. |
--- |
| 9524 |
// That's the only form we support here. |
--- |
9524 |
// That's the only form we support here. |
--- |
| 9525 |
if (PN->getNumIncomingValues() != 2) return getCouldNotCompute(); |
0 |
9525 |
if (PN->getNumIncomingValues() != 2) return getCouldNotCompute(); |
0 |
| 9526 |
|
--- |
9526 |
|
--- |
| 9527 |
DenseMap CurrentIterVals; |
0 |
9527 |
DenseMap CurrentIterVals; |
0 |
| 9528 |
BasicBlock *Header = L->getHeader(); |
0 |
9528 |
BasicBlock *Header = L->getHeader(); |
0 |
| 9529 |
assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!"); |
0 |
9529 |
assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!"); |
0 |
| 9530 |
|
--- |
9530 |
|
--- |
| 9531 |
BasicBlock *Latch = L->getLoopLatch(); |
0 |
9531 |
BasicBlock *Latch = L->getLoopLatch(); |
0 |
| 9532 |
assert(Latch && "Should follow from NumIncomingValues == 2!"); |
0 |
9532 |
assert(Latch && "Should follow from NumIncomingValues == 2!"); |
0 |
| 9533 |
|
--- |
9533 |
|
--- |
| 9534 |
for (PHINode &PHI : Header->phis()) { |
0 |
9534 |
for (PHINode &PHI : Header->phis()) { |
0 |
| 9535 |
if (auto *StartCST = getOtherIncomingValue(&PHI, Latch)) |
0 |
9535 |
if (auto *StartCST = getOtherIncomingValue(&PHI, Latch)) |
0 |
| 9536 |
CurrentIterVals[&PHI] = StartCST; |
0 |
9536 |
CurrentIterVals[&PHI] = StartCST; |
0 |
| 9537 |
} |
--- |
9537 |
} |
--- |
| 9538 |
if (!CurrentIterVals.count(PN)) |
0 |
9538 |
if (!CurrentIterVals.count(PN)) |
0 |
| 9539 |
return getCouldNotCompute(); |
0 |
9539 |
return getCouldNotCompute(); |
0 |
| 9540 |
|
--- |
9540 |
|
--- |
| 9541 |
// Okay, we find a PHI node that defines the trip count of this loop. Execute |
--- |
9541 |
// Okay, we find a PHI node that defines the trip count of this loop. Execute |
--- |
| 9542 |
// the loop symbolically to determine when the condition gets a value of |
--- |
9542 |
// the loop symbolically to determine when the condition gets a value of |
--- |
| 9543 |
// "ExitWhen". |
--- |
9543 |
// "ExitWhen". |
--- |
| 9544 |
unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. |
0 |
9544 |
unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. |
0 |
| 9545 |
const DataLayout &DL = getDataLayout(); |
0 |
9545 |
const DataLayout &DL = getDataLayout(); |
0 |
| 9546 |
for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){ |
0 |
9546 |
for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){ |
0 |
| 9547 |
auto *CondVal = dyn_cast_or_null( |
0 |
9547 |
auto *CondVal = dyn_cast_or_null( |
0 |
| 9548 |
EvaluateExpression(Cond, L, CurrentIterVals, DL, &TLI)); |
0 |
9548 |
EvaluateExpression(Cond, L, CurrentIterVals, DL, &TLI)); |
0 |
| 9549 |
|
--- |
9549 |
|
--- |
| 9550 |
// Couldn't symbolically evaluate. |
--- |
9550 |
// Couldn't symbolically evaluate. |
--- |
| 9551 |
if (!CondVal) return getCouldNotCompute(); |
0 |
9551 |
if (!CondVal) return getCouldNotCompute(); |
0 |
| 9552 |
|
--- |
9552 |
|
--- |
| 9553 |
if (CondVal->getValue() == uint64_t(ExitWhen)) { |
0 |
9553 |
if (CondVal->getValue() == uint64_t(ExitWhen)) { |
0 |
| 9554 |
++NumBruteForceTripCountsComputed; |
0 |
9554 |
++NumBruteForceTripCountsComputed; |
0 |
| 9555 |
return getConstant(Type::getInt32Ty(getContext()), IterationNum); |
0 |
9555 |
return getConstant(Type::getInt32Ty(getContext()), IterationNum); |
0 |
| 9556 |
} |
--- |
9556 |
} |
--- |
| 9557 |
|
--- |
9557 |
|
--- |
| 9558 |
// Update all the PHI nodes for the next iteration. |
--- |
9558 |
// Update all the PHI nodes for the next iteration. |
--- |
| 9559 |
DenseMap NextIterVals; |
0 |
9559 |
DenseMap NextIterVals; |
0 |
| 9560 |
|
--- |
9560 |
|
--- |
| 9561 |
// Create a list of which PHIs we need to compute. We want to do this before |
--- |
9561 |
// Create a list of which PHIs we need to compute. We want to do this before |
--- |
| 9562 |
// calling EvaluateExpression on them because that may invalidate iterators |
--- |
9562 |
// calling EvaluateExpression on them because that may invalidate iterators |
--- |
| 9563 |
// into CurrentIterVals. |
--- |
9563 |
// into CurrentIterVals. |
--- |
| 9564 |
SmallVector PHIsToCompute; |
0 |
9564 |
SmallVector PHIsToCompute; |
0 |
| 9565 |
for (const auto &I : CurrentIterVals) { |
0 |
9565 |
for (const auto &I : CurrentIterVals) { |
0 |
| 9566 |
PHINode *PHI = dyn_cast(I.first); |
0 |
9566 |
PHINode *PHI = dyn_cast(I.first); |
0 |
| 9567 |
if (!PHI || PHI->getParent() != Header) continue; |
0 |
9567 |
if (!PHI || PHI->getParent() != Header) continue; |
0 |
| 9568 |
PHIsToCompute.push_back(PHI); |
0 |
9568 |
PHIsToCompute.push_back(PHI); |
0 |
| 9569 |
} |
--- |
9569 |
} |
--- |
| 9570 |
for (PHINode *PHI : PHIsToCompute) { |
0 |
9570 |
for (PHINode *PHI : PHIsToCompute) { |
0 |
| 9571 |
Constant *&NextPHI = NextIterVals[PHI]; |
0 |
9571 |
Constant *&NextPHI = NextIterVals[PHI]; |
0 |
| 9572 |
if (NextPHI) continue; // Already computed! |
0 |
9572 |
if (NextPHI) continue; // Already computed! |
0 |
| 9573 |
|
--- |
9573 |
|
--- |
| 9574 |
Value *BEValue = PHI->getIncomingValueForBlock(Latch); |
0 |
9574 |
Value *BEValue = PHI->getIncomingValueForBlock(Latch); |
0 |
| 9575 |
NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI); |
0 |
9575 |
NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI); |
0 |
| 9576 |
} |
--- |
9576 |
} |
--- |
| 9577 |
CurrentIterVals.swap(NextIterVals); |
0 |
9577 |
CurrentIterVals.swap(NextIterVals); |
0 |
| 9578 |
} |
0 |
9578 |
} |
0 |
| 9579 |
|
--- |
9579 |
|
--- |
| 9580 |
// Too many iterations were needed to evaluate. |
--- |
9580 |
// Too many iterations were needed to evaluate. |
--- |
| 9581 |
return getCouldNotCompute(); |
0 |
9581 |
return getCouldNotCompute(); |
0 |
| 9582 |
} |
0 |
9582 |
} |
0 |
| 9583 |
|
--- |
9583 |
|
--- |
| 9584 |
const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { |
0 |
9584 |
const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { |
0 |
| 9585 |
SmallVector, 2> &Values = |
--- |
9585 |
SmallVector, 2> &Values = |
--- |
| 9586 |
ValuesAtScopes[V]; |
0 |
9586 |
ValuesAtScopes[V]; |
0 |
| 9587 |
// Check to see if we've folded this expression at this loop before. |
--- |
9587 |
// Check to see if we've folded this expression at this loop before. |
--- |
| 9588 |
for (auto &LS : Values) |
0 |
9588 |
for (auto &LS : Values) |
0 |
| 9589 |
if (LS.first == L) |
0 |
9589 |
if (LS.first == L) |
0 |
| 9590 |
return LS.second ? LS.second : V; |
0 |
9590 |
return LS.second ? LS.second : V; |
0 |
| 9591 |
|
--- |
9591 |
|
--- |
| 9592 |
Values.emplace_back(L, nullptr); |
0 |
9592 |
Values.emplace_back(L, nullptr); |
0 |
| 9593 |
|
--- |
9593 |
|
--- |
| 9594 |
// Otherwise compute it. |
--- |
9594 |
// Otherwise compute it. |
--- |
| 9595 |
const SCEV *C = computeSCEVAtScope(V, L); |
0 |
9595 |
const SCEV *C = computeSCEVAtScope(V, L); |
0 |
| 9596 |
for (auto &LS : reverse(ValuesAtScopes[V])) |
0 |
9596 |
for (auto &LS : reverse(ValuesAtScopes[V])) |
0 |
| 9597 |
if (LS.first == L) { |
0 |
9597 |
if (LS.first == L) { |
0 |
| 9598 |
LS.second = C; |
0 |
9598 |
LS.second = C; |
0 |
| 9599 |
if (!isa(C)) |
0 |
9599 |
if (!isa(C)) |
0 |
| 9600 |
ValuesAtScopesUsers[C].push_back({L, V}); |
0 |
9600 |
ValuesAtScopesUsers[C].push_back({L, V}); |
0 |
| 9601 |
break; |
0 |
9601 |
break; |
0 |
| 9602 |
} |
--- |
9602 |
} |
--- |
| 9603 |
return C; |
0 |
9603 |
return C; |
0 |
| 9604 |
} |
--- |
9604 |
} |
--- |
| 9605 |
|
--- |
9605 |
|
--- |
| 9606 |
/// This builds up a Constant using the ConstantExpr interface. That way, we |
--- |
9606 |
/// This builds up a Constant using the ConstantExpr interface. That way, we |
--- |
| 9607 |
/// will return Constants for objects which aren't represented by a |
--- |
9607 |
/// will return Constants for objects which aren't represented by a |
--- |
| 9608 |
/// SCEVConstant, because SCEVConstant is restricted to ConstantInt. |
--- |
9608 |
/// SCEVConstant, because SCEVConstant is restricted to ConstantInt. |
--- |
| 9609 |
/// Returns NULL if the SCEV isn't representable as a Constant. |
--- |
9609 |
/// Returns NULL if the SCEV isn't representable as a Constant. |
--- |
| 9610 |
static Constant *BuildConstantFromSCEV(const SCEV *V) { |
0 |
9610 |
static Constant *BuildConstantFromSCEV(const SCEV *V) { |
0 |
| 9611 |
switch (V->getSCEVType()) { |
0 |
9611 |
switch (V->getSCEVType()) { |
0 |
| 9612 |
case scCouldNotCompute: |
0 |
9612 |
case scCouldNotCompute: |
0 |
| 9613 |
case scAddRecExpr: |
--- |
9613 |
case scAddRecExpr: |
--- |
| 9614 |
case scVScale: |
--- |
9614 |
case scVScale: |
--- |
| 9615 |
return nullptr; |
0 |
9615 |
return nullptr; |
0 |
| 9616 |
case scConstant: |
0 |
9616 |
case scConstant: |
0 |
| 9617 |
return cast(V)->getValue(); |
0 |
9617 |
return cast(V)->getValue(); |
0 |
| 9618 |
case scUnknown: |
0 |
9618 |
case scUnknown: |
0 |
| 9619 |
return dyn_cast(cast(V)->getValue()); |
0 |
9619 |
return dyn_cast(cast(V)->getValue()); |
0 |
| 9620 |
case scSignExtend: { |
0 |
9620 |
case scSignExtend: { |
0 |
| 9621 |
const SCEVSignExtendExpr *SS = cast(V); |
0 |
9621 |
const SCEVSignExtendExpr *SS = cast(V); |
0 |
| 9622 |
if (Constant *CastOp = BuildConstantFromSCEV(SS->getOperand())) |
0 |
9622 |
if (Constant *CastOp = BuildConstantFromSCEV(SS->getOperand())) |
0 |
| 9623 |
return ConstantExpr::getSExt(CastOp, SS->getType()); |
0 |
9623 |
return ConstantExpr::getSExt(CastOp, SS->getType()); |
0 |
| 9624 |
return nullptr; |
0 |
9624 |
return nullptr; |
0 |
| 9625 |
} |
--- |
9625 |
} |
--- |
| 9626 |
case scZeroExtend: { |
0 |
9626 |
case scZeroExtend: { |
0 |
| 9627 |
const SCEVZeroExtendExpr *SZ = cast(V); |
0 |
9627 |
const SCEVZeroExtendExpr *SZ = cast(V); |
0 |
| 9628 |
if (Constant *CastOp = BuildConstantFromSCEV(SZ->getOperand())) |
0 |
9628 |
if (Constant *CastOp = BuildConstantFromSCEV(SZ->getOperand())) |
0 |
| 9629 |
return ConstantExpr::getZExt(CastOp, SZ->getType()); |
0 |
9629 |
return ConstantExpr::getZExt(CastOp, SZ->getType()); |
0 |
| 9630 |
return nullptr; |
0 |
9630 |
return nullptr; |
0 |
| 9631 |
} |
--- |
9631 |
} |
--- |
| 9632 |
case scPtrToInt: { |
0 |
9632 |
case scPtrToInt: { |
0 |
| 9633 |
const SCEVPtrToIntExpr *P2I = cast(V); |
0 |
9633 |
const SCEVPtrToIntExpr *P2I = cast(V); |
0 |
| 9634 |
if (Constant *CastOp = BuildConstantFromSCEV(P2I->getOperand())) |
0 |
9634 |
if (Constant *CastOp = BuildConstantFromSCEV(P2I->getOperand())) |
0 |
| 9635 |
return ConstantExpr::getPtrToInt(CastOp, P2I->getType()); |
0 |
9635 |
return ConstantExpr::getPtrToInt(CastOp, P2I->getType()); |
0 |
| 9636 |
|
--- |
9636 |
|
--- |
| 9637 |
return nullptr; |
0 |
9637 |
return nullptr; |
0 |
| 9638 |
} |
--- |
9638 |
} |
--- |
| 9639 |
case scTruncate: { |
0 |
9639 |
case scTruncate: { |
0 |
| 9640 |
const SCEVTruncateExpr *ST = cast(V); |
0 |
9640 |
const SCEVTruncateExpr *ST = cast(V); |
0 |
| 9641 |
if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand())) |
0 |
9641 |
if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand())) |
0 |
| 9642 |
return ConstantExpr::getTrunc(CastOp, ST->getType()); |
0 |
9642 |
return ConstantExpr::getTrunc(CastOp, ST->getType()); |
0 |
| 9643 |
return nullptr; |
0 |
9643 |
return nullptr; |
0 |
| 9644 |
} |
--- |
9644 |
} |
--- |
| 9645 |
case scAddExpr: { |
0 |
9645 |
case scAddExpr: { |
0 |
| 9646 |
const SCEVAddExpr *SA = cast(V); |
0 |
9646 |
const SCEVAddExpr *SA = cast(V); |
0 |
| 9647 |
Constant *C = nullptr; |
0 |
9647 |
Constant *C = nullptr; |
0 |
| 9648 |
for (const SCEV *Op : SA->operands()) { |
0 |
9648 |
for (const SCEV *Op : SA->operands()) { |
0 |
| 9649 |
Constant *OpC = BuildConstantFromSCEV(Op); |
0 |
9649 |
Constant *OpC = BuildConstantFromSCEV(Op); |
0 |
| 9650 |
if (!OpC) |
0 |
9650 |
if (!OpC) |
0 |
| 9651 |
return nullptr; |
0 |
9651 |
return nullptr; |
0 |
| 9652 |
if (!C) { |
0 |
9652 |
if (!C) { |
0 |
| 9653 |
C = OpC; |
0 |
9653 |
C = OpC; |
0 |
| 9654 |
continue; |
0 |
9654 |
continue; |
0 |
| 9655 |
} |
--- |
9655 |
} |
--- |
| 9656 |
assert(!C->getType()->isPointerTy() && |
0 |
9656 |
assert(!C->getType()->isPointerTy() && |
0 |
| 9657 |
"Can only have one pointer, and it must be last"); |
--- |
9657 |
"Can only have one pointer, and it must be last"); |
--- |
| 9658 |
if (auto *PT = dyn_cast(OpC->getType())) { |
0 |
9658 |
if (auto *PT = dyn_cast(OpC->getType())) { |
0 |
| 9659 |
// The offsets have been converted to bytes. We can add bytes to an |
--- |
9659 |
// The offsets have been converted to bytes. We can add bytes to an |
--- |
| 9660 |
// i8* by GEP with the byte count in the first index. |
--- |
9660 |
// i8* by GEP with the byte count in the first index. |
--- |
| 9661 |
Type *DestPtrTy = |
--- |
9661 |
Type *DestPtrTy = |
--- |
| 9662 |
Type::getInt8PtrTy(PT->getContext(), PT->getAddressSpace()); |
0 |
9662 |
Type::getInt8PtrTy(PT->getContext(), PT->getAddressSpace()); |
0 |
| 9663 |
OpC = ConstantExpr::getBitCast(OpC, DestPtrTy); |
0 |
9663 |
OpC = ConstantExpr::getBitCast(OpC, DestPtrTy); |
0 |
| 9664 |
C = ConstantExpr::getGetElementPtr(Type::getInt8Ty(C->getContext()), |
0 |
9664 |
C = ConstantExpr::getGetElementPtr(Type::getInt8Ty(C->getContext()), |
0 |
| 9665 |
OpC, C); |
--- |
9665 |
OpC, C); |
--- |
| 9666 |
} else { |
--- |
9666 |
} else { |
--- |
| 9667 |
C = ConstantExpr::getAdd(C, OpC); |
0 |
9667 |
C = ConstantExpr::getAdd(C, OpC); |
0 |
| 9668 |
} |
--- |
9668 |
} |
--- |
| 9669 |
} |
--- |
9669 |
} |
--- |
| 9670 |
return C; |
0 |
9670 |
return C; |
0 |
| 9671 |
} |
--- |
9671 |
} |
--- |
| 9672 |
case scMulExpr: { |
0 |
9672 |
case scMulExpr: { |
0 |
| 9673 |
const SCEVMulExpr *SM = cast(V); |
0 |
9673 |
const SCEVMulExpr *SM = cast(V); |
0 |
| 9674 |
Constant *C = nullptr; |
0 |
9674 |
Constant *C = nullptr; |
0 |
| 9675 |
for (const SCEV *Op : SM->operands()) { |
0 |
9675 |
for (const SCEV *Op : SM->operands()) { |
0 |
| 9676 |
assert(!Op->getType()->isPointerTy() && "Can't multiply pointers"); |
0 |
9676 |
assert(!Op->getType()->isPointerTy() && "Can't multiply pointers"); |
0 |
| 9677 |
Constant *OpC = BuildConstantFromSCEV(Op); |
0 |
9677 |
Constant *OpC = BuildConstantFromSCEV(Op); |
0 |
| 9678 |
if (!OpC) |
0 |
9678 |
if (!OpC) |
0 |
| 9679 |
return nullptr; |
0 |
9679 |
return nullptr; |
0 |
| 9680 |
C = C ? ConstantExpr::getMul(C, OpC) : OpC; |
0 |
9680 |
C = C ? ConstantExpr::getMul(C, OpC) : OpC; |
0 |
| 9681 |
} |
--- |
9681 |
} |
--- |
| 9682 |
return C; |
0 |
9682 |
return C; |
0 |
| 9683 |
} |
--- |
9683 |
} |
--- |
| 9684 |
case scUDivExpr: |
0 |
9684 |
case scUDivExpr: |
0 |
| 9685 |
case scSMaxExpr: |
--- |
9685 |
case scSMaxExpr: |
--- |
| 9686 |
case scUMaxExpr: |
--- |
9686 |
case scUMaxExpr: |
--- |
| 9687 |
case scSMinExpr: |
--- |
9687 |
case scSMinExpr: |
--- |
| 9688 |
case scUMinExpr: |
--- |
9688 |
case scUMinExpr: |
--- |
| 9689 |
case scSequentialUMinExpr: |
--- |
9689 |
case scSequentialUMinExpr: |
--- |
| 9690 |
return nullptr; // TODO: smax, umax, smin, umax, umin_seq. |
0 |
9690 |
return nullptr; // TODO: smax, umax, smin, umax, umin_seq. |
0 |
| 9691 |
} |
--- |
9691 |
} |
--- |
| 9692 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
9692 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
| 9693 |
} |
--- |
9693 |
} |
--- |
| 9694 |
|
--- |
9694 |
|
--- |
| 9695 |
const SCEV * |
--- |
9695 |
const SCEV * |
--- |
| 9696 |
ScalarEvolution::getWithOperands(const SCEV *S, |
0 |
9696 |
ScalarEvolution::getWithOperands(const SCEV *S, |
0 |
| 9697 |
SmallVectorImpl &NewOps) { |
--- |
9697 |
SmallVectorImpl &NewOps) { |
--- |
| 9698 |
switch (S->getSCEVType()) { |
0 |
9698 |
switch (S->getSCEVType()) { |
0 |
| 9699 |
case scTruncate: |
0 |
9699 |
case scTruncate: |
0 |
| 9700 |
case scZeroExtend: |
--- |
9700 |
case scZeroExtend: |
--- |
| 9701 |
case scSignExtend: |
--- |
9701 |
case scSignExtend: |
--- |
| 9702 |
case scPtrToInt: |
--- |
9702 |
case scPtrToInt: |
--- |
| 9703 |
return getCastExpr(S->getSCEVType(), NewOps[0], S->getType()); |
0 |
9703 |
return getCastExpr(S->getSCEVType(), NewOps[0], S->getType()); |
0 |
| 9704 |
case scAddRecExpr: { |
0 |
9704 |
case scAddRecExpr: { |
0 |
| 9705 |
auto *AddRec = cast(S); |
0 |
9705 |
auto *AddRec = cast(S); |
0 |
| 9706 |
return getAddRecExpr(NewOps, AddRec->getLoop(), AddRec->getNoWrapFlags()); |
0 |
9706 |
return getAddRecExpr(NewOps, AddRec->getLoop(), AddRec->getNoWrapFlags()); |
0 |
| 9707 |
} |
--- |
9707 |
} |
--- |
| 9708 |
case scAddExpr: |
0 |
9708 |
case scAddExpr: |
0 |
| 9709 |
return getAddExpr(NewOps, cast(S)->getNoWrapFlags()); |
0 |
9709 |
return getAddExpr(NewOps, cast(S)->getNoWrapFlags()); |
0 |
| 9710 |
case scMulExpr: |
0 |
9710 |
case scMulExpr: |
0 |
| 9711 |
return getMulExpr(NewOps, cast(S)->getNoWrapFlags()); |
0 |
9711 |
return getMulExpr(NewOps, cast(S)->getNoWrapFlags()); |
0 |
| 9712 |
case scUDivExpr: |
0 |
9712 |
case scUDivExpr: |
0 |
| 9713 |
return getUDivExpr(NewOps[0], NewOps[1]); |
0 |
9713 |
return getUDivExpr(NewOps[0], NewOps[1]); |
0 |
| 9714 |
case scUMaxExpr: |
0 |
9714 |
case scUMaxExpr: |
0 |
| 9715 |
case scSMaxExpr: |
--- |
9715 |
case scSMaxExpr: |
--- |
| 9716 |
case scUMinExpr: |
--- |
9716 |
case scUMinExpr: |
--- |
| 9717 |
case scSMinExpr: |
--- |
9717 |
case scSMinExpr: |
--- |
| 9718 |
return getMinMaxExpr(S->getSCEVType(), NewOps); |
0 |
9718 |
return getMinMaxExpr(S->getSCEVType(), NewOps); |
0 |
| 9719 |
case scSequentialUMinExpr: |
0 |
9719 |
case scSequentialUMinExpr: |
0 |
| 9720 |
return getSequentialMinMaxExpr(S->getSCEVType(), NewOps); |
0 |
9720 |
return getSequentialMinMaxExpr(S->getSCEVType(), NewOps); |
0 |
| 9721 |
case scConstant: |
0 |
9721 |
case scConstant: |
0 |
| 9722 |
case scVScale: |
--- |
9722 |
case scVScale: |
--- |
| 9723 |
case scUnknown: |
--- |
9723 |
case scUnknown: |
--- |
| 9724 |
return S; |
0 |
9724 |
return S; |
0 |
| 9725 |
case scCouldNotCompute: |
0 |
9725 |
case scCouldNotCompute: |
0 |
| 9726 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
9726 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
| 9727 |
} |
--- |
9727 |
} |
--- |
| 9728 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
9728 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
| 9729 |
} |
--- |
9729 |
} |
--- |
| 9730 |
|
--- |
9730 |
|
--- |
| 9731 |
const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) { |
0 |
9731 |
const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) { |
0 |
| 9732 |
switch (V->getSCEVType()) { |
0 |
9732 |
switch (V->getSCEVType()) { |
0 |
| 9733 |
case scConstant: |
0 |
9733 |
case scConstant: |
0 |
| 9734 |
case scVScale: |
--- |
9734 |
case scVScale: |
--- |
| 9735 |
return V; |
0 |
9735 |
return V; |
0 |
| 9736 |
case scAddRecExpr: { |
0 |
9736 |
case scAddRecExpr: { |
0 |
| 9737 |
// If this is a loop recurrence for a loop that does not contain L, then we |
--- |
9737 |
// If this is a loop recurrence for a loop that does not contain L, then we |
--- |
| 9738 |
// are dealing with the final value computed by the loop. |
--- |
9738 |
// are dealing with the final value computed by the loop. |
--- |
| 9739 |
const SCEVAddRecExpr *AddRec = cast(V); |
0 |
9739 |
const SCEVAddRecExpr *AddRec = cast(V); |
0 |
| 9740 |
// First, attempt to evaluate each operand. |
--- |
9740 |
// First, attempt to evaluate each operand. |
--- |
| 9741 |
// Avoid performing the look-up in the common case where the specified |
--- |
9741 |
// Avoid performing the look-up in the common case where the specified |
--- |
| 9742 |
// expression has no loop-variant portions. |
--- |
9742 |
// expression has no loop-variant portions. |
--- |
| 9743 |
for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
0 |
9743 |
for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
0 |
| 9744 |
const SCEV *OpAtScope = getSCEVAtScope(AddRec->getOperand(i), L); |
0 |
9744 |
const SCEV *OpAtScope = getSCEVAtScope(AddRec->getOperand(i), L); |
0 |
| 9745 |
if (OpAtScope == AddRec->getOperand(i)) |
0 |
9745 |
if (OpAtScope == AddRec->getOperand(i)) |
0 |
| 9746 |
continue; |
0 |
9746 |
continue; |
0 |
| 9747 |
|
--- |
9747 |
|
--- |
| 9748 |
// Okay, at least one of these operands is loop variant but might be |
--- |
9748 |
// Okay, at least one of these operands is loop variant but might be |
--- |
| 9749 |
// foldable. Build a new instance of the folded commutative expression. |
--- |
9749 |
// foldable. Build a new instance of the folded commutative expression. |
--- |
| 9750 |
SmallVector NewOps; |
0 |
9750 |
SmallVector NewOps; |
0 |
| 9751 |
NewOps.reserve(AddRec->getNumOperands()); |
0 |
9751 |
NewOps.reserve(AddRec->getNumOperands()); |
0 |
| 9752 |
append_range(NewOps, AddRec->operands().take_front(i)); |
0 |
9752 |
append_range(NewOps, AddRec->operands().take_front(i)); |
0 |
| 9753 |
NewOps.push_back(OpAtScope); |
0 |
9753 |
NewOps.push_back(OpAtScope); |
0 |
| 9754 |
for (++i; i != e; ++i) |
0 |
9754 |
for (++i; i != e; ++i) |
0 |
| 9755 |
NewOps.push_back(getSCEVAtScope(AddRec->getOperand(i), L)); |
0 |
9755 |
NewOps.push_back(getSCEVAtScope(AddRec->getOperand(i), L)); |
0 |
| 9756 |
|
--- |
9756 |
|
--- |
| 9757 |
const SCEV *FoldedRec = getAddRecExpr( |
0 |
9757 |
const SCEV *FoldedRec = getAddRecExpr( |
0 |
| 9758 |
NewOps, AddRec->getLoop(), AddRec->getNoWrapFlags(SCEV::FlagNW)); |
--- |
9758 |
NewOps, AddRec->getLoop(), AddRec->getNoWrapFlags(SCEV::FlagNW)); |
--- |
| 9759 |
AddRec = dyn_cast(FoldedRec); |
0 |
9759 |
AddRec = dyn_cast(FoldedRec); |
0 |
| 9760 |
// The addrec may be folded to a nonrecurrence, for example, if the |
--- |
9760 |
// The addrec may be folded to a nonrecurrence, for example, if the |
--- |
| 9761 |
// induction variable is multiplied by zero after constant folding. Go |
--- |
9761 |
// induction variable is multiplied by zero after constant folding. Go |
--- |
| 9762 |
// ahead and return the folded value. |
--- |
9762 |
// ahead and return the folded value. |
--- |
| 9763 |
if (!AddRec) |
0 |
9763 |
if (!AddRec) |
0 |
| 9764 |
return FoldedRec; |
0 |
9764 |
return FoldedRec; |
0 |
| 9765 |
break; |
0 |
9765 |
break; |
0 |
| 9766 |
} |
0 |
9766 |
} |
0 |
| 9767 |
|
--- |
9767 |
|
--- |
| 9768 |
// If the scope is outside the addrec's loop, evaluate it by using the |
--- |
9768 |
// If the scope is outside the addrec's loop, evaluate it by using the |
--- |
| 9769 |
// loop exit value of the addrec. |
--- |
9769 |
// loop exit value of the addrec. |
--- |
| 9770 |
if (!AddRec->getLoop()->contains(L)) { |
0 |
9770 |
if (!AddRec->getLoop()->contains(L)) { |
0 |
| 9771 |
// To evaluate this recurrence, we need to know how many times the AddRec |
--- |
9771 |
// To evaluate this recurrence, we need to know how many times the AddRec |
--- |
| 9772 |
// loop iterates. Compute this now. |
--- |
9772 |
// loop iterates. Compute this now. |
--- |
| 9773 |
const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); |
0 |
9773 |
const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); |
0 |
| 9774 |
if (BackedgeTakenCount == getCouldNotCompute()) |
0 |
9774 |
if (BackedgeTakenCount == getCouldNotCompute()) |
0 |
| 9775 |
return AddRec; |
0 |
9775 |
return AddRec; |
0 |
| 9776 |
|
--- |
9776 |
|
--- |
| 9777 |
// Then, evaluate the AddRec. |
--- |
9777 |
// Then, evaluate the AddRec. |
--- |
| 9778 |
return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); |
0 |
9778 |
return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); |
0 |
| 9779 |
} |
--- |
9779 |
} |
--- |
| 9780 |
|
--- |
9780 |
|
--- |
| 9781 |
return AddRec; |
0 |
9781 |
return AddRec; |
0 |
| 9782 |
} |
--- |
9782 |
} |
--- |
| 9783 |
case scTruncate: |
0 |
9783 |
case scTruncate: |
0 |
| 9784 |
case scZeroExtend: |
--- |
9784 |
case scZeroExtend: |
--- |
| 9785 |
case scSignExtend: |
--- |
9785 |
case scSignExtend: |
--- |
| 9786 |
case scPtrToInt: |
--- |
9786 |
case scPtrToInt: |
--- |
| 9787 |
case scAddExpr: |
--- |
9787 |
case scAddExpr: |
--- |
| 9788 |
case scMulExpr: |
--- |
9788 |
case scMulExpr: |
--- |
| 9789 |
case scUDivExpr: |
--- |
9789 |
case scUDivExpr: |
--- |
| 9790 |
case scUMaxExpr: |
--- |
9790 |
case scUMaxExpr: |
--- |
| 9791 |
case scSMaxExpr: |
--- |
9791 |
case scSMaxExpr: |
--- |
| 9792 |
case scUMinExpr: |
--- |
9792 |
case scUMinExpr: |
--- |
| 9793 |
case scSMinExpr: |
--- |
9793 |
case scSMinExpr: |
--- |
| 9794 |
case scSequentialUMinExpr: { |
--- |
9794 |
case scSequentialUMinExpr: { |
--- |
| 9795 |
ArrayRef Ops = V->operands(); |
0 |
9795 |
ArrayRef Ops = V->operands(); |
0 |
| 9796 |
// Avoid performing the look-up in the common case where the specified |
--- |
9796 |
// Avoid performing the look-up in the common case where the specified |
--- |
| 9797 |
// expression has no loop-variant portions. |
--- |
9797 |
// expression has no loop-variant portions. |
--- |
| 9798 |
for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
0 |
9798 |
for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
0 |
| 9799 |
const SCEV *OpAtScope = getSCEVAtScope(Ops[i], L); |
0 |
9799 |
const SCEV *OpAtScope = getSCEVAtScope(Ops[i], L); |
0 |
| 9800 |
if (OpAtScope != Ops[i]) { |
0 |
9800 |
if (OpAtScope != Ops[i]) { |
0 |
| 9801 |
// Okay, at least one of these operands is loop variant but might be |
--- |
9801 |
// Okay, at least one of these operands is loop variant but might be |
--- |
| 9802 |
// foldable. Build a new instance of the folded commutative expression. |
--- |
9802 |
// foldable. Build a new instance of the folded commutative expression. |
--- |
| 9803 |
SmallVector NewOps; |
0 |
9803 |
SmallVector NewOps; |
0 |
| 9804 |
NewOps.reserve(Ops.size()); |
0 |
9804 |
NewOps.reserve(Ops.size()); |
0 |
| 9805 |
append_range(NewOps, Ops.take_front(i)); |
0 |
9805 |
append_range(NewOps, Ops.take_front(i)); |
0 |
| 9806 |
NewOps.push_back(OpAtScope); |
0 |
9806 |
NewOps.push_back(OpAtScope); |
0 |
| 9807 |
|
--- |
9807 |
|
--- |
| 9808 |
for (++i; i != e; ++i) { |
0 |
9808 |
for (++i; i != e; ++i) { |
0 |
| 9809 |
OpAtScope = getSCEVAtScope(Ops[i], L); |
0 |
9809 |
OpAtScope = getSCEVAtScope(Ops[i], L); |
0 |
| 9810 |
NewOps.push_back(OpAtScope); |
0 |
9810 |
NewOps.push_back(OpAtScope); |
0 |
| 9811 |
} |
--- |
9811 |
} |
--- |
| 9812 |
|
--- |
9812 |
|
--- |
| 9813 |
return getWithOperands(V, NewOps); |
0 |
9813 |
return getWithOperands(V, NewOps); |
0 |
| 9814 |
} |
0 |
9814 |
} |
0 |
| 9815 |
} |
--- |
9815 |
} |
--- |
| 9816 |
// If we got here, all operands are loop invariant. |
--- |
9816 |
// If we got here, all operands are loop invariant. |
--- |
| 9817 |
return V; |
0 |
9817 |
return V; |
0 |
| 9818 |
} |
--- |
9818 |
} |
--- |
| 9819 |
case scUnknown: { |
0 |
9819 |
case scUnknown: { |
0 |
| 9820 |
// If this instruction is evolved from a constant-evolving PHI, compute the |
--- |
9820 |
// If this instruction is evolved from a constant-evolving PHI, compute the |
--- |
| 9821 |
// exit value from the loop without using SCEVs. |
--- |
9821 |
// exit value from the loop without using SCEVs. |
--- |
| 9822 |
const SCEVUnknown *SU = cast(V); |
0 |
9822 |
const SCEVUnknown *SU = cast(V); |
0 |
| 9823 |
Instruction *I = dyn_cast(SU->getValue()); |
0 |
9823 |
Instruction *I = dyn_cast(SU->getValue()); |
0 |
| 9824 |
if (!I) |
0 |
9824 |
if (!I) |
0 |
| 9825 |
return V; // This is some other type of SCEVUnknown, just return it. |
0 |
9825 |
return V; // This is some other type of SCEVUnknown, just return it. |
0 |
| 9826 |
|
--- |
9826 |
|
--- |
| 9827 |
if (PHINode *PN = dyn_cast(I)) { |
0 |
9827 |
if (PHINode *PN = dyn_cast(I)) { |
0 |
| 9828 |
const Loop *CurrLoop = this->LI[I->getParent()]; |
0 |
9828 |
const Loop *CurrLoop = this->LI[I->getParent()]; |
0 |
| 9829 |
// Looking for loop exit value. |
--- |
9829 |
// Looking for loop exit value. |
--- |
| 9830 |
if (CurrLoop && CurrLoop->getParentLoop() == L && |
0 |
9830 |
if (CurrLoop && CurrLoop->getParentLoop() == L && |
0 |
| 9831 |
PN->getParent() == CurrLoop->getHeader()) { |
0 |
9831 |
PN->getParent() == CurrLoop->getHeader()) { |
0 |
| 9832 |
// Okay, there is no closed form solution for the PHI node. Check |
--- |
9832 |
// Okay, there is no closed form solution for the PHI node. Check |
--- |
| 9833 |
// to see if the loop that contains it has a known backedge-taken |
--- |
9833 |
// to see if the loop that contains it has a known backedge-taken |
--- |
| 9834 |
// count. If so, we may be able to force computation of the exit |
--- |
9834 |
// count. If so, we may be able to force computation of the exit |
--- |
| 9835 |
// value. |
--- |
9835 |
// value. |
--- |
| 9836 |
const SCEV *BackedgeTakenCount = getBackedgeTakenCount(CurrLoop); |
0 |
9836 |
const SCEV *BackedgeTakenCount = getBackedgeTakenCount(CurrLoop); |
0 |
| 9837 |
// This trivial case can show up in some degenerate cases where |
--- |
9837 |
// This trivial case can show up in some degenerate cases where |
--- |
| 9838 |
// the incoming IR has not yet been fully simplified. |
--- |
9838 |
// the incoming IR has not yet been fully simplified. |
--- |
| 9839 |
if (BackedgeTakenCount->isZero()) { |
0 |
9839 |
if (BackedgeTakenCount->isZero()) { |
0 |
| 9840 |
Value *InitValue = nullptr; |
0 |
9840 |
Value *InitValue = nullptr; |
0 |
| 9841 |
bool MultipleInitValues = false; |
0 |
9841 |
bool MultipleInitValues = false; |
0 |
| 9842 |
for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) { |
0 |
9842 |
for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) { |
0 |
| 9843 |
if (!CurrLoop->contains(PN->getIncomingBlock(i))) { |
0 |
9843 |
if (!CurrLoop->contains(PN->getIncomingBlock(i))) { |
0 |
| 9844 |
if (!InitValue) |
0 |
9844 |
if (!InitValue) |
0 |
| 9845 |
InitValue = PN->getIncomingValue(i); |
0 |
9845 |
InitValue = PN->getIncomingValue(i); |
0 |
| 9846 |
else if (InitValue != PN->getIncomingValue(i)) { |
0 |
9846 |
else if (InitValue != PN->getIncomingValue(i)) { |
0 |
| 9847 |
MultipleInitValues = true; |
0 |
9847 |
MultipleInitValues = true; |
0 |
| 9848 |
break; |
0 |
9848 |
break; |
0 |
| 9849 |
} |
--- |
9849 |
} |
--- |
| 9850 |
} |
--- |
9850 |
} |
--- |
| 9851 |
} |
--- |
9851 |
} |
--- |
| 9852 |
if (!MultipleInitValues && InitValue) |
0 |
9852 |
if (!MultipleInitValues && InitValue) |
0 |
| 9853 |
return getSCEV(InitValue); |
0 |
9853 |
return getSCEV(InitValue); |
0 |
| 9854 |
} |
--- |
9854 |
} |
--- |
| 9855 |
// Do we have a loop invariant value flowing around the backedge |
--- |
9855 |
// Do we have a loop invariant value flowing around the backedge |
--- |
| 9856 |
// for a loop which must execute the backedge? |
--- |
9856 |
// for a loop which must execute the backedge? |
--- |
| 9857 |
if (!isa(BackedgeTakenCount) && |
0 |
9857 |
if (!isa(BackedgeTakenCount) && |
0 |
| 9858 |
isKnownPositive(BackedgeTakenCount) && |
0 |
9858 |
isKnownPositive(BackedgeTakenCount) && |
0 |
| 9859 |
PN->getNumIncomingValues() == 2) { |
0 |
9859 |
PN->getNumIncomingValues() == 2) { |
0 |
| 9860 |
|
--- |
9860 |
|
--- |
| 9861 |
unsigned InLoopPred = |
--- |
9861 |
unsigned InLoopPred = |
--- |
| 9862 |
CurrLoop->contains(PN->getIncomingBlock(0)) ? 0 : 1; |
0 |
9862 |
CurrLoop->contains(PN->getIncomingBlock(0)) ? 0 : 1; |
0 |
| 9863 |
Value *BackedgeVal = PN->getIncomingValue(InLoopPred); |
0 |
9863 |
Value *BackedgeVal = PN->getIncomingValue(InLoopPred); |
0 |
| 9864 |
if (CurrLoop->isLoopInvariant(BackedgeVal)) |
0 |
9864 |
if (CurrLoop->isLoopInvariant(BackedgeVal)) |
0 |
| 9865 |
return getSCEV(BackedgeVal); |
0 |
9865 |
return getSCEV(BackedgeVal); |
0 |
| 9866 |
} |
--- |
9866 |
} |
--- |
| 9867 |
if (auto *BTCC = dyn_cast(BackedgeTakenCount)) { |
0 |
9867 |
if (auto *BTCC = dyn_cast(BackedgeTakenCount)) { |
0 |
| 9868 |
// Okay, we know how many times the containing loop executes. If |
--- |
9868 |
// Okay, we know how many times the containing loop executes. If |
--- |
| 9869 |
// this is a constant evolving PHI node, get the final value at |
--- |
9869 |
// this is a constant evolving PHI node, get the final value at |
--- |
| 9870 |
// the specified iteration number. |
--- |
9870 |
// the specified iteration number. |
--- |
| 9871 |
Constant *RV = |
--- |
9871 |
Constant *RV = |
--- |
| 9872 |
getConstantEvolutionLoopExitValue(PN, BTCC->getAPInt(), CurrLoop); |
0 |
9872 |
getConstantEvolutionLoopExitValue(PN, BTCC->getAPInt(), CurrLoop); |
0 |
| 9873 |
if (RV) |
0 |
9873 |
if (RV) |
0 |
| 9874 |
return getSCEV(RV); |
0 |
9874 |
return getSCEV(RV); |
0 |
| 9875 |
} |
--- |
9875 |
} |
--- |
| 9876 |
} |
--- |
9876 |
} |
--- |
| 9877 |
} |
--- |
9877 |
} |
--- |
| 9878 |
|
--- |
9878 |
|
--- |
| 9879 |
// Okay, this is an expression that we cannot symbolically evaluate |
--- |
9879 |
// Okay, this is an expression that we cannot symbolically evaluate |
--- |
| 9880 |
// into a SCEV. Check to see if it's possible to symbolically evaluate |
--- |
9880 |
// into a SCEV. Check to see if it's possible to symbolically evaluate |
--- |
| 9881 |
// the arguments into constants, and if so, try to constant propagate the |
--- |
9881 |
// the arguments into constants, and if so, try to constant propagate the |
--- |
| 9882 |
// result. This is particularly useful for computing loop exit values. |
--- |
9882 |
// result. This is particularly useful for computing loop exit values. |
--- |
| 9883 |
if (!CanConstantFold(I)) |
0 |
9883 |
if (!CanConstantFold(I)) |
0 |
| 9884 |
return V; // This is some other type of SCEVUnknown, just return it. |
0 |
9884 |
return V; // This is some other type of SCEVUnknown, just return it. |
0 |
| 9885 |
|
--- |
9885 |
|
--- |
| 9886 |
SmallVector Operands; |
0 |
9886 |
SmallVector Operands; |
0 |
| 9887 |
Operands.reserve(I->getNumOperands()); |
0 |
9887 |
Operands.reserve(I->getNumOperands()); |
0 |
| 9888 |
bool MadeImprovement = false; |
0 |
9888 |
bool MadeImprovement = false; |
0 |
| 9889 |
for (Value *Op : I->operands()) { |
0 |
9889 |
for (Value *Op : I->operands()) { |
0 |
| 9890 |
if (Constant *C = dyn_cast(Op)) { |
0 |
9890 |
if (Constant *C = dyn_cast(Op)) { |
0 |
| 9891 |
Operands.push_back(C); |
0 |
9891 |
Operands.push_back(C); |
0 |
| 9892 |
continue; |
0 |
9892 |
continue; |
0 |
| 9893 |
} |
--- |
9893 |
} |
--- |
| 9894 |
|
--- |
9894 |
|
--- |
| 9895 |
// If any of the operands is non-constant and if they are |
--- |
9895 |
// If any of the operands is non-constant and if they are |
--- |
| 9896 |
// non-integer and non-pointer, don't even try to analyze them |
--- |
9896 |
// non-integer and non-pointer, don't even try to analyze them |
--- |
| 9897 |
// with scev techniques. |
--- |
9897 |
// with scev techniques. |
--- |
| 9898 |
if (!isSCEVable(Op->getType())) |
0 |
9898 |
if (!isSCEVable(Op->getType())) |
0 |
| 9899 |
return V; |
0 |
9899 |
return V; |
0 |
| 9900 |
|
--- |
9900 |
|
--- |
| 9901 |
const SCEV *OrigV = getSCEV(Op); |
0 |
9901 |
const SCEV *OrigV = getSCEV(Op); |
0 |
| 9902 |
const SCEV *OpV = getSCEVAtScope(OrigV, L); |
0 |
9902 |
const SCEV *OpV = getSCEVAtScope(OrigV, L); |
0 |
| 9903 |
MadeImprovement |= OrigV != OpV; |
0 |
9903 |
MadeImprovement |= OrigV != OpV; |
0 |
| 9904 |
|
--- |
9904 |
|
--- |
| 9905 |
Constant *C = BuildConstantFromSCEV(OpV); |
0 |
9905 |
Constant *C = BuildConstantFromSCEV(OpV); |
0 |
| 9906 |
if (!C) |
0 |
9906 |
if (!C) |
0 |
| 9907 |
return V; |
0 |
9907 |
return V; |
0 |
| 9908 |
if (C->getType() != Op->getType()) |
0 |
9908 |
if (C->getType() != Op->getType()) |
0 |
| 9909 |
C = ConstantExpr::getCast( |
0 |
9909 |
C = ConstantExpr::getCast( |
0 |
| 9910 |
CastInst::getCastOpcode(C, false, Op->getType(), false), C, |
0 |
9910 |
CastInst::getCastOpcode(C, false, Op->getType(), false), C, |
0 |
| 9911 |
Op->getType()); |
--- |
9911 |
Op->getType()); |
--- |
| 9912 |
Operands.push_back(C); |
0 |
9912 |
Operands.push_back(C); |
0 |
| 9913 |
} |
--- |
9913 |
} |
--- |
| 9914 |
|
--- |
9914 |
|
--- |
| 9915 |
// Check to see if getSCEVAtScope actually made an improvement. |
--- |
9915 |
// Check to see if getSCEVAtScope actually made an improvement. |
--- |
| 9916 |
if (!MadeImprovement) |
0 |
9916 |
if (!MadeImprovement) |
0 |
| 9917 |
return V; // This is some other type of SCEVUnknown, just return it. |
0 |
9917 |
return V; // This is some other type of SCEVUnknown, just return it. |
0 |
| 9918 |
|
--- |
9918 |
|
--- |
| 9919 |
Constant *C = nullptr; |
0 |
9919 |
Constant *C = nullptr; |
0 |
| 9920 |
const DataLayout &DL = getDataLayout(); |
0 |
9920 |
const DataLayout &DL = getDataLayout(); |
0 |
| 9921 |
C = ConstantFoldInstOperands(I, Operands, DL, &TLI); |
0 |
9921 |
C = ConstantFoldInstOperands(I, Operands, DL, &TLI); |
0 |
| 9922 |
if (!C) |
0 |
9922 |
if (!C) |
0 |
| 9923 |
return V; |
0 |
9923 |
return V; |
0 |
| 9924 |
return getSCEV(C); |
0 |
9924 |
return getSCEV(C); |
0 |
| 9925 |
} |
0 |
9925 |
} |
0 |
| 9926 |
case scCouldNotCompute: |
0 |
9926 |
case scCouldNotCompute: |
0 |
| 9927 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
9927 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
| 9928 |
} |
--- |
9928 |
} |
--- |
| 9929 |
llvm_unreachable("Unknown SCEV type!"); |
0 |
9929 |
llvm_unreachable("Unknown SCEV type!"); |
0 |
| 9930 |
} |
--- |
9930 |
} |
--- |
| 9931 |
|
--- |
9931 |
|
--- |
| 9932 |
const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { |
0 |
9932 |
const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { |
0 |
| 9933 |
return getSCEVAtScope(getSCEV(V), L); |
0 |
9933 |
return getSCEVAtScope(getSCEV(V), L); |
0 |
| 9934 |
} |
--- |
9934 |
} |
--- |
| 9935 |
|
--- |
9935 |
|
--- |
| 9936 |
const SCEV *ScalarEvolution::stripInjectiveFunctions(const SCEV *S) const { |
0 |
9936 |
const SCEV *ScalarEvolution::stripInjectiveFunctions(const SCEV *S) const { |
0 |
| 9937 |
if (const SCEVZeroExtendExpr *ZExt = dyn_cast(S)) |
0 |
9937 |
if (const SCEVZeroExtendExpr *ZExt = dyn_cast(S)) |
0 |
| 9938 |
return stripInjectiveFunctions(ZExt->getOperand()); |
0 |
9938 |
return stripInjectiveFunctions(ZExt->getOperand()); |
0 |
| 9939 |
if (const SCEVSignExtendExpr *SExt = dyn_cast(S)) |
0 |
9939 |
if (const SCEVSignExtendExpr *SExt = dyn_cast(S)) |
0 |
| 9940 |
return stripInjectiveFunctions(SExt->getOperand()); |
0 |
9940 |
return stripInjectiveFunctions(SExt->getOperand()); |
0 |
| 9941 |
return S; |
0 |
9941 |
return S; |
0 |
| 9942 |
} |
--- |
9942 |
} |
--- |
| 9943 |
|
--- |
9943 |
|
--- |
| 9944 |
/// Finds the minimum unsigned root of the following equation: |
--- |
9944 |
/// Finds the minimum unsigned root of the following equation: |
--- |
| 9945 |
/// |
--- |
9945 |
/// |
--- |
| 9946 |
/// A * X = B (mod N) |
--- |
9946 |
/// A * X = B (mod N) |
--- |
| 9947 |
/// |
--- |
9947 |
/// |
--- |
| 9948 |
/// where N = 2^BW and BW is the common bit width of A and B. The signedness of |
--- |
9948 |
/// where N = 2^BW and BW is the common bit width of A and B. The signedness of |
--- |
| 9949 |
/// A and B isn't important. |
--- |
9949 |
/// A and B isn't important. |
--- |
| 9950 |
/// |
--- |
9950 |
/// |
--- |
| 9951 |
/// If the equation does not have a solution, SCEVCouldNotCompute is returned. |
--- |
9951 |
/// If the equation does not have a solution, SCEVCouldNotCompute is returned. |
--- |
| 9952 |
static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const SCEV *B, |
0 |
9952 |
static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const SCEV *B, |
0 |
| 9953 |
ScalarEvolution &SE) { |
--- |
9953 |
ScalarEvolution &SE) { |
--- |
| 9954 |
uint32_t BW = A.getBitWidth(); |
0 |
9954 |
uint32_t BW = A.getBitWidth(); |
0 |
| 9955 |
assert(BW == SE.getTypeSizeInBits(B->getType())); |
0 |
9955 |
assert(BW == SE.getTypeSizeInBits(B->getType())); |
0 |
| 9956 |
assert(A != 0 && "A must be non-zero."); |
0 |
9956 |
assert(A != 0 && "A must be non-zero."); |
0 |
| 9957 |
|
--- |
9957 |
|
--- |
| 9958 |
// 1. D = gcd(A, N) |
--- |
9958 |
// 1. D = gcd(A, N) |
--- |
| 9959 |
// |
--- |
9959 |
// |
--- |
| 9960 |
// The gcd of A and N may have only one prime factor: 2. The number of |
--- |
9960 |
// The gcd of A and N may have only one prime factor: 2. The number of |
--- |
| 9961 |
// trailing zeros in A is its multiplicity |
--- |
9961 |
// trailing zeros in A is its multiplicity |
--- |
| 9962 |
uint32_t Mult2 = A.countr_zero(); |
0 |
9962 |
uint32_t Mult2 = A.countr_zero(); |
0 |
| 9963 |
// D = 2^Mult2 |
--- |
9963 |
// D = 2^Mult2 |
--- |
| 9964 |
|
--- |
9964 |
|
--- |
| 9965 |
// 2. Check if B is divisible by D. |
--- |
9965 |
// 2. Check if B is divisible by D. |
--- |
| 9966 |
// |
--- |
9966 |
// |
--- |
| 9967 |
// B is divisible by D if and only if the multiplicity of prime factor 2 for B |
--- |
9967 |
// B is divisible by D if and only if the multiplicity of prime factor 2 for B |
--- |
| 9968 |
// is not less than multiplicity of this prime factor for D. |
--- |
9968 |
// is not less than multiplicity of this prime factor for D. |
--- |
| 9969 |
if (SE.getMinTrailingZeros(B) < Mult2) |
0 |
9969 |
if (SE.getMinTrailingZeros(B) < Mult2) |
0 |
| 9970 |
return SE.getCouldNotCompute(); |
0 |
9970 |
return SE.getCouldNotCompute(); |
0 |
| 9971 |
|
--- |
9971 |
|
--- |
| 9972 |
// 3. Compute I: the multiplicative inverse of (A / D) in arithmetic |
--- |
9972 |
// 3. Compute I: the multiplicative inverse of (A / D) in arithmetic |
--- |
| 9973 |
// modulo (N / D). |
--- |
9973 |
// modulo (N / D). |
--- |
| 9974 |
// |
--- |
9974 |
// |
--- |
| 9975 |
// If D == 1, (N / D) == N == 2^BW, so we need one extra bit to represent |
--- |
9975 |
// If D == 1, (N / D) == N == 2^BW, so we need one extra bit to represent |
--- |
| 9976 |
// (N / D) in general. The inverse itself always fits into BW bits, though, |
--- |
9976 |
// (N / D) in general. The inverse itself always fits into BW bits, though, |
--- |
| 9977 |
// so we immediately truncate it. |
--- |
9977 |
// so we immediately truncate it. |
--- |
| 9978 |
APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D |
0 |
9978 |
APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D |
0 |
| 9979 |
APInt Mod(BW + 1, 0); |
0 |
9979 |
APInt Mod(BW + 1, 0); |
0 |
| 9980 |
Mod.setBit(BW - Mult2); // Mod = N / D |
0 |
9980 |
Mod.setBit(BW - Mult2); // Mod = N / D |
0 |
| 9981 |
APInt I = AD.multiplicativeInverse(Mod).trunc(BW); |
0 |
9981 |
APInt I = AD.multiplicativeInverse(Mod).trunc(BW); |
0 |
| 9982 |
|
--- |
9982 |
|
--- |
| 9983 |
// 4. Compute the minimum unsigned root of the equation: |
--- |
9983 |
// 4. Compute the minimum unsigned root of the equation: |
--- |
| 9984 |
// I * (B / D) mod (N / D) |
--- |
9984 |
// I * (B / D) mod (N / D) |
--- |
| 9985 |
// To simplify the computation, we factor out the divide by D: |
--- |
9985 |
// To simplify the computation, we factor out the divide by D: |
--- |
| 9986 |
// (I * B mod N) / D |
--- |
9986 |
// (I * B mod N) / D |
--- |
| 9987 |
const SCEV *D = SE.getConstant(APInt::getOneBitSet(BW, Mult2)); |
0 |
9987 |
const SCEV *D = SE.getConstant(APInt::getOneBitSet(BW, Mult2)); |
0 |
| 9988 |
return SE.getUDivExactExpr(SE.getMulExpr(B, SE.getConstant(I)), D); |
0 |
9988 |
return SE.getUDivExactExpr(SE.getMulExpr(B, SE.getConstant(I)), D); |
0 |
| 9989 |
} |
0 |
9989 |
} |
0 |
| 9990 |
|
--- |
9990 |
|
--- |
| 9991 |
/// For a given quadratic addrec, generate coefficients of the corresponding |
--- |
9991 |
/// For a given quadratic addrec, generate coefficients of the corresponding |
--- |
| 9992 |
/// quadratic equation, multiplied by a common value to ensure that they are |
--- |
9992 |
/// quadratic equation, multiplied by a common value to ensure that they are |
--- |
| 9993 |
/// integers. |
--- |
9993 |
/// integers. |
--- |
| 9994 |
/// The returned value is a tuple { A, B, C, M, BitWidth }, where |
--- |
9994 |
/// The returned value is a tuple { A, B, C, M, BitWidth }, where |
--- |
| 9995 |
/// Ax^2 + Bx + C is the quadratic function, M is the value that A, B and C |
--- |
9995 |
/// Ax^2 + Bx + C is the quadratic function, M is the value that A, B and C |
--- |
| 9996 |
/// were multiplied by, and BitWidth is the bit width of the original addrec |
--- |
9996 |
/// were multiplied by, and BitWidth is the bit width of the original addrec |
--- |
| 9997 |
/// coefficients. |
--- |
9997 |
/// coefficients. |
--- |
| 9998 |
/// This function returns std::nullopt if the addrec coefficients are not |
--- |
9998 |
/// This function returns std::nullopt if the addrec coefficients are not |
--- |
| 9999 |
/// compile- time constants. |
--- |
9999 |
/// compile- time constants. |
--- |
| 10000 |
static std::optional> |
--- |
10000 |
static std::optional> |
--- |
| 10001 |
GetQuadraticEquation(const SCEVAddRecExpr *AddRec) { |
0 |
10001 |
GetQuadraticEquation(const SCEVAddRecExpr *AddRec) { |
0 |
| 10002 |
assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); |
0 |
10002 |
assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); |
0 |
| 10003 |
const SCEVConstant *LC = dyn_cast(AddRec->getOperand(0)); |
0 |
10003 |
const SCEVConstant *LC = dyn_cast(AddRec->getOperand(0)); |
0 |
| 10004 |
const SCEVConstant *MC = dyn_cast(AddRec->getOperand(1)); |
0 |
10004 |
const SCEVConstant *MC = dyn_cast(AddRec->getOperand(1)); |
0 |
| 10005 |
const SCEVConstant *NC = dyn_cast(AddRec->getOperand(2)); |
0 |
10005 |
const SCEVConstant *NC = dyn_cast(AddRec->getOperand(2)); |
0 |
| 10006 |
LLVM_DEBUG(dbgs() << __func__ << ": analyzing quadratic addrec: " |
0 |
10006 |
LLVM_DEBUG(dbgs() << __func__ << ": analyzing quadratic addrec: " |
0 |
| 10007 |
<< *AddRec << '\n'); |
--- |
10007 |
<< *AddRec << '\n'); |
--- |
| 10008 |
|
--- |
10008 |
|
--- |
| 10009 |
// We currently can only solve this if the coefficients are constants. |
--- |
10009 |
// We currently can only solve this if the coefficients are constants. |
--- |
| 10010 |
if (!LC || !MC || !NC) { |
0 |
10010 |
if (!LC || !MC || !NC) { |
0 |
| 10011 |
LLVM_DEBUG(dbgs() << __func__ << ": coefficients are not constant\n"); |
0 |
10011 |
LLVM_DEBUG(dbgs() << __func__ << ": coefficients are not constant\n"); |
0 |
| 10012 |
return std::nullopt; |
0 |
10012 |
return std::nullopt; |
0 |
| 10013 |
} |
--- |
10013 |
} |
--- |
| 10014 |
|
--- |
10014 |
|
--- |
| 10015 |
APInt L = LC->getAPInt(); |
0 |
10015 |
APInt L = LC->getAPInt(); |
0 |
| 10016 |
APInt M = MC->getAPInt(); |
0 |
10016 |
APInt M = MC->getAPInt(); |
0 |
| 10017 |
APInt N = NC->getAPInt(); |
0 |
10017 |
APInt N = NC->getAPInt(); |
0 |
| 10018 |
assert(!N.isZero() && "This is not a quadratic addrec"); |
0 |
10018 |
assert(!N.isZero() && "This is not a quadratic addrec"); |
0 |
| 10019 |
|
--- |
10019 |
|
--- |
| 10020 |
unsigned BitWidth = LC->getAPInt().getBitWidth(); |
0 |
10020 |
unsigned BitWidth = LC->getAPInt().getBitWidth(); |
0 |
| 10021 |
unsigned NewWidth = BitWidth + 1; |
0 |
10021 |
unsigned NewWidth = BitWidth + 1; |
0 |
| 10022 |
LLVM_DEBUG(dbgs() << __func__ << ": addrec coeff bw: " |
0 |
10022 |
LLVM_DEBUG(dbgs() << __func__ << ": addrec coeff bw: " |
0 |
| 10023 |
<< BitWidth << '\n'); |
--- |
10023 |
<< BitWidth << '\n'); |
--- |
| 10024 |
// The sign-extension (as opposed to a zero-extension) here matches the |
--- |
10024 |
// The sign-extension (as opposed to a zero-extension) here matches the |
--- |
| 10025 |
// extension used in SolveQuadraticEquationWrap (with the same motivation). |
--- |
10025 |
// extension used in SolveQuadraticEquationWrap (with the same motivation). |
--- |
| 10026 |
N = N.sext(NewWidth); |
0 |
10026 |
N = N.sext(NewWidth); |
0 |
| 10027 |
M = M.sext(NewWidth); |
0 |
10027 |
M = M.sext(NewWidth); |
0 |
| 10028 |
L = L.sext(NewWidth); |
0 |
10028 |
L = L.sext(NewWidth); |
0 |
| 10029 |
|
--- |
10029 |
|
--- |
| 10030 |
// The increments are M, M+N, M+2N, ..., so the accumulated values are |
--- |
10030 |
// The increments are M, M+N, M+2N, ..., so the accumulated values are |
--- |
| 10031 |
// L+M, (L+M)+(M+N), (L+M)+(M+N)+(M+2N), ..., that is, |
--- |
10031 |
// L+M, (L+M)+(M+N), (L+M)+(M+N)+(M+2N), ..., that is, |
--- |
| 10032 |
// L+M, L+2M+N, L+3M+3N, ... |
--- |
10032 |
// L+M, L+2M+N, L+3M+3N, ... |
--- |
| 10033 |
// After n iterations the accumulated value Acc is L + nM + n(n-1)/2 N. |
--- |
10033 |
// After n iterations the accumulated value Acc is L + nM + n(n-1)/2 N. |
--- |
| 10034 |
// |
--- |
10034 |
// |
--- |
| 10035 |
// The equation Acc = 0 is then |
--- |
10035 |
// The equation Acc = 0 is then |
--- |
| 10036 |
// L + nM + n(n-1)/2 N = 0, or 2L + 2M n + n(n-1) N = 0. |
--- |
10036 |
// L + nM + n(n-1)/2 N = 0, or 2L + 2M n + n(n-1) N = 0. |
--- |
| 10037 |
// In a quadratic form it becomes: |
--- |
10037 |
// In a quadratic form it becomes: |
--- |
| 10038 |
// N n^2 + (2M-N) n + 2L = 0. |
--- |
10038 |
// N n^2 + (2M-N) n + 2L = 0. |
--- |
| 10039 |
|
--- |
10039 |
|
--- |
| 10040 |
APInt A = N; |
0 |
10040 |
APInt A = N; |
0 |
| 10041 |
APInt B = 2 * M - A; |
0 |
10041 |
APInt B = 2 * M - A; |
0 |
| 10042 |
APInt C = 2 * L; |
0 |
10042 |
APInt C = 2 * L; |
0 |
| 10043 |
APInt T = APInt(NewWidth, 2); |
0 |
10043 |
APInt T = APInt(NewWidth, 2); |
0 |
| 10044 |
LLVM_DEBUG(dbgs() << __func__ << ": equation " << A << "x^2 + " << B |
0 |
10044 |
LLVM_DEBUG(dbgs() << __func__ << ": equation " << A << "x^2 + " << B |
0 |
| 10045 |
<< "x + " << C << ", coeff bw: " << NewWidth |
--- |
10045 |
<< "x + " << C << ", coeff bw: " << NewWidth |
--- |
| 10046 |
<< ", multiplied by " << T << '\n'); |
--- |
10046 |
<< ", multiplied by " << T << '\n'); |
--- |
| 10047 |
return std::make_tuple(A, B, C, T, BitWidth); |
0 |
10047 |
return std::make_tuple(A, B, C, T, BitWidth); |
0 |
| 10048 |
} |
0 |
10048 |
} |
0 |
| 10049 |
|
--- |
10049 |
|
--- |
| 10050 |
/// Helper function to compare optional APInts: |
--- |
10050 |
/// Helper function to compare optional APInts: |
--- |
| 10051 |
/// (a) if X and Y both exist, return min(X, Y), |
--- |
10051 |
/// (a) if X and Y both exist, return min(X, Y), |
--- |
| 10052 |
/// (b) if neither X nor Y exist, return std::nullopt, |
--- |
10052 |
/// (b) if neither X nor Y exist, return std::nullopt, |
--- |
| 10053 |
/// (c) if exactly one of X and Y exists, return that value. |
--- |
10053 |
/// (c) if exactly one of X and Y exists, return that value. |
--- |
| 10054 |
static std::optional MinOptional(std::optional X, |
0 |
10054 |
static std::optional MinOptional(std::optional X, |
0 |
| 10055 |
std::optional Y) { |
--- |
10055 |
std::optional Y) { |
--- |
| 10056 |
if (X && Y) { |
0 |
10056 |
if (X && Y) { |
0 |
| 10057 |
unsigned W = std::max(X->getBitWidth(), Y->getBitWidth()); |
0 |
10057 |
unsigned W = std::max(X->getBitWidth(), Y->getBitWidth()); |
0 |
| 10058 |
APInt XW = X->sext(W); |
0 |
10058 |
APInt XW = X->sext(W); |
0 |
| 10059 |
APInt YW = Y->sext(W); |
0 |
10059 |
APInt YW = Y->sext(W); |
0 |
| 10060 |
return XW.slt(YW) ? *X : *Y; |
0 |
10060 |
return XW.slt(YW) ? *X : *Y; |
0 |
| 10061 |
} |
0 |
10061 |
} |
0 |
| 10062 |
if (!X && !Y) |
0 |
10062 |
if (!X && !Y) |
0 |
| 10063 |
return std::nullopt; |
0 |
10063 |
return std::nullopt; |
0 |
| 10064 |
return X ? *X : *Y; |
0 |
10064 |
return X ? *X : *Y; |
0 |
| 10065 |
} |
--- |
10065 |
} |
--- |
| 10066 |
|
--- |
10066 |
|
--- |
| 10067 |
/// Helper function to truncate an optional APInt to a given BitWidth. |
--- |
10067 |
/// Helper function to truncate an optional APInt to a given BitWidth. |
--- |
| 10068 |
/// When solving addrec-related equations, it is preferable to return a value |
--- |
10068 |
/// When solving addrec-related equations, it is preferable to return a value |
--- |
| 10069 |
/// that has the same bit width as the original addrec's coefficients. If the |
--- |
10069 |
/// that has the same bit width as the original addrec's coefficients. If the |
--- |
| 10070 |
/// solution fits in the original bit width, truncate it (except for i1). |
--- |
10070 |
/// solution fits in the original bit width, truncate it (except for i1). |
--- |
| 10071 |
/// Returning a value of a different bit width may inhibit some optimizations. |
--- |
10071 |
/// Returning a value of a different bit width may inhibit some optimizations. |
--- |
| 10072 |
/// |
--- |
10072 |
/// |
--- |
| 10073 |
/// In general, a solution to a quadratic equation generated from an addrec |
--- |
10073 |
/// In general, a solution to a quadratic equation generated from an addrec |
--- |
| 10074 |
/// may require BW+1 bits, where BW is the bit width of the addrec's |
--- |
10074 |
/// may require BW+1 bits, where BW is the bit width of the addrec's |
--- |
| 10075 |
/// coefficients. The reason is that the coefficients of the quadratic |
--- |
10075 |
/// coefficients. The reason is that the coefficients of the quadratic |
--- |
| 10076 |
/// equation are BW+1 bits wide (to avoid truncation when converting from |
--- |
10076 |
/// equation are BW+1 bits wide (to avoid truncation when converting from |
--- |
| 10077 |
/// the addrec to the equation). |
--- |
10077 |
/// the addrec to the equation). |
--- |
| 10078 |
static std::optional TruncIfPossible(std::optional X, |
0 |
10078 |
static std::optional TruncIfPossible(std::optional X, |
0 |
| 10079 |
unsigned BitWidth) { |
--- |
10079 |
unsigned BitWidth) { |
--- |
| 10080 |
if (!X) |
0 |
10080 |
if (!X) |
0 |
| 10081 |
return std::nullopt; |
0 |
10081 |
return std::nullopt; |
0 |
| 10082 |
unsigned W = X->getBitWidth(); |
0 |
10082 |
unsigned W = X->getBitWidth(); |
0 |
| 10083 |
if (BitWidth > 1 && BitWidth < W && X->isIntN(BitWidth)) |
0 |
10083 |
if (BitWidth > 1 && BitWidth < W && X->isIntN(BitWidth)) |
0 |
| 10084 |
return X->trunc(BitWidth); |
0 |
10084 |
return X->trunc(BitWidth); |
0 |
| 10085 |
return X; |
0 |
10085 |
return X; |
0 |
| 10086 |
} |
--- |
10086 |
} |
--- |
| 10087 |
|
--- |
10087 |
|
--- |
| 10088 |
/// Let c(n) be the value of the quadratic chrec {L,+,M,+,N} after n |
--- |
10088 |
/// Let c(n) be the value of the quadratic chrec {L,+,M,+,N} after n |
--- |
| 10089 |
/// iterations. The values L, M, N are assumed to be signed, and they |
--- |
10089 |
/// iterations. The values L, M, N are assumed to be signed, and they |
--- |
| 10090 |
/// should all have the same bit widths. |
--- |
10090 |
/// should all have the same bit widths. |
--- |
| 10091 |
/// Find the least n >= 0 such that c(n) = 0 in the arithmetic modulo 2^BW, |
--- |
10091 |
/// Find the least n >= 0 such that c(n) = 0 in the arithmetic modulo 2^BW, |
--- |
| 10092 |
/// where BW is the bit width of the addrec's coefficients. |
--- |
10092 |
/// where BW is the bit width of the addrec's coefficients. |
--- |
| 10093 |
/// If the calculated value is a BW-bit integer (for BW > 1), it will be |
--- |
10093 |
/// If the calculated value is a BW-bit integer (for BW > 1), it will be |
--- |
| 10094 |
/// returned as such, otherwise the bit width of the returned value may |
--- |
10094 |
/// returned as such, otherwise the bit width of the returned value may |
--- |
| 10095 |
/// be greater than BW. |
--- |
10095 |
/// be greater than BW. |
--- |
| 10096 |
/// |
--- |
10096 |
/// |
--- |
| 10097 |
/// This function returns std::nullopt if |
--- |
10097 |
/// This function returns std::nullopt if |
--- |
| 10098 |
/// (a) the addrec coefficients are not constant, or |
--- |
10098 |
/// (a) the addrec coefficients are not constant, or |
--- |
| 10099 |
/// (b) SolveQuadraticEquationWrap was unable to find a solution. For cases |
--- |
10099 |
/// (b) SolveQuadraticEquationWrap was unable to find a solution. For cases |
--- |
| 10100 |
/// like x^2 = 5, no integer solutions exist, in other cases an integer |
--- |
10100 |
/// like x^2 = 5, no integer solutions exist, in other cases an integer |
--- |
| 10101 |
/// solution may exist, but SolveQuadraticEquationWrap may fail to find it. |
--- |
10101 |
/// solution may exist, but SolveQuadraticEquationWrap may fail to find it. |
--- |
| 10102 |
static std::optional |
--- |
10102 |
static std::optional |
--- |
| 10103 |
SolveQuadraticAddRecExact(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { |
0 |
10103 |
SolveQuadraticAddRecExact(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { |
0 |
| 10104 |
APInt A, B, C, M; |
0 |
10104 |
APInt A, B, C, M; |
0 |
| 10105 |
unsigned BitWidth; |
--- |
10105 |
unsigned BitWidth; |
--- |
| 10106 |
auto T = GetQuadraticEquation(AddRec); |
0 |
10106 |
auto T = GetQuadraticEquation(AddRec); |
0 |
| 10107 |
if (!T) |
0 |
10107 |
if (!T) |
0 |
| 10108 |
return std::nullopt; |
0 |
10108 |
return std::nullopt; |
0 |
| 10109 |
|
--- |
10109 |
|
--- |
| 10110 |
std::tie(A, B, C, M, BitWidth) = *T; |
0 |
10110 |
std::tie(A, B, C, M, BitWidth) = *T; |
0 |
| 10111 |
LLVM_DEBUG(dbgs() << __func__ << ": solving for unsigned overflow\n"); |
0 |
10111 |
LLVM_DEBUG(dbgs() << __func__ << ": solving for unsigned overflow\n"); |
0 |
| 10112 |
std::optional X = |
--- |
10112 |
std::optional X = |
--- |
| 10113 |
APIntOps::SolveQuadraticEquationWrap(A, B, C, BitWidth + 1); |
0 |
10113 |
APIntOps::SolveQuadraticEquationWrap(A, B, C, BitWidth + 1); |
0 |
| 10114 |
if (!X) |
0 |
10114 |
if (!X) |
0 |
| 10115 |
return std::nullopt; |
0 |
10115 |
return std::nullopt; |
0 |
| 10116 |
|
--- |
10116 |
|
--- |
| 10117 |
ConstantInt *CX = ConstantInt::get(SE.getContext(), *X); |
0 |
10117 |
ConstantInt *CX = ConstantInt::get(SE.getContext(), *X); |
0 |
| 10118 |
ConstantInt *V = EvaluateConstantChrecAtConstant(AddRec, CX, SE); |
0 |
10118 |
ConstantInt *V = EvaluateConstantChrecAtConstant(AddRec, CX, SE); |
0 |
| 10119 |
if (!V->isZero()) |
0 |
10119 |
if (!V->isZero()) |
0 |
| 10120 |
return std::nullopt; |
0 |
10120 |
return std::nullopt; |
0 |
| 10121 |
|
--- |
10121 |
|
--- |
| 10122 |
return TruncIfPossible(X, BitWidth); |
0 |
10122 |
return TruncIfPossible(X, BitWidth); |
0 |
| 10123 |
} |
0 |
10123 |
} |
0 |
| 10124 |
|
--- |
10124 |
|
--- |
| 10125 |
/// Let c(n) be the value of the quadratic chrec {0,+,M,+,N} after n |
--- |
10125 |
/// Let c(n) be the value of the quadratic chrec {0,+,M,+,N} after n |
--- |
| 10126 |
/// iterations. The values M, N are assumed to be signed, and they |
--- |
10126 |
/// iterations. The values M, N are assumed to be signed, and they |
--- |
| 10127 |
/// should all have the same bit widths. |
--- |
10127 |
/// should all have the same bit widths. |
--- |
| 10128 |
/// Find the least n such that c(n) does not belong to the given range, |
--- |
10128 |
/// Find the least n such that c(n) does not belong to the given range, |
--- |
| 10129 |
/// while c(n-1) does. |
--- |
10129 |
/// while c(n-1) does. |
--- |
| 10130 |
/// |
--- |
10130 |
/// |
--- |
| 10131 |
/// This function returns std::nullopt if |
--- |
10131 |
/// This function returns std::nullopt if |
--- |
| 10132 |
/// (a) the addrec coefficients are not constant, or |
--- |
10132 |
/// (a) the addrec coefficients are not constant, or |
--- |
| 10133 |
/// (b) SolveQuadraticEquationWrap was unable to find a solution for the |
--- |
10133 |
/// (b) SolveQuadraticEquationWrap was unable to find a solution for the |
--- |
| 10134 |
/// bounds of the range. |
--- |
10134 |
/// bounds of the range. |
--- |
| 10135 |
static std::optional |
--- |
10135 |
static std::optional |
--- |
| 10136 |
SolveQuadraticAddRecRange(const SCEVAddRecExpr *AddRec, |
0 |
10136 |
SolveQuadraticAddRecRange(const SCEVAddRecExpr *AddRec, |
0 |
| 10137 |
const ConstantRange &Range, ScalarEvolution &SE) { |
--- |
10137 |
const ConstantRange &Range, ScalarEvolution &SE) { |
--- |
| 10138 |
assert(AddRec->getOperand(0)->isZero() && |
0 |
10138 |
assert(AddRec->getOperand(0)->isZero() && |
0 |
| 10139 |
"Starting value of addrec should be 0"); |
--- |
10139 |
"Starting value of addrec should be 0"); |
--- |
| 10140 |
LLVM_DEBUG(dbgs() << __func__ << ": solving boundary crossing for range " |
0 |
10140 |
LLVM_DEBUG(dbgs() << __func__ << ": solving boundary crossing for range " |
0 |
| 10141 |
<< Range << ", addrec " << *AddRec << '\n'); |
--- |
10141 |
<< Range << ", addrec " << *AddRec << '\n'); |
--- |
| 10142 |
// This case is handled in getNumIterationsInRange. Here we can assume that |
--- |
10142 |
// This case is handled in getNumIterationsInRange. Here we can assume that |
--- |
| 10143 |
// we start in the range. |
--- |
10143 |
// we start in the range. |
--- |
| 10144 |
assert(Range.contains(APInt(SE.getTypeSizeInBits(AddRec->getType()), 0)) && |
0 |
10144 |
assert(Range.contains(APInt(SE.getTypeSizeInBits(AddRec->getType()), 0)) && |
0 |
| 10145 |
"Addrec's initial value should be in range"); |
--- |
10145 |
"Addrec's initial value should be in range"); |
--- |
| 10146 |
|
--- |
10146 |
|
--- |
| 10147 |
APInt A, B, C, M; |
0 |
10147 |
APInt A, B, C, M; |
0 |
| 10148 |
unsigned BitWidth; |
--- |
10148 |
unsigned BitWidth; |
--- |
| 10149 |
auto T = GetQuadraticEquation(AddRec); |
0 |
10149 |
auto T = GetQuadraticEquation(AddRec); |
0 |
| 10150 |
if (!T) |
0 |
10150 |
if (!T) |
0 |
| 10151 |
return std::nullopt; |
0 |
10151 |
return std::nullopt; |
0 |
| 10152 |
|
--- |
10152 |
|
--- |
| 10153 |
// Be careful about the return value: there can be two reasons for not |
--- |
10153 |
// Be careful about the return value: there can be two reasons for not |
--- |
| 10154 |
// returning an actual number. First, if no solutions to the equations |
--- |
10154 |
// returning an actual number. First, if no solutions to the equations |
--- |
| 10155 |
// were found, and second, if the solutions don't leave the given range. |
--- |
10155 |
// were found, and second, if the solutions don't leave the given range. |
--- |
| 10156 |
// The first case means that the actual solution is "unknown", the second |
--- |
10156 |
// The first case means that the actual solution is "unknown", the second |
--- |
| 10157 |
// means that it's known, but not valid. If the solution is unknown, we |
--- |
10157 |
// means that it's known, but not valid. If the solution is unknown, we |
--- |
| 10158 |
// cannot make any conclusions. |
--- |
10158 |
// cannot make any conclusions. |
--- |
| 10159 |
// Return a pair: the optional solution and a flag indicating if the |
--- |
10159 |
// Return a pair: the optional solution and a flag indicating if the |
--- |
| 10160 |
// solution was found. |
--- |
10160 |
// solution was found. |
--- |
| 10161 |
auto SolveForBoundary = |
--- |
10161 |
auto SolveForBoundary = |
--- |
| 10162 |
[&](APInt Bound) -> std::pair, bool> { |
0 |
10162 |
[&](APInt Bound) -> std::pair, bool> { |
0 |
| 10163 |
// Solve for signed overflow and unsigned overflow, pick the lower |
--- |
10163 |
// Solve for signed overflow and unsigned overflow, pick the lower |
--- |
| 10164 |
// solution. |
--- |
10164 |
// solution. |
--- |
| 10165 |
LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: checking boundary " |
0 |
10165 |
LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: checking boundary " |
0 |
| 10166 |
<< Bound << " (before multiplying by " << M << ")\n"); |
--- |
10166 |
<< Bound << " (before multiplying by " << M << ")\n"); |
--- |
| 10167 |
Bound *= M; // The quadratic equation multiplier. |
0 |
10167 |
Bound *= M; // The quadratic equation multiplier. |
0 |
| 10168 |
|
--- |
10168 |
|
--- |
| 10169 |
std::optional SO; |
0 |
10169 |
std::optional SO; |
0 |
| 10170 |
if (BitWidth > 1) { |
0 |
10170 |
if (BitWidth > 1) { |
0 |
| 10171 |
LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: solving for " |
0 |
10171 |
LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: solving for " |
0 |
| 10172 |
"signed overflow\n"); |
--- |
10172 |
"signed overflow\n"); |
--- |
| 10173 |
SO = APIntOps::SolveQuadraticEquationWrap(A, B, -Bound, BitWidth); |
0 |
10173 |
SO = APIntOps::SolveQuadraticEquationWrap(A, B, -Bound, BitWidth); |
0 |
| 10174 |
} |
--- |
10174 |
} |
--- |
| 10175 |
LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: solving for " |
0 |
10175 |
LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: solving for " |
0 |
| 10176 |
"unsigned overflow\n"); |
--- |
10176 |
"unsigned overflow\n"); |
--- |
| 10177 |
std::optional UO = |
--- |
10177 |
std::optional UO = |
--- |
| 10178 |
APIntOps::SolveQuadraticEquationWrap(A, B, -Bound, BitWidth + 1); |
0 |
10178 |
APIntOps::SolveQuadraticEquationWrap(A, B, -Bound, BitWidth + 1); |
0 |
| 10179 |
|
--- |
10179 |
|
--- |
| 10180 |
auto LeavesRange = [&] (const APInt &X) { |
0 |
10180 |
auto LeavesRange = [&] (const APInt &X) { |
0 |
| 10181 |
ConstantInt *C0 = ConstantInt::get(SE.getContext(), X); |
0 |
10181 |
ConstantInt *C0 = ConstantInt::get(SE.getContext(), X); |
0 |
| 10182 |
ConstantInt *V0 = EvaluateConstantChrecAtConstant(AddRec, C0, SE); |
0 |
10182 |
ConstantInt *V0 = EvaluateConstantChrecAtConstant(AddRec, C0, SE); |
0 |
| 10183 |
if (Range.contains(V0->getValue())) |
0 |
10183 |
if (Range.contains(V0->getValue())) |
0 |
| 10184 |
return false; |
0 |
10184 |
return false; |
0 |
| 10185 |
// X should be at least 1, so X-1 is non-negative. |
--- |
10185 |
// X should be at least 1, so X-1 is non-negative. |
--- |
| 10186 |
ConstantInt *C1 = ConstantInt::get(SE.getContext(), X-1); |
0 |
10186 |
ConstantInt *C1 = ConstantInt::get(SE.getContext(), X-1); |
0 |
| 10187 |
ConstantInt *V1 = EvaluateConstantChrecAtConstant(AddRec, C1, SE); |
0 |
10187 |
ConstantInt *V1 = EvaluateConstantChrecAtConstant(AddRec, C1, SE); |
0 |
| 10188 |
if (Range.contains(V1->getValue())) |
0 |
10188 |
if (Range.contains(V1->getValue())) |
0 |
| 10189 |
return true; |
0 |
10189 |
return true; |
0 |
| 10190 |
return false; |
0 |
10190 |
return false; |
0 |
| 10191 |
}; |
0 |
10191 |
}; |
0 |
| 10192 |
|
--- |
10192 |
|
--- |
| 10193 |
// If SolveQuadraticEquationWrap returns std::nullopt, it means that there |
--- |
10193 |
// If SolveQuadraticEquationWrap returns std::nullopt, it means that there |
--- |
| 10194 |
// can be a solution, but the function failed to find it. We cannot treat it |
--- |
10194 |
// can be a solution, but the function failed to find it. We cannot treat it |
--- |
| 10195 |
// as "no solution". |
--- |
10195 |
// as "no solution". |
--- |
| 10196 |
if (!SO || !UO) |
0 |
10196 |
if (!SO || !UO) |
0 |
| 10197 |
return {std::nullopt, false}; |
0 |
10197 |
return {std::nullopt, false}; |
0 |
| 10198 |
|
--- |
10198 |
|
--- |
| 10199 |
// Check the smaller value first to see if it leaves the range. |
--- |
10199 |
// Check the smaller value first to see if it leaves the range. |
--- |
| 10200 |
// At this point, both SO and UO must have values. |
--- |
10200 |
// At this point, both SO and UO must have values. |
--- |
| 10201 |
std::optional Min = MinOptional(SO, UO); |
0 |
10201 |
std::optional Min = MinOptional(SO, UO); |
0 |
| 10202 |
if (LeavesRange(*Min)) |
0 |
10202 |
if (LeavesRange(*Min)) |
0 |
| 10203 |
return { Min, true }; |
0 |
10203 |
return { Min, true }; |
0 |
| 10204 |
std::optional Max = Min == SO ? UO : SO; |
0 |
10204 |
std::optional Max = Min == SO ? UO : SO; |
0 |
| 10205 |
if (LeavesRange(*Max)) |
0 |
10205 |
if (LeavesRange(*Max)) |
0 |
| 10206 |
return { Max, true }; |
0 |
10206 |
return { Max, true }; |
0 |
| 10207 |
|
--- |
10207 |
|
--- |
| 10208 |
// Solutions were found, but were eliminated, hence the "true". |
--- |
10208 |
// Solutions were found, but were eliminated, hence the "true". |
--- |
| 10209 |
return {std::nullopt, true}; |
0 |
10209 |
return {std::nullopt, true}; |
0 |
| 10210 |
}; |
0 |
10210 |
}; |
0 |
| 10211 |
|
--- |
10211 |
|
--- |
| 10212 |
std::tie(A, B, C, M, BitWidth) = *T; |
0 |
10212 |
std::tie(A, B, C, M, BitWidth) = *T; |
0 |
| 10213 |
// Lower bound is inclusive, subtract 1 to represent the exiting value. |
--- |
10213 |
// Lower bound is inclusive, subtract 1 to represent the exiting value. |
--- |
| 10214 |
APInt Lower = Range.getLower().sext(A.getBitWidth()) - 1; |
0 |
10214 |
APInt Lower = Range.getLower().sext(A.getBitWidth()) - 1; |
0 |
| 10215 |
APInt Upper = Range.getUpper().sext(A.getBitWidth()); |
0 |
10215 |
APInt Upper = Range.getUpper().sext(A.getBitWidth()); |
0 |
| 10216 |
auto SL = SolveForBoundary(Lower); |
0 |
10216 |
auto SL = SolveForBoundary(Lower); |
0 |
| 10217 |
auto SU = SolveForBoundary(Upper); |
0 |
10217 |
auto SU = SolveForBoundary(Upper); |
0 |
| 10218 |
// If any of the solutions was unknown, no meaninigful conclusions can |
--- |
10218 |
// If any of the solutions was unknown, no meaninigful conclusions can |
--- |
| 10219 |
// be made. |
--- |
10219 |
// be made. |
--- |
| 10220 |
if (!SL.second || !SU.second) |
0 |
10220 |
if (!SL.second || !SU.second) |
0 |
| 10221 |
return std::nullopt; |
0 |
10221 |
return std::nullopt; |
0 |
| 10222 |
|
--- |
10222 |
|
--- |
| 10223 |
// Claim: The correct solution is not some value between Min and Max. |
--- |
10223 |
// Claim: The correct solution is not some value between Min and Max. |
--- |
| 10224 |
// |
--- |
10224 |
// |
--- |
| 10225 |
// Justification: Assuming that Min and Max are different values, one of |
--- |
10225 |
// Justification: Assuming that Min and Max are different values, one of |
--- |
| 10226 |
// them is when the first signed overflow happens, the other is when the |
--- |
10226 |
// them is when the first signed overflow happens, the other is when the |
--- |
| 10227 |
// first unsigned overflow happens. Crossing the range boundary is only |
--- |
10227 |
// first unsigned overflow happens. Crossing the range boundary is only |
--- |
| 10228 |
// possible via an overflow (treating 0 as a special case of it, modeling |
--- |
10228 |
// possible via an overflow (treating 0 as a special case of it, modeling |
--- |
| 10229 |
// an overflow as crossing k*2^W for some k). |
--- |
10229 |
// an overflow as crossing k*2^W for some k). |
--- |
| 10230 |
// |
--- |
10230 |
// |
--- |
| 10231 |
// The interesting case here is when Min was eliminated as an invalid |
--- |
10231 |
// The interesting case here is when Min was eliminated as an invalid |
--- |
| 10232 |
// solution, but Max was not. The argument is that if there was another |
--- |
10232 |
// solution, but Max was not. The argument is that if there was another |
--- |
| 10233 |
// overflow between Min and Max, it would also have been eliminated if |
--- |
10233 |
// overflow between Min and Max, it would also have been eliminated if |
--- |
| 10234 |
// it was considered. |
--- |
10234 |
// it was considered. |
--- |
| 10235 |
// |
--- |
10235 |
// |
--- |
| 10236 |
// For a given boundary, it is possible to have two overflows of the same |
--- |
10236 |
// For a given boundary, it is possible to have two overflows of the same |
--- |
| 10237 |
// type (signed/unsigned) without having the other type in between: this |
--- |
10237 |
// type (signed/unsigned) without having the other type in between: this |
--- |
| 10238 |
// can happen when the vertex of the parabola is between the iterations |
--- |
10238 |
// can happen when the vertex of the parabola is between the iterations |
--- |
| 10239 |
// corresponding to the overflows. This is only possible when the two |
--- |
10239 |
// corresponding to the overflows. This is only possible when the two |
--- |
| 10240 |
// overflows cross k*2^W for the same k. In such case, if the second one |
--- |
10240 |
// overflows cross k*2^W for the same k. In such case, if the second one |
--- |
| 10241 |
// left the range (and was the first one to do so), the first overflow |
--- |
10241 |
// left the range (and was the first one to do so), the first overflow |
--- |
| 10242 |
// would have to enter the range, which would mean that either we had left |
--- |
10242 |
// would have to enter the range, which would mean that either we had left |
--- |
| 10243 |
// the range before or that we started outside of it. Both of these cases |
--- |
10243 |
// the range before or that we started outside of it. Both of these cases |
--- |
| 10244 |
// are contradictions. |
--- |
10244 |
// are contradictions. |
--- |
| 10245 |
// |
--- |
10245 |
// |
--- |
| 10246 |
// Claim: In the case where SolveForBoundary returns std::nullopt, the correct |
--- |
10246 |
// Claim: In the case where SolveForBoundary returns std::nullopt, the correct |
--- |
| 10247 |
// solution is not some value between the Max for this boundary and the |
--- |
10247 |
// solution is not some value between the Max for this boundary and the |
--- |
| 10248 |
// Min of the other boundary. |
--- |
10248 |
// Min of the other boundary. |
--- |
| 10249 |
// |
--- |
10249 |
// |
--- |
| 10250 |
// Justification: Assume that we had such Max_A and Min_B corresponding |
--- |
10250 |
// Justification: Assume that we had such Max_A and Min_B corresponding |
--- |
| 10251 |
// to range boundaries A and B and such that Max_A < Min_B. If there was |
--- |
10251 |
// to range boundaries A and B and such that Max_A < Min_B. If there was |
--- |
| 10252 |
// a solution between Max_A and Min_B, it would have to be caused by an |
--- |
10252 |
// a solution between Max_A and Min_B, it would have to be caused by an |
--- |
| 10253 |
// overflow corresponding to either A or B. It cannot correspond to B, |
--- |
10253 |
// overflow corresponding to either A or B. It cannot correspond to B, |
--- |
| 10254 |
// since Min_B is the first occurrence of such an overflow. If it |
--- |
10254 |
// since Min_B is the first occurrence of such an overflow. If it |
--- |
| 10255 |
// corresponded to A, it would have to be either a signed or an unsigned |
--- |
10255 |
// corresponded to A, it would have to be either a signed or an unsigned |
--- |
| 10256 |
// overflow that is larger than both eliminated overflows for A. But |
--- |
10256 |
// overflow that is larger than both eliminated overflows for A. But |
--- |
| 10257 |
// between the eliminated overflows and this overflow, the values would |
--- |
10257 |
// between the eliminated overflows and this overflow, the values would |
--- |
| 10258 |
// cover the entire value space, thus crossing the other boundary, which |
--- |
10258 |
// cover the entire value space, thus crossing the other boundary, which |
--- |
| 10259 |
// is a contradiction. |
--- |
10259 |
// is a contradiction. |
--- |
| 10260 |
|
--- |
10260 |
|
--- |
| 10261 |
return TruncIfPossible(MinOptional(SL.first, SU.first), BitWidth); |
0 |
10261 |
return TruncIfPossible(MinOptional(SL.first, SU.first), BitWidth); |
0 |
| 10262 |
} |
0 |
10262 |
} |
0 |
| 10263 |
|
--- |
10263 |
|
--- |
| 10264 |
ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V, |
0 |
10264 |
ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V, |
0 |
| 10265 |
const Loop *L, |
--- |
10265 |
const Loop *L, |
--- |
| 10266 |
bool ControlsOnlyExit, |
--- |
10266 |
bool ControlsOnlyExit, |
--- |
| 10267 |
bool AllowPredicates) { |
--- |
10267 |
bool AllowPredicates) { |
--- |
| 10268 |
|
--- |
10268 |
|
--- |
| 10269 |
// This is only used for loops with a "x != y" exit test. The exit condition |
--- |
10269 |
// This is only used for loops with a "x != y" exit test. The exit condition |
--- |
| 10270 |
// is now expressed as a single expression, V = x-y. So the exit test is |
--- |
10270 |
// is now expressed as a single expression, V = x-y. So the exit test is |
--- |
| 10271 |
// effectively V != 0. We know and take advantage of the fact that this |
--- |
10271 |
// effectively V != 0. We know and take advantage of the fact that this |
--- |
| 10272 |
// expression only being used in a comparison by zero context. |
--- |
10272 |
// expression only being used in a comparison by zero context. |
--- |
| 10273 |
|
--- |
10273 |
|
--- |
| 10274 |
SmallPtrSet Predicates; |
0 |
10274 |
SmallPtrSet Predicates; |
0 |
| 10275 |
// If the value is a constant |
--- |
10275 |
// If the value is a constant |
--- |
| 10276 |
if (const SCEVConstant *C = dyn_cast(V)) { |
0 |
10276 |
if (const SCEVConstant *C = dyn_cast(V)) { |
0 |
| 10277 |
// If the value is already zero, the branch will execute zero times. |
--- |
10277 |
// If the value is already zero, the branch will execute zero times. |
--- |
| 10278 |
if (C->getValue()->isZero()) return C; |
0 |
10278 |
if (C->getValue()->isZero()) return C; |
0 |
| 10279 |
return getCouldNotCompute(); // Otherwise it will loop infinitely. |
0 |
10279 |
return getCouldNotCompute(); // Otherwise it will loop infinitely. |
0 |
| 10280 |
} |
--- |
10280 |
} |
--- |
| 10281 |
|
--- |
10281 |
|
--- |
| 10282 |
const SCEVAddRecExpr *AddRec = |
--- |
10282 |
const SCEVAddRecExpr *AddRec = |
--- |
| 10283 |
dyn_cast(stripInjectiveFunctions(V)); |
0 |
10283 |
dyn_cast(stripInjectiveFunctions(V)); |
0 |
| 10284 |
|
--- |
10284 |
|
--- |
| 10285 |
if (!AddRec && AllowPredicates) |
0 |
10285 |
if (!AddRec && AllowPredicates) |
0 |
| 10286 |
// Try to make this an AddRec using runtime tests, in the first X |
--- |
10286 |
// Try to make this an AddRec using runtime tests, in the first X |
--- |
| 10287 |
// iterations of this loop, where X is the SCEV expression found by the |
--- |
10287 |
// iterations of this loop, where X is the SCEV expression found by the |
--- |
| 10288 |
// algorithm below. |
--- |
10288 |
// algorithm below. |
--- |
| 10289 |
AddRec = convertSCEVToAddRecWithPredicates(V, L, Predicates); |
0 |
10289 |
AddRec = convertSCEVToAddRecWithPredicates(V, L, Predicates); |
0 |
| 10290 |
|
--- |
10290 |
|
--- |
| 10291 |
if (!AddRec || AddRec->getLoop() != L) |
0 |
10291 |
if (!AddRec || AddRec->getLoop() != L) |
0 |
| 10292 |
return getCouldNotCompute(); |
0 |
10292 |
return getCouldNotCompute(); |
0 |
| 10293 |
|
--- |
10293 |
|
--- |
| 10294 |
// If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of |
--- |
10294 |
// If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of |
--- |
| 10295 |
// the quadratic equation to solve it. |
--- |
10295 |
// the quadratic equation to solve it. |
--- |
| 10296 |
if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) { |
0 |
10296 |
if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) { |
0 |
| 10297 |
// We can only use this value if the chrec ends up with an exact zero |
--- |
10297 |
// We can only use this value if the chrec ends up with an exact zero |
--- |
| 10298 |
// value at this index. When solving for "X*X != 5", for example, we |
--- |
10298 |
// value at this index. When solving for "X*X != 5", for example, we |
--- |
| 10299 |
// should not accept a root of 2. |
--- |
10299 |
// should not accept a root of 2. |
--- |
| 10300 |
if (auto S = SolveQuadraticAddRecExact(AddRec, *this)) { |
0 |
10300 |
if (auto S = SolveQuadraticAddRecExact(AddRec, *this)) { |
0 |
| 10301 |
const auto *R = cast(getConstant(*S)); |
0 |
10301 |
const auto *R = cast(getConstant(*S)); |
0 |
| 10302 |
return ExitLimit(R, R, R, false, Predicates); |
0 |
10302 |
return ExitLimit(R, R, R, false, Predicates); |
0 |
| 10303 |
} |
0 |
10303 |
} |
0 |
| 10304 |
return getCouldNotCompute(); |
0 |
10304 |
return getCouldNotCompute(); |
0 |
| 10305 |
} |
--- |
10305 |
} |
--- |
| 10306 |
|
--- |
10306 |
|
--- |
| 10307 |
// Otherwise we can only handle this if it is affine. |
--- |
10307 |
// Otherwise we can only handle this if it is affine. |
--- |
| 10308 |
if (!AddRec->isAffine()) |
0 |
10308 |
if (!AddRec->isAffine()) |
0 |
| 10309 |
return getCouldNotCompute(); |
0 |
10309 |
return getCouldNotCompute(); |
0 |
| 10310 |
|
--- |
10310 |
|
--- |
| 10311 |
// If this is an affine expression, the execution count of this branch is |
--- |
10311 |
// If this is an affine expression, the execution count of this branch is |
--- |
| 10312 |
// the minimum unsigned root of the following equation: |
--- |
10312 |
// the minimum unsigned root of the following equation: |
--- |
| 10313 |
// |
--- |
10313 |
// |
--- |
| 10314 |
// Start + Step*N = 0 (mod 2^BW) |
--- |
10314 |
// Start + Step*N = 0 (mod 2^BW) |
--- |
| 10315 |
// |
--- |
10315 |
// |
--- |
| 10316 |
// equivalent to: |
--- |
10316 |
// equivalent to: |
--- |
| 10317 |
// |
--- |
10317 |
// |
--- |
| 10318 |
// Step*N = -Start (mod 2^BW) |
--- |
10318 |
// Step*N = -Start (mod 2^BW) |
--- |
| 10319 |
// |
--- |
10319 |
// |
--- |
| 10320 |
// where BW is the common bit width of Start and Step. |
--- |
10320 |
// where BW is the common bit width of Start and Step. |
--- |
| 10321 |
|
--- |
10321 |
|
--- |
| 10322 |
// Get the initial value for the loop. |
--- |
10322 |
// Get the initial value for the loop. |
--- |
| 10323 |
const SCEV *Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop()); |
0 |
10323 |
const SCEV *Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop()); |
0 |
| 10324 |
const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop()); |
0 |
10324 |
const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop()); |
0 |
| 10325 |
|
--- |
10325 |
|
--- |
| 10326 |
// For now we handle only constant steps. |
--- |
10326 |
// For now we handle only constant steps. |
--- |
| 10327 |
// |
--- |
10327 |
// |
--- |
| 10328 |
// TODO: Handle a nonconstant Step given AddRec. If the |
--- |
10328 |
// TODO: Handle a nonconstant Step given AddRec. If the |
--- |
| 10329 |
// AddRec is NUW, then (in an unsigned sense) it cannot be counting up to wrap |
--- |
10329 |
// AddRec is NUW, then (in an unsigned sense) it cannot be counting up to wrap |
--- |
| 10330 |
// to 0, it must be counting down to equal 0. Consequently, N = Start / -Step. |
--- |
10330 |
// to 0, it must be counting down to equal 0. Consequently, N = Start / -Step. |
--- |
| 10331 |
// We have not yet seen any such cases. |
--- |
10331 |
// We have not yet seen any such cases. |
--- |
| 10332 |
const SCEVConstant *StepC = dyn_cast(Step); |
0 |
10332 |
const SCEVConstant *StepC = dyn_cast(Step); |
0 |
| 10333 |
if (!StepC || StepC->getValue()->isZero()) |
0 |
10333 |
if (!StepC || StepC->getValue()->isZero()) |
0 |
| 10334 |
return getCouldNotCompute(); |
0 |
10334 |
return getCouldNotCompute(); |
0 |
| 10335 |
|
--- |
10335 |
|
--- |
| 10336 |
// For positive steps (counting up until unsigned overflow): |
--- |
10336 |
// For positive steps (counting up until unsigned overflow): |
--- |
| 10337 |
// N = -Start/Step (as unsigned) |
--- |
10337 |
// N = -Start/Step (as unsigned) |
--- |
| 10338 |
// For negative steps (counting down to zero): |
--- |
10338 |
// For negative steps (counting down to zero): |
--- |
| 10339 |
// N = Start/-Step |
--- |
10339 |
// N = Start/-Step |
--- |
| 10340 |
// First compute the unsigned distance from zero in the direction of Step. |
--- |
10340 |
// First compute the unsigned distance from zero in the direction of Step. |
--- |
| 10341 |
bool CountDown = StepC->getAPInt().isNegative(); |
0 |
10341 |
bool CountDown = StepC->getAPInt().isNegative(); |
0 |
| 10342 |
const SCEV *Distance = CountDown ? Start : getNegativeSCEV(Start); |
0 |
10342 |
const SCEV *Distance = CountDown ? Start : getNegativeSCEV(Start); |
0 |
| 10343 |
|
--- |
10343 |
|
--- |
| 10344 |
// Handle unitary steps, which cannot wraparound. |
--- |
10344 |
// Handle unitary steps, which cannot wraparound. |
--- |
| 10345 |
// 1*N = -Start; -1*N = Start (mod 2^BW), so: |
--- |
10345 |
// 1*N = -Start; -1*N = Start (mod 2^BW), so: |
--- |
| 10346 |
// N = Distance (as unsigned) |
--- |
10346 |
// N = Distance (as unsigned) |
--- |
| 10347 |
if (StepC->getValue()->isOne() || StepC->getValue()->isMinusOne()) { |
0 |
10347 |
if (StepC->getValue()->isOne() || StepC->getValue()->isMinusOne()) { |
0 |
| 10348 |
APInt MaxBECount = getUnsignedRangeMax(applyLoopGuards(Distance, L)); |
0 |
10348 |
APInt MaxBECount = getUnsignedRangeMax(applyLoopGuards(Distance, L)); |
0 |
| 10349 |
MaxBECount = APIntOps::umin(MaxBECount, getUnsignedRangeMax(Distance)); |
0 |
10349 |
MaxBECount = APIntOps::umin(MaxBECount, getUnsignedRangeMax(Distance)); |
0 |
| 10350 |
|
--- |
10350 |
|
--- |
| 10351 |
// When a loop like "for (int i = 0; i != n; ++i) { /* body */ }" is rotated, |
--- |
10351 |
// When a loop like "for (int i = 0; i != n; ++i) { /* body */ }" is rotated, |
--- |
| 10352 |
// we end up with a loop whose backedge-taken count is n - 1. Detect this |
--- |
10352 |
// we end up with a loop whose backedge-taken count is n - 1. Detect this |
--- |
| 10353 |
// case, and see if we can improve the bound. |
--- |
10353 |
// case, and see if we can improve the bound. |
--- |
| 10354 |
// |
--- |
10354 |
// |
--- |
| 10355 |
// Explicitly handling this here is necessary because getUnsignedRange |
--- |
10355 |
// Explicitly handling this here is necessary because getUnsignedRange |
--- |
| 10356 |
// isn't context-sensitive; it doesn't know that we only care about the |
--- |
10356 |
// isn't context-sensitive; it doesn't know that we only care about the |
--- |
| 10357 |
// range inside the loop. |
--- |
10357 |
// range inside the loop. |
--- |
| 10358 |
const SCEV *Zero = getZero(Distance->getType()); |
0 |
10358 |
const SCEV *Zero = getZero(Distance->getType()); |
0 |
| 10359 |
const SCEV *One = getOne(Distance->getType()); |
0 |
10359 |
const SCEV *One = getOne(Distance->getType()); |
0 |
| 10360 |
const SCEV *DistancePlusOne = getAddExpr(Distance, One); |
0 |
10360 |
const SCEV *DistancePlusOne = getAddExpr(Distance, One); |
0 |
| 10361 |
if (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, DistancePlusOne, Zero)) { |
0 |
10361 |
if (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, DistancePlusOne, Zero)) { |
0 |
| 10362 |
// If Distance + 1 doesn't overflow, we can compute the maximum distance |
--- |
10362 |
// If Distance + 1 doesn't overflow, we can compute the maximum distance |
--- |
| 10363 |
// as "unsigned_max(Distance + 1) - 1". |
--- |
10363 |
// as "unsigned_max(Distance + 1) - 1". |
--- |
| 10364 |
ConstantRange CR = getUnsignedRange(DistancePlusOne); |
0 |
10364 |
ConstantRange CR = getUnsignedRange(DistancePlusOne); |
0 |
| 10365 |
MaxBECount = APIntOps::umin(MaxBECount, CR.getUnsignedMax() - 1); |
0 |
10365 |
MaxBECount = APIntOps::umin(MaxBECount, CR.getUnsignedMax() - 1); |
0 |
| 10366 |
} |
0 |
10366 |
} |
0 |
| 10367 |
return ExitLimit(Distance, getConstant(MaxBECount), Distance, false, |
--- |
10367 |
return ExitLimit(Distance, getConstant(MaxBECount), Distance, false, |
--- |
| 10368 |
Predicates); |
0 |
10368 |
Predicates); |
0 |
| 10369 |
} |
0 |
10369 |
} |
0 |
| 10370 |
|
--- |
10370 |
|
--- |
| 10371 |
// If the condition controls loop exit (the loop exits only if the expression |
--- |
10371 |
// If the condition controls loop exit (the loop exits only if the expression |
--- |
| 10372 |
// is true) and the addition is no-wrap we can use unsigned divide to |
--- |
10372 |
// is true) and the addition is no-wrap we can use unsigned divide to |
--- |
| 10373 |
// compute the backedge count. In this case, the step may not divide the |
--- |
10373 |
// compute the backedge count. In this case, the step may not divide the |
--- |
| 10374 |
// distance, but we don't care because if the condition is "missed" the loop |
--- |
10374 |
// distance, but we don't care because if the condition is "missed" the loop |
--- |
| 10375 |
// will have undefined behavior due to wrapping. |
--- |
10375 |
// will have undefined behavior due to wrapping. |
--- |
| 10376 |
if (ControlsOnlyExit && AddRec->hasNoSelfWrap() && |
0 |
10376 |
if (ControlsOnlyExit && AddRec->hasNoSelfWrap() && |
0 |
| 10377 |
loopHasNoAbnormalExits(AddRec->getLoop())) { |
0 |
10377 |
loopHasNoAbnormalExits(AddRec->getLoop())) { |
0 |
| 10378 |
const SCEV *Exact = |
--- |
10378 |
const SCEV *Exact = |
--- |
| 10379 |
getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step); |
0 |
10379 |
getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step); |
0 |
| 10380 |
const SCEV *ConstantMax = getCouldNotCompute(); |
0 |
10380 |
const SCEV *ConstantMax = getCouldNotCompute(); |
0 |
| 10381 |
if (Exact != getCouldNotCompute()) { |
0 |
10381 |
if (Exact != getCouldNotCompute()) { |
0 |
| 10382 |
APInt MaxInt = getUnsignedRangeMax(applyLoopGuards(Exact, L)); |
0 |
10382 |
APInt MaxInt = getUnsignedRangeMax(applyLoopGuards(Exact, L)); |
0 |
| 10383 |
ConstantMax = |
--- |
10383 |
ConstantMax = |
--- |
| 10384 |
getConstant(APIntOps::umin(MaxInt, getUnsignedRangeMax(Exact))); |
0 |
10384 |
getConstant(APIntOps::umin(MaxInt, getUnsignedRangeMax(Exact))); |
0 |
| 10385 |
} |
0 |
10385 |
} |
0 |
| 10386 |
const SCEV *SymbolicMax = |
--- |
10386 |
const SCEV *SymbolicMax = |
--- |
| 10387 |
isa(Exact) ? ConstantMax : Exact; |
0 |
10387 |
isa(Exact) ? ConstantMax : Exact; |
0 |
| 10388 |
return ExitLimit(Exact, ConstantMax, SymbolicMax, false, Predicates); |
0 |
10388 |
return ExitLimit(Exact, ConstantMax, SymbolicMax, false, Predicates); |
0 |
| 10389 |
} |
--- |
10389 |
} |
--- |
| 10390 |
|
--- |
10390 |
|
--- |
| 10391 |
// Solve the general equation. |
--- |
10391 |
// Solve the general equation. |
--- |
| 10392 |
const SCEV *E = SolveLinEquationWithOverflow(StepC->getAPInt(), |
0 |
10392 |
const SCEV *E = SolveLinEquationWithOverflow(StepC->getAPInt(), |
0 |
| 10393 |
getNegativeSCEV(Start), *this); |
0 |
10393 |
getNegativeSCEV(Start), *this); |
0 |
| 10394 |
|
--- |
10394 |
|
--- |
| 10395 |
const SCEV *M = E; |
0 |
10395 |
const SCEV *M = E; |
0 |
| 10396 |
if (E != getCouldNotCompute()) { |
0 |
10396 |
if (E != getCouldNotCompute()) { |
0 |
| 10397 |
APInt MaxWithGuards = getUnsignedRangeMax(applyLoopGuards(E, L)); |
0 |
10397 |
APInt MaxWithGuards = getUnsignedRangeMax(applyLoopGuards(E, L)); |
0 |
| 10398 |
M = getConstant(APIntOps::umin(MaxWithGuards, getUnsignedRangeMax(E))); |
0 |
10398 |
M = getConstant(APIntOps::umin(MaxWithGuards, getUnsignedRangeMax(E))); |
0 |
| 10399 |
} |
0 |
10399 |
} |
0 |
| 10400 |
auto *S = isa(E) ? M : E; |
0 |
10400 |
auto *S = isa(E) ? M : E; |
0 |
| 10401 |
return ExitLimit(E, M, S, false, Predicates); |
0 |
10401 |
return ExitLimit(E, M, S, false, Predicates); |
0 |
| 10402 |
} |
0 |
10402 |
} |
0 |
| 10403 |
|
--- |
10403 |
|
--- |
| 10404 |
ScalarEvolution::ExitLimit |
--- |
10404 |
ScalarEvolution::ExitLimit |
--- |
| 10405 |
ScalarEvolution::howFarToNonZero(const SCEV *V, const Loop *L) { |
0 |
10405 |
ScalarEvolution::howFarToNonZero(const SCEV *V, const Loop *L) { |
0 |
| 10406 |
// Loops that look like: while (X == 0) are very strange indeed. We don't |
--- |
10406 |
// Loops that look like: while (X == 0) are very strange indeed. We don't |
--- |
| 10407 |
// handle them yet except for the trivial case. This could be expanded in the |
--- |
10407 |
// handle them yet except for the trivial case. This could be expanded in the |
--- |
| 10408 |
// future as needed. |
--- |
10408 |
// future as needed. |
--- |
| 10409 |
|
--- |
10409 |
|
--- |
| 10410 |
// If the value is a constant, check to see if it is known to be non-zero |
--- |
10410 |
// If the value is a constant, check to see if it is known to be non-zero |
--- |
| 10411 |
// already. If so, the backedge will execute zero times. |
--- |
10411 |
// already. If so, the backedge will execute zero times. |
--- |
| 10412 |
if (const SCEVConstant *C = dyn_cast(V)) { |
0 |
10412 |
if (const SCEVConstant *C = dyn_cast(V)) { |
0 |
| 10413 |
if (!C->getValue()->isZero()) |
0 |
10413 |
if (!C->getValue()->isZero()) |
0 |
| 10414 |
return getZero(C->getType()); |
0 |
10414 |
return getZero(C->getType()); |
0 |
| 10415 |
return getCouldNotCompute(); // Otherwise it will loop infinitely. |
0 |
10415 |
return getCouldNotCompute(); // Otherwise it will loop infinitely. |
0 |
| 10416 |
} |
--- |
10416 |
} |
--- |
| 10417 |
|
--- |
10417 |
|
--- |
| 10418 |
// We could implement others, but I really doubt anyone writes loops like |
--- |
10418 |
// We could implement others, but I really doubt anyone writes loops like |
--- |
| 10419 |
// this, and if they did, they would already be constant folded. |
--- |
10419 |
// this, and if they did, they would already be constant folded. |
--- |
| 10420 |
return getCouldNotCompute(); |
0 |
10420 |
return getCouldNotCompute(); |
0 |
| 10421 |
} |
--- |
10421 |
} |
--- |
| 10422 |
|
--- |
10422 |
|
--- |
| 10423 |
std::pair |
--- |
10423 |
std::pair |
--- |
| 10424 |
ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(const BasicBlock *BB) |
0 |
10424 |
ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(const BasicBlock *BB) |
0 |
| 10425 |
const { |
--- |
10425 |
const { |
--- |
| 10426 |
// If the block has a unique predecessor, then there is no path from the |
--- |
10426 |
// If the block has a unique predecessor, then there is no path from the |
--- |
| 10427 |
// predecessor to the block that does not go through the direct edge |
--- |
10427 |
// predecessor to the block that does not go through the direct edge |
--- |
| 10428 |
// from the predecessor to the block. |
--- |
10428 |
// from the predecessor to the block. |
--- |
| 10429 |
if (const BasicBlock *Pred = BB->getSinglePredecessor()) |
0 |
10429 |
if (const BasicBlock *Pred = BB->getSinglePredecessor()) |
0 |
| 10430 |
return {Pred, BB}; |
0 |
10430 |
return {Pred, BB}; |
0 |
| 10431 |
|
--- |
10431 |
|
--- |
| 10432 |
// A loop's header is defined to be a block that dominates the loop. |
--- |
10432 |
// A loop's header is defined to be a block that dominates the loop. |
--- |
| 10433 |
// If the header has a unique predecessor outside the loop, it must be |
--- |
10433 |
// If the header has a unique predecessor outside the loop, it must be |
--- |
| 10434 |
// a block that has exactly one successor that can reach the loop. |
--- |
10434 |
// a block that has exactly one successor that can reach the loop. |
--- |
| 10435 |
if (const Loop *L = LI.getLoopFor(BB)) |
0 |
10435 |
if (const Loop *L = LI.getLoopFor(BB)) |
0 |
| 10436 |
return {L->getLoopPredecessor(), L->getHeader()}; |
0 |
10436 |
return {L->getLoopPredecessor(), L->getHeader()}; |
0 |
| 10437 |
|
--- |
10437 |
|
--- |
| 10438 |
return {nullptr, nullptr}; |
0 |
10438 |
return {nullptr, nullptr}; |
0 |
| 10439 |
} |
--- |
10439 |
} |
--- |
| 10440 |
|
--- |
10440 |
|
--- |
| 10441 |
/// SCEV structural equivalence is usually sufficient for testing whether two |
--- |
10441 |
/// SCEV structural equivalence is usually sufficient for testing whether two |
--- |
| 10442 |
/// expressions are equal, however for the purposes of looking for a condition |
--- |
10442 |
/// expressions are equal, however for the purposes of looking for a condition |
--- |
| 10443 |
/// guarding a loop, it can be useful to be a little more general, since a |
--- |
10443 |
/// guarding a loop, it can be useful to be a little more general, since a |
--- |
| 10444 |
/// front-end may have replicated the controlling expression. |
--- |
10444 |
/// front-end may have replicated the controlling expression. |
--- |
| 10445 |
static bool HasSameValue(const SCEV *A, const SCEV *B) { |
0 |
10445 |
static bool HasSameValue(const SCEV *A, const SCEV *B) { |
0 |
| 10446 |
// Quick check to see if they are the same SCEV. |
--- |
10446 |
// Quick check to see if they are the same SCEV. |
--- |
| 10447 |
if (A == B) return true; |
0 |
10447 |
if (A == B) return true; |
0 |
| 10448 |
|
--- |
10448 |
|
--- |
| 10449 |
auto ComputesEqualValues = [](const Instruction *A, const Instruction *B) { |
0 |
10449 |
auto ComputesEqualValues = [](const Instruction *A, const Instruction *B) { |
0 |
| 10450 |
// Not all instructions that are "identical" compute the same value. For |
--- |
10450 |
// Not all instructions that are "identical" compute the same value. For |
--- |
| 10451 |
// instance, two distinct alloca instructions allocating the same type are |
--- |
10451 |
// instance, two distinct alloca instructions allocating the same type are |
--- |
| 10452 |
// identical and do not read memory; but compute distinct values. |
--- |
10452 |
// identical and do not read memory; but compute distinct values. |
--- |
| 10453 |
return A->isIdenticalTo(B) && (isa(A) || isa(A)); |
0 |
10453 |
return A->isIdenticalTo(B) && (isa(A) || isa(A)); |
0 |
| 10454 |
}; |
--- |
10454 |
}; |
--- |
| 10455 |
|
--- |
10455 |
|
--- |
| 10456 |
// Otherwise, if they're both SCEVUnknown, it's possible that they hold |
--- |
10456 |
// Otherwise, if they're both SCEVUnknown, it's possible that they hold |
--- |
| 10457 |
// two different instructions with the same value. Check for this case. |
--- |
10457 |
// two different instructions with the same value. Check for this case. |
--- |
| 10458 |
if (const SCEVUnknown *AU = dyn_cast(A)) |
0 |
10458 |
if (const SCEVUnknown *AU = dyn_cast(A)) |
0 |
| 10459 |
if (const SCEVUnknown *BU = dyn_cast(B)) |
0 |
10459 |
if (const SCEVUnknown *BU = dyn_cast(B)) |
0 |
| 10460 |
if (const Instruction *AI = dyn_cast(AU->getValue())) |
0 |
10460 |
if (const Instruction *AI = dyn_cast(AU->getValue())) |
0 |
| 10461 |
if (const Instruction *BI = dyn_cast(BU->getValue())) |
0 |
10461 |
if (const Instruction *BI = dyn_cast(BU->getValue())) |
0 |
| 10462 |
if (ComputesEqualValues(AI, BI)) |
0 |
10462 |
if (ComputesEqualValues(AI, BI)) |
0 |
| 10463 |
return true; |
0 |
10463 |
return true; |
0 |
| 10464 |
|
--- |
10464 |
|
--- |
| 10465 |
// Otherwise assume they may have a different value. |
--- |
10465 |
// Otherwise assume they may have a different value. |
--- |
| 10466 |
return false; |
0 |
10466 |
return false; |
0 |
| 10467 |
} |
--- |
10467 |
} |
--- |
| 10468 |
|
--- |
10468 |
|
--- |
| 10469 |
bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred, |
0 |
10469 |
bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred, |
0 |
| 10470 |
const SCEV *&LHS, const SCEV *&RHS, |
--- |
10470 |
const SCEV *&LHS, const SCEV *&RHS, |
--- |
| 10471 |
unsigned Depth) { |
--- |
10471 |
unsigned Depth) { |
--- |
| 10472 |
bool Changed = false; |
0 |
10472 |
bool Changed = false; |
0 |
| 10473 |
// Simplifies ICMP to trivial true or false by turning it into '0 == 0' or |
--- |
10473 |
// Simplifies ICMP to trivial true or false by turning it into '0 == 0' or |
--- |
| 10474 |
// '0 != 0'. |
--- |
10474 |
// '0 != 0'. |
--- |
| 10475 |
auto TrivialCase = [&](bool TriviallyTrue) { |
0 |
10475 |
auto TrivialCase = [&](bool TriviallyTrue) { |
0 |
| 10476 |
LHS = RHS = getConstant(ConstantInt::getFalse(getContext())); |
0 |
10476 |
LHS = RHS = getConstant(ConstantInt::getFalse(getContext())); |
0 |
| 10477 |
Pred = TriviallyTrue ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE; |
0 |
10477 |
Pred = TriviallyTrue ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE; |
0 |
| 10478 |
return true; |
0 |
10478 |
return true; |
0 |
| 10479 |
}; |
0 |
10479 |
}; |
0 |
| 10480 |
// If we hit the max recursion limit bail out. |
--- |
10480 |
// If we hit the max recursion limit bail out. |
--- |
| 10481 |
if (Depth >= 3) |
0 |
10481 |
if (Depth >= 3) |
0 |
| 10482 |
return false; |
0 |
10482 |
return false; |
0 |
| 10483 |
|
--- |
10483 |
|
--- |
| 10484 |
// Canonicalize a constant to the right side. |
--- |
10484 |
// Canonicalize a constant to the right side. |
--- |
| 10485 |
if (const SCEVConstant *LHSC = dyn_cast(LHS)) { |
0 |
10485 |
if (const SCEVConstant *LHSC = dyn_cast(LHS)) { |
0 |
| 10486 |
// Check for both operands constant. |
--- |
10486 |
// Check for both operands constant. |
--- |
| 10487 |
if (const SCEVConstant *RHSC = dyn_cast(RHS)) { |
0 |
10487 |
if (const SCEVConstant *RHSC = dyn_cast(RHS)) { |
0 |
| 10488 |
if (ConstantExpr::getICmp(Pred, |
0 |
10488 |
if (ConstantExpr::getICmp(Pred, |
0 |
| 10489 |
LHSC->getValue(), |
0 |
10489 |
LHSC->getValue(), |
0 |
| 10490 |
RHSC->getValue())->isNullValue()) |
0 |
10490 |
RHSC->getValue())->isNullValue()) |
0 |
| 10491 |
return TrivialCase(false); |
0 |
10491 |
return TrivialCase(false); |
0 |
| 10492 |
return TrivialCase(true); |
0 |
10492 |
return TrivialCase(true); |
0 |
| 10493 |
} |
--- |
10493 |
} |
--- |
| 10494 |
// Otherwise swap the operands to put the constant on the right. |
--- |
10494 |
// Otherwise swap the operands to put the constant on the right. |
--- |
| 10495 |
std::swap(LHS, RHS); |
0 |
10495 |
std::swap(LHS, RHS); |
0 |
| 10496 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
10496 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
| 10497 |
Changed = true; |
0 |
10497 |
Changed = true; |
0 |
| 10498 |
} |
--- |
10498 |
} |
--- |
| 10499 |
|
--- |
10499 |
|
--- |
| 10500 |
// If we're comparing an addrec with a value which is loop-invariant in the |
--- |
10500 |
// If we're comparing an addrec with a value which is loop-invariant in the |
--- |
| 10501 |
// addrec's loop, put the addrec on the left. Also make a dominance check, |
--- |
10501 |
// addrec's loop, put the addrec on the left. Also make a dominance check, |
--- |
| 10502 |
// as both operands could be addrecs loop-invariant in each other's loop. |
--- |
10502 |
// as both operands could be addrecs loop-invariant in each other's loop. |
--- |
| 10503 |
if (const SCEVAddRecExpr *AR = dyn_cast(RHS)) { |
0 |
10503 |
if (const SCEVAddRecExpr *AR = dyn_cast(RHS)) { |
0 |
| 10504 |
const Loop *L = AR->getLoop(); |
0 |
10504 |
const Loop *L = AR->getLoop(); |
0 |
| 10505 |
if (isLoopInvariant(LHS, L) && properlyDominates(LHS, L->getHeader())) { |
0 |
10505 |
if (isLoopInvariant(LHS, L) && properlyDominates(LHS, L->getHeader())) { |
0 |
| 10506 |
std::swap(LHS, RHS); |
0 |
10506 |
std::swap(LHS, RHS); |
0 |
| 10507 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
10507 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
| 10508 |
Changed = true; |
0 |
10508 |
Changed = true; |
0 |
| 10509 |
} |
--- |
10509 |
} |
--- |
| 10510 |
} |
--- |
10510 |
} |
--- |
| 10511 |
|
--- |
10511 |
|
--- |
| 10512 |
// If there's a constant operand, canonicalize comparisons with boundary |
--- |
10512 |
// If there's a constant operand, canonicalize comparisons with boundary |
--- |
| 10513 |
// cases, and canonicalize *-or-equal comparisons to regular comparisons. |
--- |
10513 |
// cases, and canonicalize *-or-equal comparisons to regular comparisons. |
--- |
| 10514 |
if (const SCEVConstant *RC = dyn_cast(RHS)) { |
0 |
10514 |
if (const SCEVConstant *RC = dyn_cast(RHS)) { |
0 |
| 10515 |
const APInt &RA = RC->getAPInt(); |
0 |
10515 |
const APInt &RA = RC->getAPInt(); |
0 |
| 10516 |
|
--- |
10516 |
|
--- |
| 10517 |
bool SimplifiedByConstantRange = false; |
0 |
10517 |
bool SimplifiedByConstantRange = false; |
0 |
| 10518 |
|
--- |
10518 |
|
--- |
| 10519 |
if (!ICmpInst::isEquality(Pred)) { |
0 |
10519 |
if (!ICmpInst::isEquality(Pred)) { |
0 |
| 10520 |
ConstantRange ExactCR = ConstantRange::makeExactICmpRegion(Pred, RA); |
0 |
10520 |
ConstantRange ExactCR = ConstantRange::makeExactICmpRegion(Pred, RA); |
0 |
| 10521 |
if (ExactCR.isFullSet()) |
0 |
10521 |
if (ExactCR.isFullSet()) |
0 |
| 10522 |
return TrivialCase(true); |
0 |
10522 |
return TrivialCase(true); |
0 |
| 10523 |
if (ExactCR.isEmptySet()) |
0 |
10523 |
if (ExactCR.isEmptySet()) |
0 |
| 10524 |
return TrivialCase(false); |
0 |
10524 |
return TrivialCase(false); |
0 |
| 10525 |
|
--- |
10525 |
|
--- |
| 10526 |
APInt NewRHS; |
0 |
10526 |
APInt NewRHS; |
0 |
| 10527 |
CmpInst::Predicate NewPred; |
--- |
10527 |
CmpInst::Predicate NewPred; |
--- |
| 10528 |
if (ExactCR.getEquivalentICmp(NewPred, NewRHS) && |
0 |
10528 |
if (ExactCR.getEquivalentICmp(NewPred, NewRHS) && |
0 |
| 10529 |
ICmpInst::isEquality(NewPred)) { |
0 |
10529 |
ICmpInst::isEquality(NewPred)) { |
0 |
| 10530 |
// We were able to convert an inequality to an equality. |
--- |
10530 |
// We were able to convert an inequality to an equality. |
--- |
| 10531 |
Pred = NewPred; |
0 |
10531 |
Pred = NewPred; |
0 |
| 10532 |
RHS = getConstant(NewRHS); |
0 |
10532 |
RHS = getConstant(NewRHS); |
0 |
| 10533 |
Changed = SimplifiedByConstantRange = true; |
0 |
10533 |
Changed = SimplifiedByConstantRange = true; |
0 |
| 10534 |
} |
--- |
10534 |
} |
--- |
| 10535 |
} |
0 |
10535 |
} |
0 |
| 10536 |
|
--- |
10536 |
|
--- |
| 10537 |
if (!SimplifiedByConstantRange) { |
0 |
10537 |
if (!SimplifiedByConstantRange) { |
0 |
| 10538 |
switch (Pred) { |
0 |
10538 |
switch (Pred) { |
0 |
| 10539 |
default: |
0 |
10539 |
default: |
0 |
| 10540 |
break; |
0 |
10540 |
break; |
0 |
| 10541 |
case ICmpInst::ICMP_EQ: |
0 |
10541 |
case ICmpInst::ICMP_EQ: |
0 |
| 10542 |
case ICmpInst::ICMP_NE: |
--- |
10542 |
case ICmpInst::ICMP_NE: |
--- |
| 10543 |
// Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b. |
--- |
10543 |
// Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b. |
--- |
| 10544 |
if (!RA) |
0 |
10544 |
if (!RA) |
0 |
| 10545 |
if (const SCEVAddExpr *AE = dyn_cast(LHS)) |
0 |
10545 |
if (const SCEVAddExpr *AE = dyn_cast(LHS)) |
0 |
| 10546 |
if (const SCEVMulExpr *ME = |
0 |
10546 |
if (const SCEVMulExpr *ME = |
0 |
| 10547 |
dyn_cast(AE->getOperand(0))) |
0 |
10547 |
dyn_cast(AE->getOperand(0))) |
0 |
| 10548 |
if (AE->getNumOperands() == 2 && ME->getNumOperands() == 2 && |
0 |
10548 |
if (AE->getNumOperands() == 2 && ME->getNumOperands() == 2 && |
0 |
| 10549 |
ME->getOperand(0)->isAllOnesValue()) { |
0 |
10549 |
ME->getOperand(0)->isAllOnesValue()) { |
0 |
| 10550 |
RHS = AE->getOperand(1); |
0 |
10550 |
RHS = AE->getOperand(1); |
0 |
| 10551 |
LHS = ME->getOperand(1); |
0 |
10551 |
LHS = ME->getOperand(1); |
0 |
| 10552 |
Changed = true; |
0 |
10552 |
Changed = true; |
0 |
| 10553 |
} |
--- |
10553 |
} |
--- |
| 10554 |
break; |
0 |
10554 |
break; |
0 |
| 10555 |
|
--- |
10555 |
|
--- |
| 10556 |
|
--- |
10556 |
|
--- |
| 10557 |
// The "Should have been caught earlier!" messages refer to the fact |
--- |
10557 |
// The "Should have been caught earlier!" messages refer to the fact |
--- |
| 10558 |
// that the ExactCR.isFullSet() or ExactCR.isEmptySet() check above |
--- |
10558 |
// that the ExactCR.isFullSet() or ExactCR.isEmptySet() check above |
--- |
| 10559 |
// should have fired on the corresponding cases, and canonicalized the |
--- |
10559 |
// should have fired on the corresponding cases, and canonicalized the |
--- |
| 10560 |
// check to trivial case. |
--- |
10560 |
// check to trivial case. |
--- |
| 10561 |
|
--- |
10561 |
|
--- |
| 10562 |
case ICmpInst::ICMP_UGE: |
0 |
10562 |
case ICmpInst::ICMP_UGE: |
0 |
| 10563 |
assert(!RA.isMinValue() && "Should have been caught earlier!"); |
0 |
10563 |
assert(!RA.isMinValue() && "Should have been caught earlier!"); |
0 |
| 10564 |
Pred = ICmpInst::ICMP_UGT; |
0 |
10564 |
Pred = ICmpInst::ICMP_UGT; |
0 |
| 10565 |
RHS = getConstant(RA - 1); |
0 |
10565 |
RHS = getConstant(RA - 1); |
0 |
| 10566 |
Changed = true; |
0 |
10566 |
Changed = true; |
0 |
| 10567 |
break; |
0 |
10567 |
break; |
0 |
| 10568 |
case ICmpInst::ICMP_ULE: |
0 |
10568 |
case ICmpInst::ICMP_ULE: |
0 |
| 10569 |
assert(!RA.isMaxValue() && "Should have been caught earlier!"); |
0 |
10569 |
assert(!RA.isMaxValue() && "Should have been caught earlier!"); |
0 |
| 10570 |
Pred = ICmpInst::ICMP_ULT; |
0 |
10570 |
Pred = ICmpInst::ICMP_ULT; |
0 |
| 10571 |
RHS = getConstant(RA + 1); |
0 |
10571 |
RHS = getConstant(RA + 1); |
0 |
| 10572 |
Changed = true; |
0 |
10572 |
Changed = true; |
0 |
| 10573 |
break; |
0 |
10573 |
break; |
0 |
| 10574 |
case ICmpInst::ICMP_SGE: |
0 |
10574 |
case ICmpInst::ICMP_SGE: |
0 |
| 10575 |
assert(!RA.isMinSignedValue() && "Should have been caught earlier!"); |
0 |
10575 |
assert(!RA.isMinSignedValue() && "Should have been caught earlier!"); |
0 |
| 10576 |
Pred = ICmpInst::ICMP_SGT; |
0 |
10576 |
Pred = ICmpInst::ICMP_SGT; |
0 |
| 10577 |
RHS = getConstant(RA - 1); |
0 |
10577 |
RHS = getConstant(RA - 1); |
0 |
| 10578 |
Changed = true; |
0 |
10578 |
Changed = true; |
0 |
| 10579 |
break; |
0 |
10579 |
break; |
0 |
| 10580 |
case ICmpInst::ICMP_SLE: |
0 |
10580 |
case ICmpInst::ICMP_SLE: |
0 |
| 10581 |
assert(!RA.isMaxSignedValue() && "Should have been caught earlier!"); |
0 |
10581 |
assert(!RA.isMaxSignedValue() && "Should have been caught earlier!"); |
0 |
| 10582 |
Pred = ICmpInst::ICMP_SLT; |
0 |
10582 |
Pred = ICmpInst::ICMP_SLT; |
0 |
| 10583 |
RHS = getConstant(RA + 1); |
0 |
10583 |
RHS = getConstant(RA + 1); |
0 |
| 10584 |
Changed = true; |
0 |
10584 |
Changed = true; |
0 |
| 10585 |
break; |
0 |
10585 |
break; |
0 |
| 10586 |
} |
--- |
10586 |
} |
--- |
| 10587 |
} |
--- |
10587 |
} |
--- |
| 10588 |
} |
--- |
10588 |
} |
--- |
| 10589 |
|
--- |
10589 |
|
--- |
| 10590 |
// Check for obvious equality. |
--- |
10590 |
// Check for obvious equality. |
--- |
| 10591 |
if (HasSameValue(LHS, RHS)) { |
0 |
10591 |
if (HasSameValue(LHS, RHS)) { |
0 |
| 10592 |
if (ICmpInst::isTrueWhenEqual(Pred)) |
0 |
10592 |
if (ICmpInst::isTrueWhenEqual(Pred)) |
0 |
| 10593 |
return TrivialCase(true); |
0 |
10593 |
return TrivialCase(true); |
0 |
| 10594 |
if (ICmpInst::isFalseWhenEqual(Pred)) |
0 |
10594 |
if (ICmpInst::isFalseWhenEqual(Pred)) |
0 |
| 10595 |
return TrivialCase(false); |
0 |
10595 |
return TrivialCase(false); |
0 |
| 10596 |
} |
--- |
10596 |
} |
--- |
| 10597 |
|
--- |
10597 |
|
--- |
| 10598 |
// If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by |
--- |
10598 |
// If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by |
--- |
| 10599 |
// adding or subtracting 1 from one of the operands. |
--- |
10599 |
// adding or subtracting 1 from one of the operands. |
--- |
| 10600 |
switch (Pred) { |
0 |
10600 |
switch (Pred) { |
0 |
| 10601 |
case ICmpInst::ICMP_SLE: |
0 |
10601 |
case ICmpInst::ICMP_SLE: |
0 |
| 10602 |
if (!getSignedRangeMax(RHS).isMaxSignedValue()) { |
0 |
10602 |
if (!getSignedRangeMax(RHS).isMaxSignedValue()) { |
0 |
| 10603 |
RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS, |
0 |
10603 |
RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS, |
0 |
| 10604 |
SCEV::FlagNSW); |
--- |
10604 |
SCEV::FlagNSW); |
--- |
| 10605 |
Pred = ICmpInst::ICMP_SLT; |
0 |
10605 |
Pred = ICmpInst::ICMP_SLT; |
0 |
| 10606 |
Changed = true; |
0 |
10606 |
Changed = true; |
0 |
| 10607 |
} else if (!getSignedRangeMin(LHS).isMinSignedValue()) { |
0 |
10607 |
} else if (!getSignedRangeMin(LHS).isMinSignedValue()) { |
0 |
| 10608 |
LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS, |
0 |
10608 |
LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS, |
0 |
| 10609 |
SCEV::FlagNSW); |
--- |
10609 |
SCEV::FlagNSW); |
--- |
| 10610 |
Pred = ICmpInst::ICMP_SLT; |
0 |
10610 |
Pred = ICmpInst::ICMP_SLT; |
0 |
| 10611 |
Changed = true; |
0 |
10611 |
Changed = true; |
0 |
| 10612 |
} |
--- |
10612 |
} |
--- |
| 10613 |
break; |
0 |
10613 |
break; |
0 |
| 10614 |
case ICmpInst::ICMP_SGE: |
0 |
10614 |
case ICmpInst::ICMP_SGE: |
0 |
| 10615 |
if (!getSignedRangeMin(RHS).isMinSignedValue()) { |
0 |
10615 |
if (!getSignedRangeMin(RHS).isMinSignedValue()) { |
0 |
| 10616 |
RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS, |
0 |
10616 |
RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS, |
0 |
| 10617 |
SCEV::FlagNSW); |
--- |
10617 |
SCEV::FlagNSW); |
--- |
| 10618 |
Pred = ICmpInst::ICMP_SGT; |
0 |
10618 |
Pred = ICmpInst::ICMP_SGT; |
0 |
| 10619 |
Changed = true; |
0 |
10619 |
Changed = true; |
0 |
| 10620 |
} else if (!getSignedRangeMax(LHS).isMaxSignedValue()) { |
0 |
10620 |
} else if (!getSignedRangeMax(LHS).isMaxSignedValue()) { |
0 |
| 10621 |
LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS, |
0 |
10621 |
LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS, |
0 |
| 10622 |
SCEV::FlagNSW); |
--- |
10622 |
SCEV::FlagNSW); |
--- |
| 10623 |
Pred = ICmpInst::ICMP_SGT; |
0 |
10623 |
Pred = ICmpInst::ICMP_SGT; |
0 |
| 10624 |
Changed = true; |
0 |
10624 |
Changed = true; |
0 |
| 10625 |
} |
--- |
10625 |
} |
--- |
| 10626 |
break; |
0 |
10626 |
break; |
0 |
| 10627 |
case ICmpInst::ICMP_ULE: |
0 |
10627 |
case ICmpInst::ICMP_ULE: |
0 |
| 10628 |
if (!getUnsignedRangeMax(RHS).isMaxValue()) { |
0 |
10628 |
if (!getUnsignedRangeMax(RHS).isMaxValue()) { |
0 |
| 10629 |
RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS, |
0 |
10629 |
RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS, |
0 |
| 10630 |
SCEV::FlagNUW); |
--- |
10630 |
SCEV::FlagNUW); |
--- |
| 10631 |
Pred = ICmpInst::ICMP_ULT; |
0 |
10631 |
Pred = ICmpInst::ICMP_ULT; |
0 |
| 10632 |
Changed = true; |
0 |
10632 |
Changed = true; |
0 |
| 10633 |
} else if (!getUnsignedRangeMin(LHS).isMinValue()) { |
0 |
10633 |
} else if (!getUnsignedRangeMin(LHS).isMinValue()) { |
0 |
| 10634 |
LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS); |
0 |
10634 |
LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS); |
0 |
| 10635 |
Pred = ICmpInst::ICMP_ULT; |
0 |
10635 |
Pred = ICmpInst::ICMP_ULT; |
0 |
| 10636 |
Changed = true; |
0 |
10636 |
Changed = true; |
0 |
| 10637 |
} |
--- |
10637 |
} |
--- |
| 10638 |
break; |
0 |
10638 |
break; |
0 |
| 10639 |
case ICmpInst::ICMP_UGE: |
0 |
10639 |
case ICmpInst::ICMP_UGE: |
0 |
| 10640 |
if (!getUnsignedRangeMin(RHS).isMinValue()) { |
0 |
10640 |
if (!getUnsignedRangeMin(RHS).isMinValue()) { |
0 |
| 10641 |
RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS); |
0 |
10641 |
RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS); |
0 |
| 10642 |
Pred = ICmpInst::ICMP_UGT; |
0 |
10642 |
Pred = ICmpInst::ICMP_UGT; |
0 |
| 10643 |
Changed = true; |
0 |
10643 |
Changed = true; |
0 |
| 10644 |
} else if (!getUnsignedRangeMax(LHS).isMaxValue()) { |
0 |
10644 |
} else if (!getUnsignedRangeMax(LHS).isMaxValue()) { |
0 |
| 10645 |
LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS, |
0 |
10645 |
LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS, |
0 |
| 10646 |
SCEV::FlagNUW); |
--- |
10646 |
SCEV::FlagNUW); |
--- |
| 10647 |
Pred = ICmpInst::ICMP_UGT; |
0 |
10647 |
Pred = ICmpInst::ICMP_UGT; |
0 |
| 10648 |
Changed = true; |
0 |
10648 |
Changed = true; |
0 |
| 10649 |
} |
--- |
10649 |
} |
--- |
| 10650 |
break; |
0 |
10650 |
break; |
0 |
| 10651 |
default: |
0 |
10651 |
default: |
0 |
| 10652 |
break; |
0 |
10652 |
break; |
0 |
| 10653 |
} |
--- |
10653 |
} |
--- |
| 10654 |
|
--- |
10654 |
|
--- |
| 10655 |
// TODO: More simplifications are possible here. |
--- |
10655 |
// TODO: More simplifications are possible here. |
--- |
| 10656 |
|
--- |
10656 |
|
--- |
| 10657 |
// Recursively simplify until we either hit a recursion limit or nothing |
--- |
10657 |
// Recursively simplify until we either hit a recursion limit or nothing |
--- |
| 10658 |
// changes. |
--- |
10658 |
// changes. |
--- |
| 10659 |
if (Changed) |
0 |
10659 |
if (Changed) |
0 |
| 10660 |
return SimplifyICmpOperands(Pred, LHS, RHS, Depth + 1); |
0 |
10660 |
return SimplifyICmpOperands(Pred, LHS, RHS, Depth + 1); |
0 |
| 10661 |
|
--- |
10661 |
|
--- |
| 10662 |
return Changed; |
0 |
10662 |
return Changed; |
0 |
| 10663 |
} |
--- |
10663 |
} |
--- |
| 10664 |
|
--- |
10664 |
|
--- |
| 10665 |
bool ScalarEvolution::isKnownNegative(const SCEV *S) { |
0 |
10665 |
bool ScalarEvolution::isKnownNegative(const SCEV *S) { |
0 |
| 10666 |
return getSignedRangeMax(S).isNegative(); |
0 |
10666 |
return getSignedRangeMax(S).isNegative(); |
0 |
| 10667 |
} |
--- |
10667 |
} |
--- |
| 10668 |
|
--- |
10668 |
|
--- |
| 10669 |
bool ScalarEvolution::isKnownPositive(const SCEV *S) { |
0 |
10669 |
bool ScalarEvolution::isKnownPositive(const SCEV *S) { |
0 |
| 10670 |
return getSignedRangeMin(S).isStrictlyPositive(); |
0 |
10670 |
return getSignedRangeMin(S).isStrictlyPositive(); |
0 |
| 10671 |
} |
--- |
10671 |
} |
--- |
| 10672 |
|
--- |
10672 |
|
--- |
| 10673 |
bool ScalarEvolution::isKnownNonNegative(const SCEV *S) { |
0 |
10673 |
bool ScalarEvolution::isKnownNonNegative(const SCEV *S) { |
0 |
| 10674 |
return !getSignedRangeMin(S).isNegative(); |
0 |
10674 |
return !getSignedRangeMin(S).isNegative(); |
0 |
| 10675 |
} |
--- |
10675 |
} |
--- |
| 10676 |
|
--- |
10676 |
|
--- |
| 10677 |
bool ScalarEvolution::isKnownNonPositive(const SCEV *S) { |
0 |
10677 |
bool ScalarEvolution::isKnownNonPositive(const SCEV *S) { |
0 |
| 10678 |
return !getSignedRangeMax(S).isStrictlyPositive(); |
0 |
10678 |
return !getSignedRangeMax(S).isStrictlyPositive(); |
0 |
| 10679 |
} |
--- |
10679 |
} |
--- |
| 10680 |
|
--- |
10680 |
|
--- |
| 10681 |
bool ScalarEvolution::isKnownNonZero(const SCEV *S) { |
0 |
10681 |
bool ScalarEvolution::isKnownNonZero(const SCEV *S) { |
0 |
| 10682 |
return getUnsignedRangeMin(S) != 0; |
0 |
10682 |
return getUnsignedRangeMin(S) != 0; |
0 |
| 10683 |
} |
--- |
10683 |
} |
--- |
| 10684 |
|
--- |
10684 |
|
--- |
| 10685 |
std::pair |
--- |
10685 |
std::pair |
--- |
| 10686 |
ScalarEvolution::SplitIntoInitAndPostInc(const Loop *L, const SCEV *S) { |
0 |
10686 |
ScalarEvolution::SplitIntoInitAndPostInc(const Loop *L, const SCEV *S) { |
0 |
| 10687 |
// Compute SCEV on entry of loop L. |
--- |
10687 |
// Compute SCEV on entry of loop L. |
--- |
| 10688 |
const SCEV *Start = SCEVInitRewriter::rewrite(S, L, *this); |
0 |
10688 |
const SCEV *Start = SCEVInitRewriter::rewrite(S, L, *this); |
0 |
| 10689 |
if (Start == getCouldNotCompute()) |
0 |
10689 |
if (Start == getCouldNotCompute()) |
0 |
| 10690 |
return { Start, Start }; |
0 |
10690 |
return { Start, Start }; |
0 |
| 10691 |
// Compute post increment SCEV for loop L. |
--- |
10691 |
// Compute post increment SCEV for loop L. |
--- |
| 10692 |
const SCEV *PostInc = SCEVPostIncRewriter::rewrite(S, L, *this); |
0 |
10692 |
const SCEV *PostInc = SCEVPostIncRewriter::rewrite(S, L, *this); |
0 |
| 10693 |
assert(PostInc != getCouldNotCompute() && "Unexpected could not compute"); |
0 |
10693 |
assert(PostInc != getCouldNotCompute() && "Unexpected could not compute"); |
0 |
| 10694 |
return { Start, PostInc }; |
0 |
10694 |
return { Start, PostInc }; |
0 |
| 10695 |
} |
--- |
10695 |
} |
--- |
| 10696 |
|
--- |
10696 |
|
--- |
| 10697 |
bool ScalarEvolution::isKnownViaInduction(ICmpInst::Predicate Pred, |
0 |
10697 |
bool ScalarEvolution::isKnownViaInduction(ICmpInst::Predicate Pred, |
0 |
| 10698 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
10698 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
| 10699 |
// First collect all loops. |
--- |
10699 |
// First collect all loops. |
--- |
| 10700 |
SmallPtrSet LoopsUsed; |
0 |
10700 |
SmallPtrSet LoopsUsed; |
0 |
| 10701 |
getUsedLoops(LHS, LoopsUsed); |
0 |
10701 |
getUsedLoops(LHS, LoopsUsed); |
0 |
| 10702 |
getUsedLoops(RHS, LoopsUsed); |
0 |
10702 |
getUsedLoops(RHS, LoopsUsed); |
0 |
| 10703 |
|
--- |
10703 |
|
--- |
| 10704 |
if (LoopsUsed.empty()) |
0 |
10704 |
if (LoopsUsed.empty()) |
0 |
| 10705 |
return false; |
0 |
10705 |
return false; |
0 |
| 10706 |
|
--- |
10706 |
|
--- |
| 10707 |
// Domination relationship must be a linear order on collected loops. |
--- |
10707 |
// Domination relationship must be a linear order on collected loops. |
--- |
| 10708 |
#ifndef NDEBUG |
--- |
10708 |
#ifndef NDEBUG |
--- |
| 10709 |
for (const auto *L1 : LoopsUsed) |
0 |
10709 |
for (const auto *L1 : LoopsUsed) |
0 |
| 10710 |
for (const auto *L2 : LoopsUsed) |
0 |
10710 |
for (const auto *L2 : LoopsUsed) |
0 |
| 10711 |
assert((DT.dominates(L1->getHeader(), L2->getHeader()) || |
0 |
10711 |
assert((DT.dominates(L1->getHeader(), L2->getHeader()) || |
0 |
| 10712 |
DT.dominates(L2->getHeader(), L1->getHeader())) && |
--- |
10712 |
DT.dominates(L2->getHeader(), L1->getHeader())) && |
--- |
| 10713 |
"Domination relationship is not a linear order"); |
--- |
10713 |
"Domination relationship is not a linear order"); |
--- |
| 10714 |
#endif |
--- |
10714 |
#endif |
--- |
| 10715 |
|
--- |
10715 |
|
--- |
| 10716 |
const Loop *MDL = |
--- |
10716 |
const Loop *MDL = |
--- |
| 10717 |
*std::max_element(LoopsUsed.begin(), LoopsUsed.end(), |
0 |
10717 |
*std::max_element(LoopsUsed.begin(), LoopsUsed.end(), |
0 |
| 10718 |
[&](const Loop *L1, const Loop *L2) { |
0 |
10718 |
[&](const Loop *L1, const Loop *L2) { |
0 |
| 10719 |
return DT.properlyDominates(L1->getHeader(), L2->getHeader()); |
0 |
10719 |
return DT.properlyDominates(L1->getHeader(), L2->getHeader()); |
0 |
| 10720 |
}); |
--- |
10720 |
}); |
--- |
| 10721 |
|
--- |
10721 |
|
--- |
| 10722 |
// Get init and post increment value for LHS. |
--- |
10722 |
// Get init and post increment value for LHS. |
--- |
| 10723 |
auto SplitLHS = SplitIntoInitAndPostInc(MDL, LHS); |
0 |
10723 |
auto SplitLHS = SplitIntoInitAndPostInc(MDL, LHS); |
0 |
| 10724 |
// if LHS contains unknown non-invariant SCEV then bail out. |
--- |
10724 |
// if LHS contains unknown non-invariant SCEV then bail out. |
--- |
| 10725 |
if (SplitLHS.first == getCouldNotCompute()) |
0 |
10725 |
if (SplitLHS.first == getCouldNotCompute()) |
0 |
| 10726 |
return false; |
0 |
10726 |
return false; |
0 |
| 10727 |
assert (SplitLHS.second != getCouldNotCompute() && "Unexpected CNC"); |
0 |
10727 |
assert (SplitLHS.second != getCouldNotCompute() && "Unexpected CNC"); |
0 |
| 10728 |
// Get init and post increment value for RHS. |
--- |
10728 |
// Get init and post increment value for RHS. |
--- |
| 10729 |
auto SplitRHS = SplitIntoInitAndPostInc(MDL, RHS); |
0 |
10729 |
auto SplitRHS = SplitIntoInitAndPostInc(MDL, RHS); |
0 |
| 10730 |
// if RHS contains unknown non-invariant SCEV then bail out. |
--- |
10730 |
// if RHS contains unknown non-invariant SCEV then bail out. |
--- |
| 10731 |
if (SplitRHS.first == getCouldNotCompute()) |
0 |
10731 |
if (SplitRHS.first == getCouldNotCompute()) |
0 |
| 10732 |
return false; |
0 |
10732 |
return false; |
0 |
| 10733 |
assert (SplitRHS.second != getCouldNotCompute() && "Unexpected CNC"); |
0 |
10733 |
assert (SplitRHS.second != getCouldNotCompute() && "Unexpected CNC"); |
0 |
| 10734 |
// It is possible that init SCEV contains an invariant load but it does |
--- |
10734 |
// It is possible that init SCEV contains an invariant load but it does |
--- |
| 10735 |
// not dominate MDL and is not available at MDL loop entry, so we should |
--- |
10735 |
// not dominate MDL and is not available at MDL loop entry, so we should |
--- |
| 10736 |
// check it here. |
--- |
10736 |
// check it here. |
--- |
| 10737 |
if (!isAvailableAtLoopEntry(SplitLHS.first, MDL) || |
0 |
10737 |
if (!isAvailableAtLoopEntry(SplitLHS.first, MDL) || |
0 |
| 10738 |
!isAvailableAtLoopEntry(SplitRHS.first, MDL)) |
0 |
10738 |
!isAvailableAtLoopEntry(SplitRHS.first, MDL)) |
0 |
| 10739 |
return false; |
0 |
10739 |
return false; |
0 |
| 10740 |
|
--- |
10740 |
|
--- |
| 10741 |
// It seems backedge guard check is faster than entry one so in some cases |
--- |
10741 |
// It seems backedge guard check is faster than entry one so in some cases |
--- |
| 10742 |
// it can speed up whole estimation by short circuit |
--- |
10742 |
// it can speed up whole estimation by short circuit |
--- |
| 10743 |
return isLoopBackedgeGuardedByCond(MDL, Pred, SplitLHS.second, |
0 |
10743 |
return isLoopBackedgeGuardedByCond(MDL, Pred, SplitLHS.second, |
0 |
| 10744 |
SplitRHS.second) && |
0 |
10744 |
SplitRHS.second) && |
0 |
| 10745 |
isLoopEntryGuardedByCond(MDL, Pred, SplitLHS.first, SplitRHS.first); |
0 |
10745 |
isLoopEntryGuardedByCond(MDL, Pred, SplitLHS.first, SplitRHS.first); |
0 |
| 10746 |
} |
0 |
10746 |
} |
0 |
| 10747 |
|
--- |
10747 |
|
--- |
| 10748 |
bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred, |
0 |
10748 |
bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred, |
0 |
| 10749 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
10749 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
| 10750 |
// Canonicalize the inputs first. |
--- |
10750 |
// Canonicalize the inputs first. |
--- |
| 10751 |
(void)SimplifyICmpOperands(Pred, LHS, RHS); |
0 |
10751 |
(void)SimplifyICmpOperands(Pred, LHS, RHS); |
0 |
| 10752 |
|
--- |
10752 |
|
--- |
| 10753 |
if (isKnownViaInduction(Pred, LHS, RHS)) |
0 |
10753 |
if (isKnownViaInduction(Pred, LHS, RHS)) |
0 |
| 10754 |
return true; |
0 |
10754 |
return true; |
0 |
| 10755 |
|
--- |
10755 |
|
--- |
| 10756 |
if (isKnownPredicateViaSplitting(Pred, LHS, RHS)) |
0 |
10756 |
if (isKnownPredicateViaSplitting(Pred, LHS, RHS)) |
0 |
| 10757 |
return true; |
0 |
10757 |
return true; |
0 |
| 10758 |
|
--- |
10758 |
|
--- |
| 10759 |
// Otherwise see what can be done with some simple reasoning. |
--- |
10759 |
// Otherwise see what can be done with some simple reasoning. |
--- |
| 10760 |
return isKnownViaNonRecursiveReasoning(Pred, LHS, RHS); |
0 |
10760 |
return isKnownViaNonRecursiveReasoning(Pred, LHS, RHS); |
0 |
| 10761 |
} |
--- |
10761 |
} |
--- |
| 10762 |
|
--- |
10762 |
|
--- |
| 10763 |
std::optional ScalarEvolution::evaluatePredicate(ICmpInst::Predicate Pred, |
0 |
10763 |
std::optional ScalarEvolution::evaluatePredicate(ICmpInst::Predicate Pred, |
0 |
| 10764 |
const SCEV *LHS, |
--- |
10764 |
const SCEV *LHS, |
--- |
| 10765 |
const SCEV *RHS) { |
--- |
10765 |
const SCEV *RHS) { |
--- |
| 10766 |
if (isKnownPredicate(Pred, LHS, RHS)) |
0 |
10766 |
if (isKnownPredicate(Pred, LHS, RHS)) |
0 |
| 10767 |
return true; |
0 |
10767 |
return true; |
0 |
| 10768 |
if (isKnownPredicate(ICmpInst::getInversePredicate(Pred), LHS, RHS)) |
0 |
10768 |
if (isKnownPredicate(ICmpInst::getInversePredicate(Pred), LHS, RHS)) |
0 |
| 10769 |
return false; |
0 |
10769 |
return false; |
0 |
| 10770 |
return std::nullopt; |
0 |
10770 |
return std::nullopt; |
0 |
| 10771 |
} |
--- |
10771 |
} |
--- |
| 10772 |
|
--- |
10772 |
|
--- |
| 10773 |
bool ScalarEvolution::isKnownPredicateAt(ICmpInst::Predicate Pred, |
0 |
10773 |
bool ScalarEvolution::isKnownPredicateAt(ICmpInst::Predicate Pred, |
0 |
| 10774 |
const SCEV *LHS, const SCEV *RHS, |
--- |
10774 |
const SCEV *LHS, const SCEV *RHS, |
--- |
| 10775 |
const Instruction *CtxI) { |
--- |
10775 |
const Instruction *CtxI) { |
--- |
| 10776 |
// TODO: Analyze guards and assumes from Context's block. |
--- |
10776 |
// TODO: Analyze guards and assumes from Context's block. |
--- |
| 10777 |
return isKnownPredicate(Pred, LHS, RHS) || |
0 |
10777 |
return isKnownPredicate(Pred, LHS, RHS) || |
0 |
| 10778 |
isBasicBlockEntryGuardedByCond(CtxI->getParent(), Pred, LHS, RHS); |
0 |
10778 |
isBasicBlockEntryGuardedByCond(CtxI->getParent(), Pred, LHS, RHS); |
0 |
| 10779 |
} |
--- |
10779 |
} |
--- |
| 10780 |
|
--- |
10780 |
|
--- |
| 10781 |
std::optional |
--- |
10781 |
std::optional |
--- |
| 10782 |
ScalarEvolution::evaluatePredicateAt(ICmpInst::Predicate Pred, const SCEV *LHS, |
0 |
10782 |
ScalarEvolution::evaluatePredicateAt(ICmpInst::Predicate Pred, const SCEV *LHS, |
0 |
| 10783 |
const SCEV *RHS, const Instruction *CtxI) { |
--- |
10783 |
const SCEV *RHS, const Instruction *CtxI) { |
--- |
| 10784 |
std::optional KnownWithoutContext = evaluatePredicate(Pred, LHS, RHS); |
0 |
10784 |
std::optional KnownWithoutContext = evaluatePredicate(Pred, LHS, RHS); |
0 |
| 10785 |
if (KnownWithoutContext) |
0 |
10785 |
if (KnownWithoutContext) |
0 |
| 10786 |
return KnownWithoutContext; |
0 |
10786 |
return KnownWithoutContext; |
0 |
| 10787 |
|
--- |
10787 |
|
--- |
| 10788 |
if (isBasicBlockEntryGuardedByCond(CtxI->getParent(), Pred, LHS, RHS)) |
0 |
10788 |
if (isBasicBlockEntryGuardedByCond(CtxI->getParent(), Pred, LHS, RHS)) |
0 |
| 10789 |
return true; |
0 |
10789 |
return true; |
0 |
| 10790 |
if (isBasicBlockEntryGuardedByCond(CtxI->getParent(), |
0 |
10790 |
if (isBasicBlockEntryGuardedByCond(CtxI->getParent(), |
0 |
| 10791 |
ICmpInst::getInversePredicate(Pred), |
--- |
10791 |
ICmpInst::getInversePredicate(Pred), |
--- |
| 10792 |
LHS, RHS)) |
--- |
10792 |
LHS, RHS)) |
--- |
| 10793 |
return false; |
0 |
10793 |
return false; |
0 |
| 10794 |
return std::nullopt; |
0 |
10794 |
return std::nullopt; |
0 |
| 10795 |
} |
--- |
10795 |
} |
--- |
| 10796 |
|
--- |
10796 |
|
--- |
| 10797 |
bool ScalarEvolution::isKnownOnEveryIteration(ICmpInst::Predicate Pred, |
0 |
10797 |
bool ScalarEvolution::isKnownOnEveryIteration(ICmpInst::Predicate Pred, |
0 |
| 10798 |
const SCEVAddRecExpr *LHS, |
--- |
10798 |
const SCEVAddRecExpr *LHS, |
--- |
| 10799 |
const SCEV *RHS) { |
--- |
10799 |
const SCEV *RHS) { |
--- |
| 10800 |
const Loop *L = LHS->getLoop(); |
0 |
10800 |
const Loop *L = LHS->getLoop(); |
0 |
| 10801 |
return isLoopEntryGuardedByCond(L, Pred, LHS->getStart(), RHS) && |
0 |
10801 |
return isLoopEntryGuardedByCond(L, Pred, LHS->getStart(), RHS) && |
0 |
| 10802 |
isLoopBackedgeGuardedByCond(L, Pred, LHS->getPostIncExpr(*this), RHS); |
0 |
10802 |
isLoopBackedgeGuardedByCond(L, Pred, LHS->getPostIncExpr(*this), RHS); |
0 |
| 10803 |
} |
--- |
10803 |
} |
--- |
| 10804 |
|
--- |
10804 |
|
--- |
| 10805 |
std::optional |
--- |
10805 |
std::optional |
--- |
| 10806 |
ScalarEvolution::getMonotonicPredicateType(const SCEVAddRecExpr *LHS, |
0 |
10806 |
ScalarEvolution::getMonotonicPredicateType(const SCEVAddRecExpr *LHS, |
0 |
| 10807 |
ICmpInst::Predicate Pred) { |
--- |
10807 |
ICmpInst::Predicate Pred) { |
--- |
| 10808 |
auto Result = getMonotonicPredicateTypeImpl(LHS, Pred); |
0 |
10808 |
auto Result = getMonotonicPredicateTypeImpl(LHS, Pred); |
0 |
| 10809 |
|
--- |
10809 |
|
--- |
| 10810 |
#ifndef NDEBUG |
--- |
10810 |
#ifndef NDEBUG |
--- |
| 10811 |
// Verify an invariant: inverting the predicate should turn a monotonically |
--- |
10811 |
// Verify an invariant: inverting the predicate should turn a monotonically |
--- |
| 10812 |
// increasing change to a monotonically decreasing one, and vice versa. |
--- |
10812 |
// increasing change to a monotonically decreasing one, and vice versa. |
--- |
| 10813 |
if (Result) { |
0 |
10813 |
if (Result) { |
0 |
| 10814 |
auto ResultSwapped = |
--- |
10814 |
auto ResultSwapped = |
--- |
| 10815 |
getMonotonicPredicateTypeImpl(LHS, ICmpInst::getSwappedPredicate(Pred)); |
0 |
10815 |
getMonotonicPredicateTypeImpl(LHS, ICmpInst::getSwappedPredicate(Pred)); |
0 |
| 10816 |
|
--- |
10816 |
|
--- |
| 10817 |
assert(*ResultSwapped != *Result && |
0 |
10817 |
assert(*ResultSwapped != *Result && |
0 |
| 10818 |
"monotonicity should flip as we flip the predicate"); |
--- |
10818 |
"monotonicity should flip as we flip the predicate"); |
--- |
| 10819 |
} |
--- |
10819 |
} |
--- |
| 10820 |
#endif |
--- |
10820 |
#endif |
--- |
| 10821 |
|
--- |
10821 |
|
--- |
| 10822 |
return Result; |
0 |
10822 |
return Result; |
0 |
| 10823 |
} |
--- |
10823 |
} |
--- |
| 10824 |
|
--- |
10824 |
|
--- |
| 10825 |
std::optional |
--- |
10825 |
std::optional |
--- |
| 10826 |
ScalarEvolution::getMonotonicPredicateTypeImpl(const SCEVAddRecExpr *LHS, |
0 |
10826 |
ScalarEvolution::getMonotonicPredicateTypeImpl(const SCEVAddRecExpr *LHS, |
0 |
| 10827 |
ICmpInst::Predicate Pred) { |
--- |
10827 |
ICmpInst::Predicate Pred) { |
--- |
| 10828 |
// A zero step value for LHS means the induction variable is essentially a |
--- |
10828 |
// A zero step value for LHS means the induction variable is essentially a |
--- |
| 10829 |
// loop invariant value. We don't really depend on the predicate actually |
--- |
10829 |
// loop invariant value. We don't really depend on the predicate actually |
--- |
| 10830 |
// flipping from false to true (for increasing predicates, and the other way |
--- |
10830 |
// flipping from false to true (for increasing predicates, and the other way |
--- |
| 10831 |
// around for decreasing predicates), all we care about is that *if* the |
--- |
10831 |
// around for decreasing predicates), all we care about is that *if* the |
--- |
| 10832 |
// predicate changes then it only changes from false to true. |
--- |
10832 |
// predicate changes then it only changes from false to true. |
--- |
| 10833 |
// |
--- |
10833 |
// |
--- |
| 10834 |
// A zero step value in itself is not very useful, but there may be places |
--- |
10834 |
// A zero step value in itself is not very useful, but there may be places |
--- |
| 10835 |
// where SCEV can prove X >= 0 but not prove X > 0, so it is helpful to be |
--- |
10835 |
// where SCEV can prove X >= 0 but not prove X > 0, so it is helpful to be |
--- |
| 10836 |
// as general as possible. |
--- |
10836 |
// as general as possible. |
--- |
| 10837 |
|
--- |
10837 |
|
--- |
| 10838 |
// Only handle LE/LT/GE/GT predicates. |
--- |
10838 |
// Only handle LE/LT/GE/GT predicates. |
--- |
| 10839 |
if (!ICmpInst::isRelational(Pred)) |
0 |
10839 |
if (!ICmpInst::isRelational(Pred)) |
0 |
| 10840 |
return std::nullopt; |
0 |
10840 |
return std::nullopt; |
0 |
| 10841 |
|
--- |
10841 |
|
--- |
| 10842 |
bool IsGreater = ICmpInst::isGE(Pred) || ICmpInst::isGT(Pred); |
0 |
10842 |
bool IsGreater = ICmpInst::isGE(Pred) || ICmpInst::isGT(Pred); |
0 |
| 10843 |
assert((IsGreater || ICmpInst::isLE(Pred) || ICmpInst::isLT(Pred)) && |
0 |
10843 |
assert((IsGreater || ICmpInst::isLE(Pred) || ICmpInst::isLT(Pred)) && |
0 |
| 10844 |
"Should be greater or less!"); |
--- |
10844 |
"Should be greater or less!"); |
--- |
| 10845 |
|
--- |
10845 |
|
--- |
| 10846 |
// Check that AR does not wrap. |
--- |
10846 |
// Check that AR does not wrap. |
--- |
| 10847 |
if (ICmpInst::isUnsigned(Pred)) { |
0 |
10847 |
if (ICmpInst::isUnsigned(Pred)) { |
0 |
| 10848 |
if (!LHS->hasNoUnsignedWrap()) |
0 |
10848 |
if (!LHS->hasNoUnsignedWrap()) |
0 |
| 10849 |
return std::nullopt; |
0 |
10849 |
return std::nullopt; |
0 |
| 10850 |
return IsGreater ? MonotonicallyIncreasing : MonotonicallyDecreasing; |
0 |
10850 |
return IsGreater ? MonotonicallyIncreasing : MonotonicallyDecreasing; |
0 |
| 10851 |
} |
--- |
10851 |
} |
--- |
| 10852 |
assert(ICmpInst::isSigned(Pred) && |
0 |
10852 |
assert(ICmpInst::isSigned(Pred) && |
0 |
| 10853 |
"Relational predicate is either signed or unsigned!"); |
--- |
10853 |
"Relational predicate is either signed or unsigned!"); |
--- |
| 10854 |
if (!LHS->hasNoSignedWrap()) |
0 |
10854 |
if (!LHS->hasNoSignedWrap()) |
0 |
| 10855 |
return std::nullopt; |
0 |
10855 |
return std::nullopt; |
0 |
| 10856 |
|
--- |
10856 |
|
--- |
| 10857 |
const SCEV *Step = LHS->getStepRecurrence(*this); |
0 |
10857 |
const SCEV *Step = LHS->getStepRecurrence(*this); |
0 |
| 10858 |
|
--- |
10858 |
|
--- |
| 10859 |
if (isKnownNonNegative(Step)) |
0 |
10859 |
if (isKnownNonNegative(Step)) |
0 |
| 10860 |
return IsGreater ? MonotonicallyIncreasing : MonotonicallyDecreasing; |
0 |
10860 |
return IsGreater ? MonotonicallyIncreasing : MonotonicallyDecreasing; |
0 |
| 10861 |
|
--- |
10861 |
|
--- |
| 10862 |
if (isKnownNonPositive(Step)) |
0 |
10862 |
if (isKnownNonPositive(Step)) |
0 |
| 10863 |
return !IsGreater ? MonotonicallyIncreasing : MonotonicallyDecreasing; |
0 |
10863 |
return !IsGreater ? MonotonicallyIncreasing : MonotonicallyDecreasing; |
0 |
| 10864 |
|
--- |
10864 |
|
--- |
| 10865 |
return std::nullopt; |
0 |
10865 |
return std::nullopt; |
0 |
| 10866 |
} |
--- |
10866 |
} |
--- |
| 10867 |
|
--- |
10867 |
|
--- |
| 10868 |
std::optional |
--- |
10868 |
std::optional |
--- |
| 10869 |
ScalarEvolution::getLoopInvariantPredicate(ICmpInst::Predicate Pred, |
0 |
10869 |
ScalarEvolution::getLoopInvariantPredicate(ICmpInst::Predicate Pred, |
0 |
| 10870 |
const SCEV *LHS, const SCEV *RHS, |
--- |
10870 |
const SCEV *LHS, const SCEV *RHS, |
--- |
| 10871 |
const Loop *L, |
--- |
10871 |
const Loop *L, |
--- |
| 10872 |
const Instruction *CtxI) { |
--- |
10872 |
const Instruction *CtxI) { |
--- |
| 10873 |
// If there is a loop-invariant, force it into the RHS, otherwise bail out. |
--- |
10873 |
// If there is a loop-invariant, force it into the RHS, otherwise bail out. |
--- |
| 10874 |
if (!isLoopInvariant(RHS, L)) { |
0 |
10874 |
if (!isLoopInvariant(RHS, L)) { |
0 |
| 10875 |
if (!isLoopInvariant(LHS, L)) |
0 |
10875 |
if (!isLoopInvariant(LHS, L)) |
0 |
| 10876 |
return std::nullopt; |
0 |
10876 |
return std::nullopt; |
0 |
| 10877 |
|
--- |
10877 |
|
--- |
| 10878 |
std::swap(LHS, RHS); |
0 |
10878 |
std::swap(LHS, RHS); |
0 |
| 10879 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
10879 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
| 10880 |
} |
--- |
10880 |
} |
--- |
| 10881 |
|
--- |
10881 |
|
--- |
| 10882 |
const SCEVAddRecExpr *ArLHS = dyn_cast(LHS); |
0 |
10882 |
const SCEVAddRecExpr *ArLHS = dyn_cast(LHS); |
0 |
| 10883 |
if (!ArLHS || ArLHS->getLoop() != L) |
0 |
10883 |
if (!ArLHS || ArLHS->getLoop() != L) |
0 |
| 10884 |
return std::nullopt; |
0 |
10884 |
return std::nullopt; |
0 |
| 10885 |
|
--- |
10885 |
|
--- |
| 10886 |
auto MonotonicType = getMonotonicPredicateType(ArLHS, Pred); |
0 |
10886 |
auto MonotonicType = getMonotonicPredicateType(ArLHS, Pred); |
0 |
| 10887 |
if (!MonotonicType) |
0 |
10887 |
if (!MonotonicType) |
0 |
| 10888 |
return std::nullopt; |
0 |
10888 |
return std::nullopt; |
0 |
| 10889 |
// If the predicate "ArLHS `Pred` RHS" monotonically increases from false to |
--- |
10889 |
// If the predicate "ArLHS `Pred` RHS" monotonically increases from false to |
--- |
| 10890 |
// true as the loop iterates, and the backedge is control dependent on |
--- |
10890 |
// true as the loop iterates, and the backedge is control dependent on |
--- |
| 10891 |
// "ArLHS `Pred` RHS" == true then we can reason as follows: |
--- |
10891 |
// "ArLHS `Pred` RHS" == true then we can reason as follows: |
--- |
| 10892 |
// |
--- |
10892 |
// |
--- |
| 10893 |
// * if the predicate was false in the first iteration then the predicate |
--- |
10893 |
// * if the predicate was false in the first iteration then the predicate |
--- |
| 10894 |
// is never evaluated again, since the loop exits without taking the |
--- |
10894 |
// is never evaluated again, since the loop exits without taking the |
--- |
| 10895 |
// backedge. |
--- |
10895 |
// backedge. |
--- |
| 10896 |
// * if the predicate was true in the first iteration then it will |
--- |
10896 |
// * if the predicate was true in the first iteration then it will |
--- |
| 10897 |
// continue to be true for all future iterations since it is |
--- |
10897 |
// continue to be true for all future iterations since it is |
--- |
| 10898 |
// monotonically increasing. |
--- |
10898 |
// monotonically increasing. |
--- |
| 10899 |
// |
--- |
10899 |
// |
--- |
| 10900 |
// For both the above possibilities, we can replace the loop varying |
--- |
10900 |
// For both the above possibilities, we can replace the loop varying |
--- |
| 10901 |
// predicate with its value on the first iteration of the loop (which is |
--- |
10901 |
// predicate with its value on the first iteration of the loop (which is |
--- |
| 10902 |
// loop invariant). |
--- |
10902 |
// loop invariant). |
--- |
| 10903 |
// |
--- |
10903 |
// |
--- |
| 10904 |
// A similar reasoning applies for a monotonically decreasing predicate, by |
--- |
10904 |
// A similar reasoning applies for a monotonically decreasing predicate, by |
--- |
| 10905 |
// replacing true with false and false with true in the above two bullets. |
--- |
10905 |
// replacing true with false and false with true in the above two bullets. |
--- |
| 10906 |
bool Increasing = *MonotonicType == ScalarEvolution::MonotonicallyIncreasing; |
0 |
10906 |
bool Increasing = *MonotonicType == ScalarEvolution::MonotonicallyIncreasing; |
0 |
| 10907 |
auto P = Increasing ? Pred : ICmpInst::getInversePredicate(Pred); |
0 |
10907 |
auto P = Increasing ? Pred : ICmpInst::getInversePredicate(Pred); |
0 |
| 10908 |
|
--- |
10908 |
|
--- |
| 10909 |
if (isLoopBackedgeGuardedByCond(L, P, LHS, RHS)) |
0 |
10909 |
if (isLoopBackedgeGuardedByCond(L, P, LHS, RHS)) |
0 |
| 10910 |
return ScalarEvolution::LoopInvariantPredicate(Pred, ArLHS->getStart(), |
0 |
10910 |
return ScalarEvolution::LoopInvariantPredicate(Pred, ArLHS->getStart(), |
0 |
| 10911 |
RHS); |
0 |
10911 |
RHS); |
0 |
| 10912 |
|
--- |
10912 |
|
--- |
| 10913 |
if (!CtxI) |
0 |
10913 |
if (!CtxI) |
0 |
| 10914 |
return std::nullopt; |
0 |
10914 |
return std::nullopt; |
0 |
| 10915 |
// Try to prove via context. |
--- |
10915 |
// Try to prove via context. |
--- |
| 10916 |
// TODO: Support other cases. |
--- |
10916 |
// TODO: Support other cases. |
--- |
| 10917 |
switch (Pred) { |
0 |
10917 |
switch (Pred) { |
0 |
| 10918 |
default: |
0 |
10918 |
default: |
0 |
| 10919 |
break; |
0 |
10919 |
break; |
0 |
| 10920 |
case ICmpInst::ICMP_ULE: |
0 |
10920 |
case ICmpInst::ICMP_ULE: |
0 |
| 10921 |
case ICmpInst::ICMP_ULT: { |
--- |
10921 |
case ICmpInst::ICMP_ULT: { |
--- |
| 10922 |
assert(ArLHS->hasNoUnsignedWrap() && "Is a requirement of monotonicity!"); |
0 |
10922 |
assert(ArLHS->hasNoUnsignedWrap() && "Is a requirement of monotonicity!"); |
0 |
| 10923 |
// Given preconditions |
--- |
10923 |
// Given preconditions |
--- |
| 10924 |
// (1) ArLHS does not cross the border of positive and negative parts of |
--- |
10924 |
// (1) ArLHS does not cross the border of positive and negative parts of |
--- |
| 10925 |
// range because of: |
--- |
10925 |
// range because of: |
--- |
| 10926 |
// - Positive step; (TODO: lift this limitation) |
--- |
10926 |
// - Positive step; (TODO: lift this limitation) |
--- |
| 10927 |
// - nuw - does not cross zero boundary; |
--- |
10927 |
// - nuw - does not cross zero boundary; |
--- |
| 10928 |
// - nsw - does not cross SINT_MAX boundary; |
--- |
10928 |
// - nsw - does not cross SINT_MAX boundary; |
--- |
| 10929 |
// (2) ArLHS
| --- |
10929 |
// (2) ArLHS
| --- |
| |
| 10930 |
// (3) RHS >=s 0 |
--- |
10930 |
// (3) RHS >=s 0 |
--- |
| 10931 |
// we can replace the loop variant ArLHS
| --- |
10931 |
// we can replace the loop variant ArLHS
| --- |
| 10932 |
// invariant Start(ArLHS)
| --- |
10932 |
// invariant Start(ArLHS)
| --- |
| 10933 |
// |
--- |
10933 |
// |
--- |
| 10934 |
// Because of (1) there are two options: |
--- |
10934 |
// Because of (1) there are two options: |
--- |
| 10935 |
// - ArLHS is always negative. It means that ArLHS
| --- |
10935 |
// - ArLHS is always negative. It means that ArLHS
| --- |
| 10936 |
// - ArLHS is always non-negative. Because of (3) RHS is also non-negative. |
--- |
10936 |
// - ArLHS is always non-negative. Because of (3) RHS is also non-negative. |
--- |
| 10937 |
// It means that ArLHS ArLHS
| --- |
10937 |
// It means that ArLHS ArLHS
| --- |
| |
| 10938 |
// Because of (2) ArLHS
| --- |
10938 |
// Because of (2) ArLHS
| --- |
| 10939 |
// All together it means that ArLHS Start(ArLHS) >=s 0. |
--- |
10939 |
// All together it means that ArLHS Start(ArLHS) >=s 0. |
--- |
| 10940 |
// We can strengthen this to Start(ArLHS)
| --- |
10940 |
// We can strengthen this to Start(ArLHS)
| --- |
| 10941 |
auto SignFlippedPred = ICmpInst::getFlippedSignednessPredicate(Pred); |
0 |
10941 |
auto SignFlippedPred = ICmpInst::getFlippedSignednessPredicate(Pred); |
0 |
| 10942 |
if (ArLHS->hasNoSignedWrap() && ArLHS->isAffine() && |
0 |
10942 |
if (ArLHS->hasNoSignedWrap() && ArLHS->isAffine() && |
0 |
| 10943 |
isKnownPositive(ArLHS->getStepRecurrence(*this)) && |
0 |
10943 |
isKnownPositive(ArLHS->getStepRecurrence(*this)) && |
0 |
| 10944 |
isKnownNonNegative(RHS) && |
0 |
10944 |
isKnownNonNegative(RHS) && |
0 |
| 10945 |
isKnownPredicateAt(SignFlippedPred, ArLHS, RHS, CtxI)) |
0 |
10945 |
isKnownPredicateAt(SignFlippedPred, ArLHS, RHS, CtxI)) |
0 |
| 10946 |
return ScalarEvolution::LoopInvariantPredicate(Pred, ArLHS->getStart(), |
0 |
10946 |
return ScalarEvolution::LoopInvariantPredicate(Pred, ArLHS->getStart(), |
0 |
| 10947 |
RHS); |
0 |
10947 |
RHS); |
0 |
| 10948 |
} |
--- |
10948 |
} |
--- |
| 10949 |
} |
--- |
10949 |
} |
--- |
| 10950 |
|
--- |
10950 |
|
--- |
| 10951 |
return std::nullopt; |
0 |
10951 |
return std::nullopt; |
0 |
| 10952 |
} |
--- |
10952 |
} |
--- |
| 10953 |
|
--- |
10953 |
|
--- |
| 10954 |
std::optional |
--- |
10954 |
std::optional |
--- |
| 10955 |
ScalarEvolution::getLoopInvariantExitCondDuringFirstIterations( |
0 |
10955 |
ScalarEvolution::getLoopInvariantExitCondDuringFirstIterations( |
0 |
| 10956 |
ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, |
--- |
10956 |
ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, |
--- |
| 10957 |
const Instruction *CtxI, const SCEV *MaxIter) { |
--- |
10957 |
const Instruction *CtxI, const SCEV *MaxIter) { |
--- |
| 10958 |
if (auto LIP = getLoopInvariantExitCondDuringFirstIterationsImpl( |
0 |
10958 |
if (auto LIP = getLoopInvariantExitCondDuringFirstIterationsImpl( |
0 |
| 10959 |
Pred, LHS, RHS, L, CtxI, MaxIter)) |
0 |
10959 |
Pred, LHS, RHS, L, CtxI, MaxIter)) |
0 |
| 10960 |
return LIP; |
0 |
10960 |
return LIP; |
0 |
| 10961 |
if (auto *UMin = dyn_cast(MaxIter)) |
0 |
10961 |
if (auto *UMin = dyn_cast(MaxIter)) |
0 |
| 10962 |
// Number of iterations expressed as UMIN isn't always great for expressing |
--- |
10962 |
// Number of iterations expressed as UMIN isn't always great for expressing |
--- |
| 10963 |
// the value on the last iteration. If the straightforward approach didn't |
--- |
10963 |
// the value on the last iteration. If the straightforward approach didn't |
--- |
| 10964 |
// work, try the following trick: if the a predicate is invariant for X, it |
--- |
10964 |
// work, try the following trick: if the a predicate is invariant for X, it |
--- |
| 10965 |
// is also invariant for umin(X, ...). So try to find something that works |
--- |
10965 |
// is also invariant for umin(X, ...). So try to find something that works |
--- |
| 10966 |
// among subexpressions of MaxIter expressed as umin. |
--- |
10966 |
// among subexpressions of MaxIter expressed as umin. |
--- |
| 10967 |
for (auto *Op : UMin->operands()) |
0 |
10967 |
for (auto *Op : UMin->operands()) |
0 |
| 10968 |
if (auto LIP = getLoopInvariantExitCondDuringFirstIterationsImpl( |
0 |
10968 |
if (auto LIP = getLoopInvariantExitCondDuringFirstIterationsImpl( |
0 |
| 10969 |
Pred, LHS, RHS, L, CtxI, Op)) |
0 |
10969 |
Pred, LHS, RHS, L, CtxI, Op)) |
0 |
| 10970 |
return LIP; |
0 |
10970 |
return LIP; |
0 |
| 10971 |
return std::nullopt; |
0 |
10971 |
return std::nullopt; |
0 |
| 10972 |
} |
--- |
10972 |
} |
--- |
| 10973 |
|
--- |
10973 |
|
--- |
| 10974 |
std::optional |
--- |
10974 |
std::optional |
--- |
| 10975 |
ScalarEvolution::getLoopInvariantExitCondDuringFirstIterationsImpl( |
0 |
10975 |
ScalarEvolution::getLoopInvariantExitCondDuringFirstIterationsImpl( |
0 |
| 10976 |
ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, |
--- |
10976 |
ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, |
--- |
| 10977 |
const Instruction *CtxI, const SCEV *MaxIter) { |
--- |
10977 |
const Instruction *CtxI, const SCEV *MaxIter) { |
--- |
| 10978 |
// Try to prove the following set of facts: |
--- |
10978 |
// Try to prove the following set of facts: |
--- |
| 10979 |
// - The predicate is monotonic in the iteration space. |
--- |
10979 |
// - The predicate is monotonic in the iteration space. |
--- |
| 10980 |
// - If the check does not fail on the 1st iteration: |
--- |
10980 |
// - If the check does not fail on the 1st iteration: |
--- |
| 10981 |
// - No overflow will happen during first MaxIter iterations; |
--- |
10981 |
// - No overflow will happen during first MaxIter iterations; |
--- |
| 10982 |
// - It will not fail on the MaxIter'th iteration. |
--- |
10982 |
// - It will not fail on the MaxIter'th iteration. |
--- |
| 10983 |
// If the check does fail on the 1st iteration, we leave the loop and no |
--- |
10983 |
// If the check does fail on the 1st iteration, we leave the loop and no |
--- |
| 10984 |
// other checks matter. |
--- |
10984 |
// other checks matter. |
--- |
| 10985 |
|
--- |
10985 |
|
--- |
| 10986 |
// If there is a loop-invariant, force it into the RHS, otherwise bail out. |
--- |
10986 |
// If there is a loop-invariant, force it into the RHS, otherwise bail out. |
--- |
| 10987 |
if (!isLoopInvariant(RHS, L)) { |
0 |
10987 |
if (!isLoopInvariant(RHS, L)) { |
0 |
| 10988 |
if (!isLoopInvariant(LHS, L)) |
0 |
10988 |
if (!isLoopInvariant(LHS, L)) |
0 |
| 10989 |
return std::nullopt; |
0 |
10989 |
return std::nullopt; |
0 |
| 10990 |
|
--- |
10990 |
|
--- |
| 10991 |
std::swap(LHS, RHS); |
0 |
10991 |
std::swap(LHS, RHS); |
0 |
| 10992 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
10992 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
| 10993 |
} |
--- |
10993 |
} |
--- |
| 10994 |
|
--- |
10994 |
|
--- |
| 10995 |
auto *AR = dyn_cast(LHS); |
0 |
10995 |
auto *AR = dyn_cast(LHS); |
0 |
| 10996 |
if (!AR || AR->getLoop() != L) |
0 |
10996 |
if (!AR || AR->getLoop() != L) |
0 |
| 10997 |
return std::nullopt; |
0 |
10997 |
return std::nullopt; |
0 |
| 10998 |
|
--- |
10998 |
|
--- |
| 10999 |
// The predicate must be relational (i.e. <, <=, >=, >). |
--- |
10999 |
// The predicate must be relational (i.e. <, <=, >=, >). |
--- |
| 11000 |
if (!ICmpInst::isRelational(Pred)) |
0 |
11000 |
if (!ICmpInst::isRelational(Pred)) |
0 |
| 11001 |
return std::nullopt; |
0 |
11001 |
return std::nullopt; |
0 |
| 11002 |
|
--- |
11002 |
|
--- |
| 11003 |
// TODO: Support steps other than +/- 1. |
--- |
11003 |
// TODO: Support steps other than +/- 1. |
--- |
| 11004 |
const SCEV *Step = AR->getStepRecurrence(*this); |
0 |
11004 |
const SCEV *Step = AR->getStepRecurrence(*this); |
0 |
| 11005 |
auto *One = getOne(Step->getType()); |
0 |
11005 |
auto *One = getOne(Step->getType()); |
0 |
| 11006 |
auto *MinusOne = getNegativeSCEV(One); |
0 |
11006 |
auto *MinusOne = getNegativeSCEV(One); |
0 |
| 11007 |
if (Step != One && Step != MinusOne) |
0 |
11007 |
if (Step != One && Step != MinusOne) |
0 |
| 11008 |
return std::nullopt; |
0 |
11008 |
return std::nullopt; |
0 |
| 11009 |
|
--- |
11009 |
|
--- |
| 11010 |
// Type mismatch here means that MaxIter is potentially larger than max |
--- |
11010 |
// Type mismatch here means that MaxIter is potentially larger than max |
--- |
| 11011 |
// unsigned value in start type, which mean we cannot prove no wrap for the |
--- |
11011 |
// unsigned value in start type, which mean we cannot prove no wrap for the |
--- |
| 11012 |
// indvar. |
--- |
11012 |
// indvar. |
--- |
| 11013 |
if (AR->getType() != MaxIter->getType()) |
0 |
11013 |
if (AR->getType() != MaxIter->getType()) |
0 |
| 11014 |
return std::nullopt; |
0 |
11014 |
return std::nullopt; |
0 |
| 11015 |
|
--- |
11015 |
|
--- |
| 11016 |
// Value of IV on suggested last iteration. |
--- |
11016 |
// Value of IV on suggested last iteration. |
--- |
| 11017 |
const SCEV *Last = AR->evaluateAtIteration(MaxIter, *this); |
0 |
11017 |
const SCEV *Last = AR->evaluateAtIteration(MaxIter, *this); |
0 |
| 11018 |
// Does it still meet the requirement? |
--- |
11018 |
// Does it still meet the requirement? |
--- |
| 11019 |
if (!isLoopBackedgeGuardedByCond(L, Pred, Last, RHS)) |
0 |
11019 |
if (!isLoopBackedgeGuardedByCond(L, Pred, Last, RHS)) |
0 |
| 11020 |
return std::nullopt; |
0 |
11020 |
return std::nullopt; |
0 |
| 11021 |
// Because step is +/- 1 and MaxIter has same type as Start (i.e. it does |
--- |
11021 |
// Because step is +/- 1 and MaxIter has same type as Start (i.e. it does |
--- |
| 11022 |
// not exceed max unsigned value of this type), this effectively proves |
--- |
11022 |
// not exceed max unsigned value of this type), this effectively proves |
--- |
| 11023 |
// that there is no wrap during the iteration. To prove that there is no |
--- |
11023 |
// that there is no wrap during the iteration. To prove that there is no |
--- |
| 11024 |
// signed/unsigned wrap, we need to check that |
--- |
11024 |
// signed/unsigned wrap, we need to check that |
--- |
| 11025 |
// Start <= Last for step = 1 or Start >= Last for step = -1. |
--- |
11025 |
// Start <= Last for step = 1 or Start >= Last for step = -1. |
--- |
| 11026 |
ICmpInst::Predicate NoOverflowPred = |
--- |
11026 |
ICmpInst::Predicate NoOverflowPred = |
--- |
| 11027 |
CmpInst::isSigned(Pred) ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; |
0 |
11027 |
CmpInst::isSigned(Pred) ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; |
0 |
| 11028 |
if (Step == MinusOne) |
0 |
11028 |
if (Step == MinusOne) |
0 |
| 11029 |
NoOverflowPred = CmpInst::getSwappedPredicate(NoOverflowPred); |
0 |
11029 |
NoOverflowPred = CmpInst::getSwappedPredicate(NoOverflowPred); |
0 |
| 11030 |
const SCEV *Start = AR->getStart(); |
0 |
11030 |
const SCEV *Start = AR->getStart(); |
0 |
| 11031 |
if (!isKnownPredicateAt(NoOverflowPred, Start, Last, CtxI)) |
0 |
11031 |
if (!isKnownPredicateAt(NoOverflowPred, Start, Last, CtxI)) |
0 |
| 11032 |
return std::nullopt; |
0 |
11032 |
return std::nullopt; |
0 |
| 11033 |
|
--- |
11033 |
|
--- |
| 11034 |
// Everything is fine. |
--- |
11034 |
// Everything is fine. |
--- |
| 11035 |
return ScalarEvolution::LoopInvariantPredicate(Pred, Start, RHS); |
0 |
11035 |
return ScalarEvolution::LoopInvariantPredicate(Pred, Start, RHS); |
0 |
| 11036 |
} |
--- |
11036 |
} |
--- |
| 11037 |
|
--- |
11037 |
|
--- |
| 11038 |
bool ScalarEvolution::isKnownPredicateViaConstantRanges( |
0 |
11038 |
bool ScalarEvolution::isKnownPredicateViaConstantRanges( |
0 |
| 11039 |
ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS) { |
--- |
11039 |
ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS) { |
--- |
| 11040 |
if (HasSameValue(LHS, RHS)) |
0 |
11040 |
if (HasSameValue(LHS, RHS)) |
0 |
| 11041 |
return ICmpInst::isTrueWhenEqual(Pred); |
0 |
11041 |
return ICmpInst::isTrueWhenEqual(Pred); |
0 |
| 11042 |
|
--- |
11042 |
|
--- |
| 11043 |
// This code is split out from isKnownPredicate because it is called from |
--- |
11043 |
// This code is split out from isKnownPredicate because it is called from |
--- |
| 11044 |
// within isLoopEntryGuardedByCond. |
--- |
11044 |
// within isLoopEntryGuardedByCond. |
--- |
| 11045 |
|
--- |
11045 |
|
--- |
| 11046 |
auto CheckRanges = [&](const ConstantRange &RangeLHS, |
0 |
11046 |
auto CheckRanges = [&](const ConstantRange &RangeLHS, |
0 |
| 11047 |
const ConstantRange &RangeRHS) { |
--- |
11047 |
const ConstantRange &RangeRHS) { |
--- |
| 11048 |
return RangeLHS.icmp(Pred, RangeRHS); |
0 |
11048 |
return RangeLHS.icmp(Pred, RangeRHS); |
0 |
| 11049 |
}; |
0 |
11049 |
}; |
0 |
| 11050 |
|
--- |
11050 |
|
--- |
| 11051 |
// The check at the top of the function catches the case where the values are |
--- |
11051 |
// The check at the top of the function catches the case where the values are |
--- |
| 11052 |
// known to be equal. |
--- |
11052 |
// known to be equal. |
--- |
| 11053 |
if (Pred == CmpInst::ICMP_EQ) |
0 |
11053 |
if (Pred == CmpInst::ICMP_EQ) |
0 |
| 11054 |
return false; |
0 |
11054 |
return false; |
0 |
| 11055 |
|
--- |
11055 |
|
--- |
| 11056 |
if (Pred == CmpInst::ICMP_NE) { |
0 |
11056 |
if (Pred == CmpInst::ICMP_NE) { |
0 |
| 11057 |
auto SL = getSignedRange(LHS); |
0 |
11057 |
auto SL = getSignedRange(LHS); |
0 |
| 11058 |
auto SR = getSignedRange(RHS); |
0 |
11058 |
auto SR = getSignedRange(RHS); |
0 |
| 11059 |
if (CheckRanges(SL, SR)) |
0 |
11059 |
if (CheckRanges(SL, SR)) |
0 |
| 11060 |
return true; |
0 |
11060 |
return true; |
0 |
| 11061 |
auto UL = getUnsignedRange(LHS); |
0 |
11061 |
auto UL = getUnsignedRange(LHS); |
0 |
| 11062 |
auto UR = getUnsignedRange(RHS); |
0 |
11062 |
auto UR = getUnsignedRange(RHS); |
0 |
| 11063 |
if (CheckRanges(UL, UR)) |
0 |
11063 |
if (CheckRanges(UL, UR)) |
0 |
| 11064 |
return true; |
0 |
11064 |
return true; |
0 |
| 11065 |
auto *Diff = getMinusSCEV(LHS, RHS); |
0 |
11065 |
auto *Diff = getMinusSCEV(LHS, RHS); |
0 |
| 11066 |
return !isa(Diff) && isKnownNonZero(Diff); |
0 |
11066 |
return !isa(Diff) && isKnownNonZero(Diff); |
0 |
| 11067 |
} |
0 |
11067 |
} |
0 |
| 11068 |
|
--- |
11068 |
|
--- |
| 11069 |
if (CmpInst::isSigned(Pred)) { |
0 |
11069 |
if (CmpInst::isSigned(Pred)) { |
0 |
| 11070 |
auto SL = getSignedRange(LHS); |
0 |
11070 |
auto SL = getSignedRange(LHS); |
0 |
| 11071 |
auto SR = getSignedRange(RHS); |
0 |
11071 |
auto SR = getSignedRange(RHS); |
0 |
| 11072 |
return CheckRanges(SL, SR); |
0 |
11072 |
return CheckRanges(SL, SR); |
0 |
| 11073 |
} |
0 |
11073 |
} |
0 |
| 11074 |
|
--- |
11074 |
|
--- |
| 11075 |
auto UL = getUnsignedRange(LHS); |
0 |
11075 |
auto UL = getUnsignedRange(LHS); |
0 |
| 11076 |
auto UR = getUnsignedRange(RHS); |
0 |
11076 |
auto UR = getUnsignedRange(RHS); |
0 |
| 11077 |
return CheckRanges(UL, UR); |
0 |
11077 |
return CheckRanges(UL, UR); |
0 |
| 11078 |
} |
0 |
11078 |
} |
0 |
| 11079 |
|
--- |
11079 |
|
--- |
| 11080 |
bool ScalarEvolution::isKnownPredicateViaNoOverflow(ICmpInst::Predicate Pred, |
0 |
11080 |
bool ScalarEvolution::isKnownPredicateViaNoOverflow(ICmpInst::Predicate Pred, |
0 |
| 11081 |
const SCEV *LHS, |
--- |
11081 |
const SCEV *LHS, |
--- |
| 11082 |
const SCEV *RHS) { |
--- |
11082 |
const SCEV *RHS) { |
--- |
| 11083 |
// Match X to (A + C1) and Y to (A + C2), where |
--- |
11083 |
// Match X to (A + C1) and Y to (A + C2), where |
--- |
| 11084 |
// C1 and C2 are constant integers. If either X or Y are not add expressions, |
--- |
11084 |
// C1 and C2 are constant integers. If either X or Y are not add expressions, |
--- |
| 11085 |
// consider them as X + 0 and Y + 0 respectively. C1 and C2 are returned via |
--- |
11085 |
// consider them as X + 0 and Y + 0 respectively. C1 and C2 are returned via |
--- |
| 11086 |
// OutC1 and OutC2. |
--- |
11086 |
// OutC1 and OutC2. |
--- |
| 11087 |
auto MatchBinaryAddToConst = [this](const SCEV *X, const SCEV *Y, |
0 |
11087 |
auto MatchBinaryAddToConst = [this](const SCEV *X, const SCEV *Y, |
0 |
| 11088 |
APInt &OutC1, APInt &OutC2, |
--- |
11088 |
APInt &OutC1, APInt &OutC2, |
--- |
| 11089 |
SCEV::NoWrapFlags ExpectedFlags) { |
0 |
11089 |
SCEV::NoWrapFlags ExpectedFlags) { |
0 |
| 11090 |
const SCEV *XNonConstOp, *XConstOp; |
--- |
11090 |
const SCEV *XNonConstOp, *XConstOp; |
--- |
| 11091 |
const SCEV *YNonConstOp, *YConstOp; |
--- |
11091 |
const SCEV *YNonConstOp, *YConstOp; |
--- |
| 11092 |
SCEV::NoWrapFlags XFlagsPresent; |
--- |
11092 |
SCEV::NoWrapFlags XFlagsPresent; |
--- |
| 11093 |
SCEV::NoWrapFlags YFlagsPresent; |
--- |
11093 |
SCEV::NoWrapFlags YFlagsPresent; |
--- |
| 11094 |
|
--- |
11094 |
|
--- |
| 11095 |
if (!splitBinaryAdd(X, XConstOp, XNonConstOp, XFlagsPresent)) { |
0 |
11095 |
if (!splitBinaryAdd(X, XConstOp, XNonConstOp, XFlagsPresent)) { |
0 |
| 11096 |
XConstOp = getZero(X->getType()); |
0 |
11096 |
XConstOp = getZero(X->getType()); |
0 |
| 11097 |
XNonConstOp = X; |
0 |
11097 |
XNonConstOp = X; |
0 |
| 11098 |
XFlagsPresent = ExpectedFlags; |
0 |
11098 |
XFlagsPresent = ExpectedFlags; |
0 |
| 11099 |
} |
--- |
11099 |
} |
--- |
| 11100 |
if (!isa(XConstOp) || |
0 |
11100 |
if (!isa(XConstOp) || |
0 |
| 11101 |
(XFlagsPresent & ExpectedFlags) != ExpectedFlags) |
0 |
11101 |
(XFlagsPresent & ExpectedFlags) != ExpectedFlags) |
0 |
| 11102 |
return false; |
0 |
11102 |
return false; |
0 |
| 11103 |
|
--- |
11103 |
|
--- |
| 11104 |
if (!splitBinaryAdd(Y, YConstOp, YNonConstOp, YFlagsPresent)) { |
0 |
11104 |
if (!splitBinaryAdd(Y, YConstOp, YNonConstOp, YFlagsPresent)) { |
0 |
| 11105 |
YConstOp = getZero(Y->getType()); |
0 |
11105 |
YConstOp = getZero(Y->getType()); |
0 |
| 11106 |
YNonConstOp = Y; |
0 |
11106 |
YNonConstOp = Y; |
0 |
| 11107 |
YFlagsPresent = ExpectedFlags; |
0 |
11107 |
YFlagsPresent = ExpectedFlags; |
0 |
| 11108 |
} |
--- |
11108 |
} |
--- |
| 11109 |
|
--- |
11109 |
|
--- |
| 11110 |
if (!isa(YConstOp) || |
0 |
11110 |
if (!isa(YConstOp) || |
0 |
| 11111 |
(YFlagsPresent & ExpectedFlags) != ExpectedFlags) |
0 |
11111 |
(YFlagsPresent & ExpectedFlags) != ExpectedFlags) |
0 |
| 11112 |
return false; |
0 |
11112 |
return false; |
0 |
| 11113 |
|
--- |
11113 |
|
--- |
| 11114 |
if (YNonConstOp != XNonConstOp) |
0 |
11114 |
if (YNonConstOp != XNonConstOp) |
0 |
| 11115 |
return false; |
0 |
11115 |
return false; |
0 |
| 11116 |
|
--- |
11116 |
|
--- |
| 11117 |
OutC1 = cast(XConstOp)->getAPInt(); |
0 |
11117 |
OutC1 = cast(XConstOp)->getAPInt(); |
0 |
| 11118 |
OutC2 = cast(YConstOp)->getAPInt(); |
0 |
11118 |
OutC2 = cast(YConstOp)->getAPInt(); |
0 |
| 11119 |
|
--- |
11119 |
|
--- |
| 11120 |
return true; |
0 |
11120 |
return true; |
0 |
| 11121 |
}; |
0 |
11121 |
}; |
0 |
| 11122 |
|
--- |
11122 |
|
--- |
| 11123 |
APInt C1; |
0 |
11123 |
APInt C1; |
0 |
| 11124 |
APInt C2; |
0 |
11124 |
APInt C2; |
0 |
| 11125 |
|
--- |
11125 |
|
--- |
| 11126 |
switch (Pred) { |
0 |
11126 |
switch (Pred) { |
0 |
| 11127 |
default: |
0 |
11127 |
default: |
0 |
| 11128 |
break; |
0 |
11128 |
break; |
0 |
| 11129 |
|
--- |
11129 |
|
--- |
| 11130 |
case ICmpInst::ICMP_SGE: |
0 |
11130 |
case ICmpInst::ICMP_SGE: |
0 |
| 11131 |
std::swap(LHS, RHS); |
0 |
11131 |
std::swap(LHS, RHS); |
0 |
| 11132 |
[[fallthrough]]; |
--- |
11132 |
[[fallthrough]]; |
--- |
| 11133 |
case ICmpInst::ICMP_SLE: |
0 |
11133 |
case ICmpInst::ICMP_SLE: |
0 |
| 11134 |
// (X + C1) s<= (X + C2) if C1 s<= C2. |
--- |
11134 |
// (X + C1) s<= (X + C2) if C1 s<= C2. |
--- |
| 11135 |
if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNSW) && C1.sle(C2)) |
0 |
11135 |
if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNSW) && C1.sle(C2)) |
0 |
| 11136 |
return true; |
0 |
11136 |
return true; |
0 |
| 11137 |
|
--- |
11137 |
|
--- |
| 11138 |
break; |
0 |
11138 |
break; |
0 |
| 11139 |
|
--- |
11139 |
|
--- |
| 11140 |
case ICmpInst::ICMP_SGT: |
0 |
11140 |
case ICmpInst::ICMP_SGT: |
0 |
| 11141 |
std::swap(LHS, RHS); |
0 |
11141 |
std::swap(LHS, RHS); |
0 |
| 11142 |
[[fallthrough]]; |
--- |
11142 |
[[fallthrough]]; |
--- |
| 11143 |
case ICmpInst::ICMP_SLT: |
0 |
11143 |
case ICmpInst::ICMP_SLT: |
0 |
| 11144 |
// (X + C1) s< (X + C2) if C1 s< C2. |
--- |
11144 |
// (X + C1) s< (X + C2) if C1 s< C2. |
--- |
| 11145 |
if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNSW) && C1.slt(C2)) |
0 |
11145 |
if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNSW) && C1.slt(C2)) |
0 |
| 11146 |
return true; |
0 |
11146 |
return true; |
0 |
| 11147 |
|
--- |
11147 |
|
--- |
| 11148 |
break; |
0 |
11148 |
break; |
0 |
| 11149 |
|
--- |
11149 |
|
--- |
| 11150 |
case ICmpInst::ICMP_UGE: |
0 |
11150 |
case ICmpInst::ICMP_UGE: |
0 |
| 11151 |
std::swap(LHS, RHS); |
0 |
11151 |
std::swap(LHS, RHS); |
0 |
| 11152 |
[[fallthrough]]; |
--- |
11152 |
[[fallthrough]]; |
--- |
| 11153 |
case ICmpInst::ICMP_ULE: |
0 |
11153 |
case ICmpInst::ICMP_ULE: |
0 |
| 11154 |
// (X + C1) u<= (X + C2) for C1 u<= C2. |
--- |
11154 |
// (X + C1) u<= (X + C2) for C1 u<= C2. |
--- |
| 11155 |
if (MatchBinaryAddToConst(RHS, LHS, C2, C1, SCEV::FlagNUW) && C1.ule(C2)) |
0 |
11155 |
if (MatchBinaryAddToConst(RHS, LHS, C2, C1, SCEV::FlagNUW) && C1.ule(C2)) |
0 |
| 11156 |
return true; |
0 |
11156 |
return true; |
0 |
| 11157 |
|
--- |
11157 |
|
--- |
| 11158 |
break; |
0 |
11158 |
break; |
0 |
| 11159 |
|
--- |
11159 |
|
--- |
| 11160 |
case ICmpInst::ICMP_UGT: |
0 |
11160 |
case ICmpInst::ICMP_UGT: |
0 |
| 11161 |
std::swap(LHS, RHS); |
0 |
11161 |
std::swap(LHS, RHS); |
0 |
| 11162 |
[[fallthrough]]; |
--- |
11162 |
[[fallthrough]]; |
--- |
| 11163 |
case ICmpInst::ICMP_ULT: |
0 |
11163 |
case ICmpInst::ICMP_ULT: |
0 |
| 11164 |
// (X + C1) u< (X + C2) if C1 u< C2. |
--- |
11164 |
// (X + C1) u< (X + C2) if C1 u< C2. |
--- |
| 11165 |
if (MatchBinaryAddToConst(RHS, LHS, C2, C1, SCEV::FlagNUW) && C1.ult(C2)) |
0 |
11165 |
if (MatchBinaryAddToConst(RHS, LHS, C2, C1, SCEV::FlagNUW) && C1.ult(C2)) |
0 |
| 11166 |
return true; |
0 |
11166 |
return true; |
0 |
| 11167 |
break; |
0 |
11167 |
break; |
0 |
| 11168 |
} |
--- |
11168 |
} |
--- |
| 11169 |
|
--- |
11169 |
|
--- |
| 11170 |
return false; |
0 |
11170 |
return false; |
0 |
| 11171 |
} |
0 |
11171 |
} |
0 |
| 11172 |
|
--- |
11172 |
|
--- |
| 11173 |
bool ScalarEvolution::isKnownPredicateViaSplitting(ICmpInst::Predicate Pred, |
0 |
11173 |
bool ScalarEvolution::isKnownPredicateViaSplitting(ICmpInst::Predicate Pred, |
0 |
| 11174 |
const SCEV *LHS, |
--- |
11174 |
const SCEV *LHS, |
--- |
| 11175 |
const SCEV *RHS) { |
--- |
11175 |
const SCEV *RHS) { |
--- |
| 11176 |
if (Pred != ICmpInst::ICMP_ULT || ProvingSplitPredicate) |
0 |
11176 |
if (Pred != ICmpInst::ICMP_ULT || ProvingSplitPredicate) |
0 |
| 11177 |
return false; |
0 |
11177 |
return false; |
0 |
| 11178 |
|
--- |
11178 |
|
--- |
| 11179 |
// Allowing arbitrary number of activations of isKnownPredicateViaSplitting on |
--- |
11179 |
// Allowing arbitrary number of activations of isKnownPredicateViaSplitting on |
--- |
| 11180 |
// the stack can result in exponential time complexity. |
--- |
11180 |
// the stack can result in exponential time complexity. |
--- |
| 11181 |
SaveAndRestore Restore(ProvingSplitPredicate, true); |
0 |
11181 |
SaveAndRestore Restore(ProvingSplitPredicate, true); |
0 |
| 11182 |
|
--- |
11182 |
|
--- |
| 11183 |
// If L >= 0 then I `ult` L <=> I >= 0 && I `slt` L |
--- |
11183 |
// If L >= 0 then I `ult` L <=> I >= 0 && I `slt` L |
--- |
| 11184 |
// |
--- |
11184 |
// |
--- |
| 11185 |
// To prove L >= 0 we use isKnownNonNegative whereas to prove I >= 0 we use |
--- |
11185 |
// To prove L >= 0 we use isKnownNonNegative whereas to prove I >= 0 we use |
--- |
| 11186 |
// isKnownPredicate. isKnownPredicate is more powerful, but also more |
--- |
11186 |
// isKnownPredicate. isKnownPredicate is more powerful, but also more |
--- |
| 11187 |
// expensive; and using isKnownNonNegative(RHS) is sufficient for most of the |
--- |
11187 |
// expensive; and using isKnownNonNegative(RHS) is sufficient for most of the |
--- |
| 11188 |
// interesting cases seen in practice. We can consider "upgrading" L >= 0 to |
--- |
11188 |
// interesting cases seen in practice. We can consider "upgrading" L >= 0 to |
--- |
| 11189 |
// use isKnownPredicate later if needed. |
--- |
11189 |
// use isKnownPredicate later if needed. |
--- |
| 11190 |
return isKnownNonNegative(RHS) && |
0 |
11190 |
return isKnownNonNegative(RHS) && |
0 |
| 11191 |
isKnownPredicate(CmpInst::ICMP_SGE, LHS, getZero(LHS->getType())) && |
0 |
11191 |
isKnownPredicate(CmpInst::ICMP_SGE, LHS, getZero(LHS->getType())) && |
0 |
| 11192 |
isKnownPredicate(CmpInst::ICMP_SLT, LHS, RHS); |
0 |
11192 |
isKnownPredicate(CmpInst::ICMP_SLT, LHS, RHS); |
0 |
| 11193 |
} |
0 |
11193 |
} |
0 |
| 11194 |
|
--- |
11194 |
|
--- |
| 11195 |
bool ScalarEvolution::isImpliedViaGuard(const BasicBlock *BB, |
0 |
11195 |
bool ScalarEvolution::isImpliedViaGuard(const BasicBlock *BB, |
0 |
| 11196 |
ICmpInst::Predicate Pred, |
--- |
11196 |
ICmpInst::Predicate Pred, |
--- |
| 11197 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
11197 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
| 11198 |
// No need to even try if we know the module has no guards. |
--- |
11198 |
// No need to even try if we know the module has no guards. |
--- |
| 11199 |
if (!HasGuards) |
0 |
11199 |
if (!HasGuards) |
0 |
| 11200 |
return false; |
0 |
11200 |
return false; |
0 |
| 11201 |
|
--- |
11201 |
|
--- |
| 11202 |
return any_of(*BB, [&](const Instruction &I) { |
0 |
11202 |
return any_of(*BB, [&](const Instruction &I) { |
0 |
| 11203 |
using namespace llvm::PatternMatch; |
--- |
11203 |
using namespace llvm::PatternMatch; |
--- |
| 11204 |
|
--- |
11204 |
|
--- |
| 11205 |
Value *Condition; |
--- |
11205 |
Value *Condition; |
--- |
| 11206 |
return match(&I, m_Intrinsic( |
0 |
11206 |
return match(&I, m_Intrinsic( |
0 |
| 11207 |
m_Value(Condition))) && |
0 |
11207 |
m_Value(Condition))) && |
0 |
| 11208 |
isImpliedCond(Pred, LHS, RHS, Condition, false); |
0 |
11208 |
isImpliedCond(Pred, LHS, RHS, Condition, false); |
0 |
| 11209 |
}); |
0 |
11209 |
}); |
0 |
| 11210 |
} |
--- |
11210 |
} |
--- |
| 11211 |
|
--- |
11211 |
|
--- |
| 11212 |
/// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is |
--- |
11212 |
/// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is |
--- |
| 11213 |
/// protected by a conditional between LHS and RHS. This is used to |
--- |
11213 |
/// protected by a conditional between LHS and RHS. This is used to |
--- |
| 11214 |
/// to eliminate casts. |
--- |
11214 |
/// to eliminate casts. |
--- |
| 11215 |
bool |
--- |
11215 |
bool |
--- |
| 11216 |
ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L, |
0 |
11216 |
ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L, |
0 |
| 11217 |
ICmpInst::Predicate Pred, |
--- |
11217 |
ICmpInst::Predicate Pred, |
--- |
| 11218 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
11218 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
| 11219 |
// Interpret a null as meaning no loop, where there is obviously no guard |
--- |
11219 |
// Interpret a null as meaning no loop, where there is obviously no guard |
--- |
| 11220 |
// (interprocedural conditions notwithstanding). Do not bother about |
--- |
11220 |
// (interprocedural conditions notwithstanding). Do not bother about |
--- |
| 11221 |
// unreachable loops. |
--- |
11221 |
// unreachable loops. |
--- |
| 11222 |
if (!L || !DT.isReachableFromEntry(L->getHeader())) |
0 |
11222 |
if (!L || !DT.isReachableFromEntry(L->getHeader())) |
0 |
| 11223 |
return true; |
0 |
11223 |
return true; |
0 |
| 11224 |
|
--- |
11224 |
|
--- |
| 11225 |
if (VerifyIR) |
0 |
11225 |
if (VerifyIR) |
0 |
| 11226 |
assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()) && |
0 |
11226 |
assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()) && |
0 |
| 11227 |
"This cannot be done on broken IR!"); |
--- |
11227 |
"This cannot be done on broken IR!"); |
--- |
| 11228 |
|
--- |
11228 |
|
--- |
| 11229 |
|
--- |
11229 |
|
--- |
| 11230 |
if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS)) |
0 |
11230 |
if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS)) |
0 |
| 11231 |
return true; |
0 |
11231 |
return true; |
0 |
| 11232 |
|
--- |
11232 |
|
--- |
| 11233 |
BasicBlock *Latch = L->getLoopLatch(); |
0 |
11233 |
BasicBlock *Latch = L->getLoopLatch(); |
0 |
| 11234 |
if (!Latch) |
0 |
11234 |
if (!Latch) |
0 |
| 11235 |
return false; |
0 |
11235 |
return false; |
0 |
| 11236 |
|
--- |
11236 |
|
--- |
| 11237 |
BranchInst *LoopContinuePredicate = |
--- |
11237 |
BranchInst *LoopContinuePredicate = |
--- |
| 11238 |
dyn_cast(Latch->getTerminator()); |
0 |
11238 |
dyn_cast(Latch->getTerminator()); |
0 |
| 11239 |
if (LoopContinuePredicate && LoopContinuePredicate->isConditional() && |
0 |
11239 |
if (LoopContinuePredicate && LoopContinuePredicate->isConditional() && |
0 |
| 11240 |
isImpliedCond(Pred, LHS, RHS, |
0 |
11240 |
isImpliedCond(Pred, LHS, RHS, |
0 |
| 11241 |
LoopContinuePredicate->getCondition(), |
0 |
11241 |
LoopContinuePredicate->getCondition(), |
0 |
| 11242 |
LoopContinuePredicate->getSuccessor(0) != L->getHeader())) |
0 |
11242 |
LoopContinuePredicate->getSuccessor(0) != L->getHeader())) |
0 |
| 11243 |
return true; |
0 |
11243 |
return true; |
0 |
| 11244 |
|
--- |
11244 |
|
--- |
| 11245 |
// We don't want more than one activation of the following loops on the stack |
--- |
11245 |
// We don't want more than one activation of the following loops on the stack |
--- |
| 11246 |
// -- that can lead to O(n!) time complexity. |
--- |
11246 |
// -- that can lead to O(n!) time complexity. |
--- |
| 11247 |
if (WalkingBEDominatingConds) |
0 |
11247 |
if (WalkingBEDominatingConds) |
0 |
| 11248 |
return false; |
0 |
11248 |
return false; |
0 |
| 11249 |
|
--- |
11249 |
|
--- |
| 11250 |
SaveAndRestore ClearOnExit(WalkingBEDominatingConds, true); |
0 |
11250 |
SaveAndRestore ClearOnExit(WalkingBEDominatingConds, true); |
0 |
| 11251 |
|
--- |
11251 |
|
--- |
| 11252 |
// See if we can exploit a trip count to prove the predicate. |
--- |
11252 |
// See if we can exploit a trip count to prove the predicate. |
--- |
| 11253 |
const auto &BETakenInfo = getBackedgeTakenInfo(L); |
0 |
11253 |
const auto &BETakenInfo = getBackedgeTakenInfo(L); |
0 |
| 11254 |
const SCEV *LatchBECount = BETakenInfo.getExact(Latch, this); |
0 |
11254 |
const SCEV *LatchBECount = BETakenInfo.getExact(Latch, this); |
0 |
| 11255 |
if (LatchBECount != getCouldNotCompute()) { |
0 |
11255 |
if (LatchBECount != getCouldNotCompute()) { |
0 |
| 11256 |
// We know that Latch branches back to the loop header exactly |
--- |
11256 |
// We know that Latch branches back to the loop header exactly |
--- |
| 11257 |
// LatchBECount times. This means the backdege condition at Latch is |
--- |
11257 |
// LatchBECount times. This means the backdege condition at Latch is |
--- |
| 11258 |
// equivalent to "{0,+,1} u< LatchBECount". |
--- |
11258 |
// equivalent to "{0,+,1} u< LatchBECount". |
--- |
| 11259 |
Type *Ty = LatchBECount->getType(); |
0 |
11259 |
Type *Ty = LatchBECount->getType(); |
0 |
| 11260 |
auto NoWrapFlags = SCEV::NoWrapFlags(SCEV::FlagNUW | SCEV::FlagNW); |
0 |
11260 |
auto NoWrapFlags = SCEV::NoWrapFlags(SCEV::FlagNUW | SCEV::FlagNW); |
0 |
| 11261 |
const SCEV *LoopCounter = |
--- |
11261 |
const SCEV *LoopCounter = |
--- |
| 11262 |
getAddRecExpr(getZero(Ty), getOne(Ty), L, NoWrapFlags); |
0 |
11262 |
getAddRecExpr(getZero(Ty), getOne(Ty), L, NoWrapFlags); |
0 |
| 11263 |
if (isImpliedCond(Pred, LHS, RHS, ICmpInst::ICMP_ULT, LoopCounter, |
0 |
11263 |
if (isImpliedCond(Pred, LHS, RHS, ICmpInst::ICMP_ULT, LoopCounter, |
0 |
| 11264 |
LatchBECount)) |
--- |
11264 |
LatchBECount)) |
--- |
| 11265 |
return true; |
0 |
11265 |
return true; |
0 |
| 11266 |
} |
--- |
11266 |
} |
--- |
| 11267 |
|
--- |
11267 |
|
--- |
| 11268 |
// Check conditions due to any @llvm.assume intrinsics. |
--- |
11268 |
// Check conditions due to any @llvm.assume intrinsics. |
--- |
| 11269 |
for (auto &AssumeVH : AC.assumptions()) { |
0 |
11269 |
for (auto &AssumeVH : AC.assumptions()) { |
0 |
| 11270 |
if (!AssumeVH) |
0 |
11270 |
if (!AssumeVH) |
0 |
| 11271 |
continue; |
0 |
11271 |
continue; |
0 |
| 11272 |
auto *CI = cast(AssumeVH); |
0 |
11272 |
auto *CI = cast(AssumeVH); |
0 |
| 11273 |
if (!DT.dominates(CI, Latch->getTerminator())) |
0 |
11273 |
if (!DT.dominates(CI, Latch->getTerminator())) |
0 |
| 11274 |
continue; |
0 |
11274 |
continue; |
0 |
| 11275 |
|
--- |
11275 |
|
--- |
| 11276 |
if (isImpliedCond(Pred, LHS, RHS, CI->getArgOperand(0), false)) |
0 |
11276 |
if (isImpliedCond(Pred, LHS, RHS, CI->getArgOperand(0), false)) |
0 |
| 11277 |
return true; |
0 |
11277 |
return true; |
0 |
| 11278 |
} |
--- |
11278 |
} |
--- |
| 11279 |
|
--- |
11279 |
|
--- |
| 11280 |
if (isImpliedViaGuard(Latch, Pred, LHS, RHS)) |
0 |
11280 |
if (isImpliedViaGuard(Latch, Pred, LHS, RHS)) |
0 |
| 11281 |
return true; |
0 |
11281 |
return true; |
0 |
| 11282 |
|
--- |
11282 |
|
--- |
| 11283 |
for (DomTreeNode *DTN = DT[Latch], *HeaderDTN = DT[L->getHeader()]; |
0 |
11283 |
for (DomTreeNode *DTN = DT[Latch], *HeaderDTN = DT[L->getHeader()]; |
0 |
| 11284 |
DTN != HeaderDTN; DTN = DTN->getIDom()) { |
0 |
11284 |
DTN != HeaderDTN; DTN = DTN->getIDom()) { |
0 |
| 11285 |
assert(DTN && "should reach the loop header before reaching the root!"); |
0 |
11285 |
assert(DTN && "should reach the loop header before reaching the root!"); |
0 |
| 11286 |
|
--- |
11286 |
|
--- |
| 11287 |
BasicBlock *BB = DTN->getBlock(); |
0 |
11287 |
BasicBlock *BB = DTN->getBlock(); |
0 |
| 11288 |
if (isImpliedViaGuard(BB, Pred, LHS, RHS)) |
0 |
11288 |
if (isImpliedViaGuard(BB, Pred, LHS, RHS)) |
0 |
| 11289 |
return true; |
0 |
11289 |
return true; |
0 |
| 11290 |
|
--- |
11290 |
|
--- |
| 11291 |
BasicBlock *PBB = BB->getSinglePredecessor(); |
0 |
11291 |
BasicBlock *PBB = BB->getSinglePredecessor(); |
0 |
| 11292 |
if (!PBB) |
0 |
11292 |
if (!PBB) |
0 |
| 11293 |
continue; |
0 |
11293 |
continue; |
0 |
| 11294 |
|
--- |
11294 |
|
--- |
| 11295 |
BranchInst *ContinuePredicate = dyn_cast(PBB->getTerminator()); |
0 |
11295 |
BranchInst *ContinuePredicate = dyn_cast(PBB->getTerminator()); |
0 |
| 11296 |
if (!ContinuePredicate || !ContinuePredicate->isConditional()) |
0 |
11296 |
if (!ContinuePredicate || !ContinuePredicate->isConditional()) |
0 |
| 11297 |
continue; |
0 |
11297 |
continue; |
0 |
| 11298 |
|
--- |
11298 |
|
--- |
| 11299 |
Value *Condition = ContinuePredicate->getCondition(); |
0 |
11299 |
Value *Condition = ContinuePredicate->getCondition(); |
0 |
| 11300 |
|
--- |
11300 |
|
--- |
| 11301 |
// If we have an edge `E` within the loop body that dominates the only |
--- |
11301 |
// If we have an edge `E` within the loop body that dominates the only |
--- |
| 11302 |
// latch, the condition guarding `E` also guards the backedge. This |
--- |
11302 |
// latch, the condition guarding `E` also guards the backedge. This |
--- |
| 11303 |
// reasoning works only for loops with a single latch. |
--- |
11303 |
// reasoning works only for loops with a single latch. |
--- |
| 11304 |
|
--- |
11304 |
|
--- |
| 11305 |
BasicBlockEdge DominatingEdge(PBB, BB); |
0 |
11305 |
BasicBlockEdge DominatingEdge(PBB, BB); |
0 |
| 11306 |
if (DominatingEdge.isSingleEdge()) { |
0 |
11306 |
if (DominatingEdge.isSingleEdge()) { |
0 |
| 11307 |
// We're constructively (and conservatively) enumerating edges within the |
--- |
11307 |
// We're constructively (and conservatively) enumerating edges within the |
--- |
| 11308 |
// loop body that dominate the latch. The dominator tree better agree |
--- |
11308 |
// loop body that dominate the latch. The dominator tree better agree |
--- |
| 11309 |
// with us on this: |
--- |
11309 |
// with us on this: |
--- |
| 11310 |
assert(DT.dominates(DominatingEdge, Latch) && "should be!"); |
0 |
11310 |
assert(DT.dominates(DominatingEdge, Latch) && "should be!"); |
0 |
| 11311 |
|
--- |
11311 |
|
--- |
| 11312 |
if (isImpliedCond(Pred, LHS, RHS, Condition, |
0 |
11312 |
if (isImpliedCond(Pred, LHS, RHS, Condition, |
0 |
| 11313 |
BB != ContinuePredicate->getSuccessor(0))) |
0 |
11313 |
BB != ContinuePredicate->getSuccessor(0))) |
0 |
| 11314 |
return true; |
0 |
11314 |
return true; |
0 |
| 11315 |
} |
--- |
11315 |
} |
--- |
| 11316 |
} |
--- |
11316 |
} |
--- |
| 11317 |
|
--- |
11317 |
|
--- |
| 11318 |
return false; |
0 |
11318 |
return false; |
0 |
| 11319 |
} |
0 |
11319 |
} |
0 |
| 11320 |
|
--- |
11320 |
|
--- |
| 11321 |
bool ScalarEvolution::isBasicBlockEntryGuardedByCond(const BasicBlock *BB, |
0 |
11321 |
bool ScalarEvolution::isBasicBlockEntryGuardedByCond(const BasicBlock *BB, |
0 |
| 11322 |
ICmpInst::Predicate Pred, |
--- |
11322 |
ICmpInst::Predicate Pred, |
--- |
| 11323 |
const SCEV *LHS, |
--- |
11323 |
const SCEV *LHS, |
--- |
| 11324 |
const SCEV *RHS) { |
--- |
11324 |
const SCEV *RHS) { |
--- |
| 11325 |
// Do not bother proving facts for unreachable code. |
--- |
11325 |
// Do not bother proving facts for unreachable code. |
--- |
| 11326 |
if (!DT.isReachableFromEntry(BB)) |
0 |
11326 |
if (!DT.isReachableFromEntry(BB)) |
0 |
| 11327 |
return true; |
0 |
11327 |
return true; |
0 |
| 11328 |
if (VerifyIR) |
0 |
11328 |
if (VerifyIR) |
0 |
| 11329 |
assert(!verifyFunction(*BB->getParent(), &dbgs()) && |
0 |
11329 |
assert(!verifyFunction(*BB->getParent(), &dbgs()) && |
0 |
| 11330 |
"This cannot be done on broken IR!"); |
--- |
11330 |
"This cannot be done on broken IR!"); |
--- |
| 11331 |
|
--- |
11331 |
|
--- |
| 11332 |
// If we cannot prove strict comparison (e.g. a > b), maybe we can prove |
--- |
11332 |
// If we cannot prove strict comparison (e.g. a > b), maybe we can prove |
--- |
| 11333 |
// the facts (a >= b && a != b) separately. A typical situation is when the |
--- |
11333 |
// the facts (a >= b && a != b) separately. A typical situation is when the |
--- |
| 11334 |
// non-strict comparison is known from ranges and non-equality is known from |
--- |
11334 |
// non-strict comparison is known from ranges and non-equality is known from |
--- |
| 11335 |
// dominating predicates. If we are proving strict comparison, we always try |
--- |
11335 |
// dominating predicates. If we are proving strict comparison, we always try |
--- |
| 11336 |
// to prove non-equality and non-strict comparison separately. |
--- |
11336 |
// to prove non-equality and non-strict comparison separately. |
--- |
| 11337 |
auto NonStrictPredicate = ICmpInst::getNonStrictPredicate(Pred); |
0 |
11337 |
auto NonStrictPredicate = ICmpInst::getNonStrictPredicate(Pred); |
0 |
| 11338 |
const bool ProvingStrictComparison = (Pred != NonStrictPredicate); |
0 |
11338 |
const bool ProvingStrictComparison = (Pred != NonStrictPredicate); |
0 |
| 11339 |
bool ProvedNonStrictComparison = false; |
0 |
11339 |
bool ProvedNonStrictComparison = false; |
0 |
| 11340 |
bool ProvedNonEquality = false; |
0 |
11340 |
bool ProvedNonEquality = false; |
0 |
| 11341 |
|
--- |
11341 |
|
--- |
| 11342 |
auto SplitAndProve = |
--- |
11342 |
auto SplitAndProve = |
--- |
| 11343 |
[&](std::function Fn) -> bool { |
0 |
11343 |
[&](std::function Fn) -> bool { |
0 |
| 11344 |
if (!ProvedNonStrictComparison) |
0 |
11344 |
if (!ProvedNonStrictComparison) |
0 |
| 11345 |
ProvedNonStrictComparison = Fn(NonStrictPredicate); |
0 |
11345 |
ProvedNonStrictComparison = Fn(NonStrictPredicate); |
0 |
| 11346 |
if (!ProvedNonEquality) |
0 |
11346 |
if (!ProvedNonEquality) |
0 |
| 11347 |
ProvedNonEquality = Fn(ICmpInst::ICMP_NE); |
0 |
11347 |
ProvedNonEquality = Fn(ICmpInst::ICMP_NE); |
0 |
| 11348 |
if (ProvedNonStrictComparison && ProvedNonEquality) |
0 |
11348 |
if (ProvedNonStrictComparison && ProvedNonEquality) |
0 |
| 11349 |
return true; |
0 |
11349 |
return true; |
0 |
| 11350 |
return false; |
0 |
11350 |
return false; |
0 |
| 11351 |
}; |
0 |
11351 |
}; |
0 |
| 11352 |
|
--- |
11352 |
|
--- |
| 11353 |
if (ProvingStrictComparison) { |
0 |
11353 |
if (ProvingStrictComparison) { |
0 |
| 11354 |
auto ProofFn = [&](ICmpInst::Predicate P) { |
0 |
11354 |
auto ProofFn = [&](ICmpInst::Predicate P) { |
0 |
| 11355 |
return isKnownViaNonRecursiveReasoning(P, LHS, RHS); |
0 |
11355 |
return isKnownViaNonRecursiveReasoning(P, LHS, RHS); |
0 |
| 11356 |
}; |
0 |
11356 |
}; |
0 |
| 11357 |
if (SplitAndProve(ProofFn)) |
0 |
11357 |
if (SplitAndProve(ProofFn)) |
0 |
| 11358 |
return true; |
0 |
11358 |
return true; |
0 |
| 11359 |
} |
--- |
11359 |
} |
--- |
| 11360 |
|
--- |
11360 |
|
--- |
| 11361 |
// Try to prove (Pred, LHS, RHS) using isImpliedCond. |
--- |
11361 |
// Try to prove (Pred, LHS, RHS) using isImpliedCond. |
--- |
| 11362 |
auto ProveViaCond = [&](const Value *Condition, bool Inverse) { |
0 |
11362 |
auto ProveViaCond = [&](const Value *Condition, bool Inverse) { |
0 |
| 11363 |
const Instruction *CtxI = &BB->front(); |
0 |
11363 |
const Instruction *CtxI = &BB->front(); |
0 |
| 11364 |
if (isImpliedCond(Pred, LHS, RHS, Condition, Inverse, CtxI)) |
0 |
11364 |
if (isImpliedCond(Pred, LHS, RHS, Condition, Inverse, CtxI)) |
0 |
| 11365 |
return true; |
0 |
11365 |
return true; |
0 |
| 11366 |
if (ProvingStrictComparison) { |
0 |
11366 |
if (ProvingStrictComparison) { |
0 |
| 11367 |
auto ProofFn = [&](ICmpInst::Predicate P) { |
0 |
11367 |
auto ProofFn = [&](ICmpInst::Predicate P) { |
0 |
| 11368 |
return isImpliedCond(P, LHS, RHS, Condition, Inverse, CtxI); |
0 |
11368 |
return isImpliedCond(P, LHS, RHS, Condition, Inverse, CtxI); |
0 |
| 11369 |
}; |
0 |
11369 |
}; |
0 |
| 11370 |
if (SplitAndProve(ProofFn)) |
0 |
11370 |
if (SplitAndProve(ProofFn)) |
0 |
| 11371 |
return true; |
0 |
11371 |
return true; |
0 |
| 11372 |
} |
--- |
11372 |
} |
--- |
| 11373 |
return false; |
0 |
11373 |
return false; |
0 |
| 11374 |
}; |
0 |
11374 |
}; |
0 |
| 11375 |
|
--- |
11375 |
|
--- |
| 11376 |
// Starting at the block's predecessor, climb up the predecessor chain, as long |
--- |
11376 |
// Starting at the block's predecessor, climb up the predecessor chain, as long |
--- |
| 11377 |
// as there are predecessors that can be found that have unique successors |
--- |
11377 |
// as there are predecessors that can be found that have unique successors |
--- |
| 11378 |
// leading to the original block. |
--- |
11378 |
// leading to the original block. |
--- |
| 11379 |
const Loop *ContainingLoop = LI.getLoopFor(BB); |
0 |
11379 |
const Loop *ContainingLoop = LI.getLoopFor(BB); |
0 |
| 11380 |
const BasicBlock *PredBB; |
--- |
11380 |
const BasicBlock *PredBB; |
--- |
| 11381 |
if (ContainingLoop && ContainingLoop->getHeader() == BB) |
0 |
11381 |
if (ContainingLoop && ContainingLoop->getHeader() == BB) |
0 |
| 11382 |
PredBB = ContainingLoop->getLoopPredecessor(); |
0 |
11382 |
PredBB = ContainingLoop->getLoopPredecessor(); |
0 |
| 11383 |
else |
--- |
11383 |
else |
--- |
| 11384 |
PredBB = BB->getSinglePredecessor(); |
0 |
11384 |
PredBB = BB->getSinglePredecessor(); |
0 |
| 11385 |
for (std::pair Pair(PredBB, BB); |
0 |
11385 |
for (std::pair Pair(PredBB, BB); |
0 |
| 11386 |
Pair.first; Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) { |
0 |
11386 |
Pair.first; Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) { |
0 |
| 11387 |
const BranchInst *BlockEntryPredicate = |
--- |
11387 |
const BranchInst *BlockEntryPredicate = |
--- |
| 11388 |
dyn_cast(Pair.first->getTerminator()); |
0 |
11388 |
dyn_cast(Pair.first->getTerminator()); |
0 |
| 11389 |
if (!BlockEntryPredicate || BlockEntryPredicate->isUnconditional()) |
0 |
11389 |
if (!BlockEntryPredicate || BlockEntryPredicate->isUnconditional()) |
0 |
| 11390 |
continue; |
0 |
11390 |
continue; |
0 |
| 11391 |
|
--- |
11391 |
|
--- |
| 11392 |
if (ProveViaCond(BlockEntryPredicate->getCondition(), |
0 |
11392 |
if (ProveViaCond(BlockEntryPredicate->getCondition(), |
0 |
| 11393 |
BlockEntryPredicate->getSuccessor(0) != Pair.second)) |
0 |
11393 |
BlockEntryPredicate->getSuccessor(0) != Pair.second)) |
0 |
| 11394 |
return true; |
0 |
11394 |
return true; |
0 |
| 11395 |
} |
--- |
11395 |
} |
--- |
| 11396 |
|
--- |
11396 |
|
--- |
| 11397 |
// Check conditions due to any @llvm.assume intrinsics. |
--- |
11397 |
// Check conditions due to any @llvm.assume intrinsics. |
--- |
| 11398 |
for (auto &AssumeVH : AC.assumptions()) { |
0 |
11398 |
for (auto &AssumeVH : AC.assumptions()) { |
0 |
| 11399 |
if (!AssumeVH) |
0 |
11399 |
if (!AssumeVH) |
0 |
| 11400 |
continue; |
0 |
11400 |
continue; |
0 |
| 11401 |
auto *CI = cast(AssumeVH); |
0 |
11401 |
auto *CI = cast(AssumeVH); |
0 |
| 11402 |
if (!DT.dominates(CI, BB)) |
0 |
11402 |
if (!DT.dominates(CI, BB)) |
0 |
| 11403 |
continue; |
0 |
11403 |
continue; |
0 |
| 11404 |
|
--- |
11404 |
|
--- |
| 11405 |
if (ProveViaCond(CI->getArgOperand(0), false)) |
0 |
11405 |
if (ProveViaCond(CI->getArgOperand(0), false)) |
0 |
| 11406 |
return true; |
0 |
11406 |
return true; |
0 |
| 11407 |
} |
--- |
11407 |
} |
--- |
| 11408 |
|
--- |
11408 |
|
--- |
| 11409 |
// Check conditions due to any @llvm.experimental.guard intrinsics. |
--- |
11409 |
// Check conditions due to any @llvm.experimental.guard intrinsics. |
--- |
| 11410 |
auto *GuardDecl = F.getParent()->getFunction( |
0 |
11410 |
auto *GuardDecl = F.getParent()->getFunction( |
0 |
| 11411 |
Intrinsic::getName(Intrinsic::experimental_guard)); |
--- |
11411 |
Intrinsic::getName(Intrinsic::experimental_guard)); |
--- |
| 11412 |
if (GuardDecl) |
0 |
11412 |
if (GuardDecl) |
0 |
| 11413 |
for (const auto *GU : GuardDecl->users()) |
0 |
11413 |
for (const auto *GU : GuardDecl->users()) |
0 |
| 11414 |
if (const auto *Guard = dyn_cast(GU)) |
0 |
11414 |
if (const auto *Guard = dyn_cast(GU)) |
0 |
| 11415 |
if (Guard->getFunction() == BB->getParent() && DT.dominates(Guard, BB)) |
0 |
11415 |
if (Guard->getFunction() == BB->getParent() && DT.dominates(Guard, BB)) |
0 |
| 11416 |
if (ProveViaCond(Guard->getArgOperand(0), false)) |
0 |
11416 |
if (ProveViaCond(Guard->getArgOperand(0), false)) |
0 |
| 11417 |
return true; |
0 |
11417 |
return true; |
0 |
| 11418 |
return false; |
0 |
11418 |
return false; |
0 |
| 11419 |
} |
--- |
11419 |
} |
--- |
| 11420 |
|
--- |
11420 |
|
--- |
| 11421 |
bool ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L, |
0 |
11421 |
bool ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L, |
0 |
| 11422 |
ICmpInst::Predicate Pred, |
--- |
11422 |
ICmpInst::Predicate Pred, |
--- |
| 11423 |
const SCEV *LHS, |
--- |
11423 |
const SCEV *LHS, |
--- |
| 11424 |
const SCEV *RHS) { |
--- |
11424 |
const SCEV *RHS) { |
--- |
| 11425 |
// Interpret a null as meaning no loop, where there is obviously no guard |
--- |
11425 |
// Interpret a null as meaning no loop, where there is obviously no guard |
--- |
| 11426 |
// (interprocedural conditions notwithstanding). |
--- |
11426 |
// (interprocedural conditions notwithstanding). |
--- |
| 11427 |
if (!L) |
0 |
11427 |
if (!L) |
0 |
| 11428 |
return false; |
0 |
11428 |
return false; |
0 |
| 11429 |
|
--- |
11429 |
|
--- |
| 11430 |
// Both LHS and RHS must be available at loop entry. |
--- |
11430 |
// Both LHS and RHS must be available at loop entry. |
--- |
| 11431 |
assert(isAvailableAtLoopEntry(LHS, L) && |
0 |
11431 |
assert(isAvailableAtLoopEntry(LHS, L) && |
0 |
| 11432 |
"LHS is not available at Loop Entry"); |
--- |
11432 |
"LHS is not available at Loop Entry"); |
--- |
| 11433 |
assert(isAvailableAtLoopEntry(RHS, L) && |
0 |
11433 |
assert(isAvailableAtLoopEntry(RHS, L) && |
0 |
| 11434 |
"RHS is not available at Loop Entry"); |
--- |
11434 |
"RHS is not available at Loop Entry"); |
--- |
| 11435 |
|
--- |
11435 |
|
--- |
| 11436 |
if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS)) |
0 |
11436 |
if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS)) |
0 |
| 11437 |
return true; |
0 |
11437 |
return true; |
0 |
| 11438 |
|
--- |
11438 |
|
--- |
| 11439 |
return isBasicBlockEntryGuardedByCond(L->getHeader(), Pred, LHS, RHS); |
0 |
11439 |
return isBasicBlockEntryGuardedByCond(L->getHeader(), Pred, LHS, RHS); |
0 |
| 11440 |
} |
--- |
11440 |
} |
--- |
| 11441 |
|
--- |
11441 |
|
--- |
| 11442 |
bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS, |
0 |
11442 |
bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS, |
0 |
| 11443 |
const SCEV *RHS, |
--- |
11443 |
const SCEV *RHS, |
--- |
| 11444 |
const Value *FoundCondValue, bool Inverse, |
--- |
11444 |
const Value *FoundCondValue, bool Inverse, |
--- |
| 11445 |
const Instruction *CtxI) { |
--- |
11445 |
const Instruction *CtxI) { |
--- |
| 11446 |
// False conditions implies anything. Do not bother analyzing it further. |
--- |
11446 |
// False conditions implies anything. Do not bother analyzing it further. |
--- |
| 11447 |
if (FoundCondValue == |
0 |
11447 |
if (FoundCondValue == |
0 |
| 11448 |
ConstantInt::getBool(FoundCondValue->getContext(), Inverse)) |
0 |
11448 |
ConstantInt::getBool(FoundCondValue->getContext(), Inverse)) |
0 |
| 11449 |
return true; |
0 |
11449 |
return true; |
0 |
| 11450 |
|
--- |
11450 |
|
--- |
| 11451 |
if (!PendingLoopPredicates.insert(FoundCondValue).second) |
0 |
11451 |
if (!PendingLoopPredicates.insert(FoundCondValue).second) |
0 |
| 11452 |
return false; |
0 |
11452 |
return false; |
0 |
| 11453 |
|
--- |
11453 |
|
--- |
| 11454 |
auto ClearOnExit = |
--- |
11454 |
auto ClearOnExit = |
--- |
| 11455 |
make_scope_exit([&]() { PendingLoopPredicates.erase(FoundCondValue); }); |
0 |
11455 |
make_scope_exit([&]() { PendingLoopPredicates.erase(FoundCondValue); }); |
0 |
| 11456 |
|
--- |
11456 |
|
--- |
| 11457 |
// Recursively handle And and Or conditions. |
--- |
11457 |
// Recursively handle And and Or conditions. |
--- |
| 11458 |
const Value *Op0, *Op1; |
--- |
11458 |
const Value *Op0, *Op1; |
--- |
| 11459 |
if (match(FoundCondValue, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))) { |
0 |
11459 |
if (match(FoundCondValue, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))) { |
0 |
| 11460 |
if (!Inverse) |
0 |
11460 |
if (!Inverse) |
0 |
| 11461 |
return isImpliedCond(Pred, LHS, RHS, Op0, Inverse, CtxI) || |
0 |
11461 |
return isImpliedCond(Pred, LHS, RHS, Op0, Inverse, CtxI) || |
0 |
| 11462 |
isImpliedCond(Pred, LHS, RHS, Op1, Inverse, CtxI); |
0 |
11462 |
isImpliedCond(Pred, LHS, RHS, Op1, Inverse, CtxI); |
0 |
| 11463 |
} else if (match(FoundCondValue, m_LogicalOr(m_Value(Op0), m_Value(Op1)))) { |
0 |
11463 |
} else if (match(FoundCondValue, m_LogicalOr(m_Value(Op0), m_Value(Op1)))) { |
0 |
| 11464 |
if (Inverse) |
0 |
11464 |
if (Inverse) |
0 |
| 11465 |
return isImpliedCond(Pred, LHS, RHS, Op0, Inverse, CtxI) || |
0 |
11465 |
return isImpliedCond(Pred, LHS, RHS, Op0, Inverse, CtxI) || |
0 |
| 11466 |
isImpliedCond(Pred, LHS, RHS, Op1, Inverse, CtxI); |
0 |
11466 |
isImpliedCond(Pred, LHS, RHS, Op1, Inverse, CtxI); |
0 |
| 11467 |
} |
--- |
11467 |
} |
--- |
| 11468 |
|
--- |
11468 |
|
--- |
| 11469 |
const ICmpInst *ICI = dyn_cast(FoundCondValue); |
0 |
11469 |
const ICmpInst *ICI = dyn_cast(FoundCondValue); |
0 |
| 11470 |
if (!ICI) return false; |
0 |
11470 |
if (!ICI) return false; |
0 |
| 11471 |
|
--- |
11471 |
|
--- |
| 11472 |
// Now that we found a conditional branch that dominates the loop or controls |
--- |
11472 |
// Now that we found a conditional branch that dominates the loop or controls |
--- |
| 11473 |
// the loop latch. Check to see if it is the comparison we are looking for. |
--- |
11473 |
// the loop latch. Check to see if it is the comparison we are looking for. |
--- |
| 11474 |
ICmpInst::Predicate FoundPred; |
--- |
11474 |
ICmpInst::Predicate FoundPred; |
--- |
| 11475 |
if (Inverse) |
0 |
11475 |
if (Inverse) |
0 |
| 11476 |
FoundPred = ICI->getInversePredicate(); |
0 |
11476 |
FoundPred = ICI->getInversePredicate(); |
0 |
| 11477 |
else |
--- |
11477 |
else |
--- |
| 11478 |
FoundPred = ICI->getPredicate(); |
0 |
11478 |
FoundPred = ICI->getPredicate(); |
0 |
| 11479 |
|
--- |
11479 |
|
--- |
| 11480 |
const SCEV *FoundLHS = getSCEV(ICI->getOperand(0)); |
0 |
11480 |
const SCEV *FoundLHS = getSCEV(ICI->getOperand(0)); |
0 |
| 11481 |
const SCEV *FoundRHS = getSCEV(ICI->getOperand(1)); |
0 |
11481 |
const SCEV *FoundRHS = getSCEV(ICI->getOperand(1)); |
0 |
| 11482 |
|
--- |
11482 |
|
--- |
| 11483 |
return isImpliedCond(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS, CtxI); |
0 |
11483 |
return isImpliedCond(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS, CtxI); |
0 |
| 11484 |
} |
0 |
11484 |
} |
0 |
| 11485 |
|
--- |
11485 |
|
--- |
| 11486 |
bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS, |
0 |
11486 |
bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS, |
0 |
| 11487 |
const SCEV *RHS, |
--- |
11487 |
const SCEV *RHS, |
--- |
| 11488 |
ICmpInst::Predicate FoundPred, |
--- |
11488 |
ICmpInst::Predicate FoundPred, |
--- |
| 11489 |
const SCEV *FoundLHS, const SCEV *FoundRHS, |
--- |
11489 |
const SCEV *FoundLHS, const SCEV *FoundRHS, |
--- |
| 11490 |
const Instruction *CtxI) { |
--- |
11490 |
const Instruction *CtxI) { |
--- |
| 11491 |
// Balance the types. |
--- |
11491 |
// Balance the types. |
--- |
| 11492 |
if (getTypeSizeInBits(LHS->getType()) < |
0 |
11492 |
if (getTypeSizeInBits(LHS->getType()) < |
0 |
| 11493 |
getTypeSizeInBits(FoundLHS->getType())) { |
0 |
11493 |
getTypeSizeInBits(FoundLHS->getType())) { |
0 |
| 11494 |
// For unsigned and equality predicates, try to prove that both found |
--- |
11494 |
// For unsigned and equality predicates, try to prove that both found |
--- |
| 11495 |
// operands fit into narrow unsigned range. If so, try to prove facts in |
--- |
11495 |
// operands fit into narrow unsigned range. If so, try to prove facts in |
--- |
| 11496 |
// narrow types. |
--- |
11496 |
// narrow types. |
--- |
| 11497 |
if (!CmpInst::isSigned(FoundPred) && !FoundLHS->getType()->isPointerTy() && |
0 |
11497 |
if (!CmpInst::isSigned(FoundPred) && !FoundLHS->getType()->isPointerTy() && |
0 |
| 11498 |
!FoundRHS->getType()->isPointerTy()) { |
0 |
11498 |
!FoundRHS->getType()->isPointerTy()) { |
0 |
| 11499 |
auto *NarrowType = LHS->getType(); |
0 |
11499 |
auto *NarrowType = LHS->getType(); |
0 |
| 11500 |
auto *WideType = FoundLHS->getType(); |
0 |
11500 |
auto *WideType = FoundLHS->getType(); |
0 |
| 11501 |
auto BitWidth = getTypeSizeInBits(NarrowType); |
0 |
11501 |
auto BitWidth = getTypeSizeInBits(NarrowType); |
0 |
| 11502 |
const SCEV *MaxValue = getZeroExtendExpr( |
0 |
11502 |
const SCEV *MaxValue = getZeroExtendExpr( |
0 |
| 11503 |
getConstant(APInt::getMaxValue(BitWidth)), WideType); |
0 |
11503 |
getConstant(APInt::getMaxValue(BitWidth)), WideType); |
0 |
| 11504 |
if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, FoundLHS, |
0 |
11504 |
if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, FoundLHS, |
0 |
| 11505 |
MaxValue) && |
0 |
11505 |
MaxValue) && |
0 |
| 11506 |
isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, FoundRHS, |
0 |
11506 |
isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, FoundRHS, |
0 |
| 11507 |
MaxValue)) { |
--- |
11507 |
MaxValue)) { |
--- |
| 11508 |
const SCEV *TruncFoundLHS = getTruncateExpr(FoundLHS, NarrowType); |
0 |
11508 |
const SCEV *TruncFoundLHS = getTruncateExpr(FoundLHS, NarrowType); |
0 |
| 11509 |
const SCEV *TruncFoundRHS = getTruncateExpr(FoundRHS, NarrowType); |
0 |
11509 |
const SCEV *TruncFoundRHS = getTruncateExpr(FoundRHS, NarrowType); |
0 |
| 11510 |
if (isImpliedCondBalancedTypes(Pred, LHS, RHS, FoundPred, TruncFoundLHS, |
0 |
11510 |
if (isImpliedCondBalancedTypes(Pred, LHS, RHS, FoundPred, TruncFoundLHS, |
0 |
| 11511 |
TruncFoundRHS, CtxI)) |
--- |
11511 |
TruncFoundRHS, CtxI)) |
--- |
| 11512 |
return true; |
0 |
11512 |
return true; |
0 |
| 11513 |
} |
--- |
11513 |
} |
--- |
| 11514 |
} |
--- |
11514 |
} |
--- |
| 11515 |
|
--- |
11515 |
|
--- |
| 11516 |
if (LHS->getType()->isPointerTy() || RHS->getType()->isPointerTy()) |
0 |
11516 |
if (LHS->getType()->isPointerTy() || RHS->getType()->isPointerTy()) |
0 |
| 11517 |
return false; |
0 |
11517 |
return false; |
0 |
| 11518 |
if (CmpInst::isSigned(Pred)) { |
0 |
11518 |
if (CmpInst::isSigned(Pred)) { |
0 |
| 11519 |
LHS = getSignExtendExpr(LHS, FoundLHS->getType()); |
0 |
11519 |
LHS = getSignExtendExpr(LHS, FoundLHS->getType()); |
0 |
| 11520 |
RHS = getSignExtendExpr(RHS, FoundLHS->getType()); |
0 |
11520 |
RHS = getSignExtendExpr(RHS, FoundLHS->getType()); |
0 |
| 11521 |
} else { |
--- |
11521 |
} else { |
--- |
| 11522 |
LHS = getZeroExtendExpr(LHS, FoundLHS->getType()); |
0 |
11522 |
LHS = getZeroExtendExpr(LHS, FoundLHS->getType()); |
0 |
| 11523 |
RHS = getZeroExtendExpr(RHS, FoundLHS->getType()); |
0 |
11523 |
RHS = getZeroExtendExpr(RHS, FoundLHS->getType()); |
0 |
| 11524 |
} |
--- |
11524 |
} |
--- |
| 11525 |
} else if (getTypeSizeInBits(LHS->getType()) > |
0 |
11525 |
} else if (getTypeSizeInBits(LHS->getType()) > |
0 |
| 11526 |
getTypeSizeInBits(FoundLHS->getType())) { |
0 |
11526 |
getTypeSizeInBits(FoundLHS->getType())) { |
0 |
| 11527 |
if (FoundLHS->getType()->isPointerTy() || FoundRHS->getType()->isPointerTy()) |
0 |
11527 |
if (FoundLHS->getType()->isPointerTy() || FoundRHS->getType()->isPointerTy()) |
0 |
| 11528 |
return false; |
0 |
11528 |
return false; |
0 |
| 11529 |
if (CmpInst::isSigned(FoundPred)) { |
0 |
11529 |
if (CmpInst::isSigned(FoundPred)) { |
0 |
| 11530 |
FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType()); |
0 |
11530 |
FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType()); |
0 |
| 11531 |
FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType()); |
0 |
11531 |
FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType()); |
0 |
| 11532 |
} else { |
--- |
11532 |
} else { |
--- |
| 11533 |
FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType()); |
0 |
11533 |
FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType()); |
0 |
| 11534 |
FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType()); |
0 |
11534 |
FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType()); |
0 |
| 11535 |
} |
--- |
11535 |
} |
--- |
| 11536 |
} |
--- |
11536 |
} |
--- |
| 11537 |
return isImpliedCondBalancedTypes(Pred, LHS, RHS, FoundPred, FoundLHS, |
0 |
11537 |
return isImpliedCondBalancedTypes(Pred, LHS, RHS, FoundPred, FoundLHS, |
0 |
| 11538 |
FoundRHS, CtxI); |
0 |
11538 |
FoundRHS, CtxI); |
0 |
| 11539 |
} |
--- |
11539 |
} |
--- |
| 11540 |
|
--- |
11540 |
|
--- |
| 11541 |
bool ScalarEvolution::isImpliedCondBalancedTypes( |
0 |
11541 |
bool ScalarEvolution::isImpliedCondBalancedTypes( |
0 |
| 11542 |
ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, |
--- |
11542 |
ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, |
--- |
| 11543 |
ICmpInst::Predicate FoundPred, const SCEV *FoundLHS, const SCEV *FoundRHS, |
--- |
11543 |
ICmpInst::Predicate FoundPred, const SCEV *FoundLHS, const SCEV *FoundRHS, |
--- |
| 11544 |
const Instruction *CtxI) { |
--- |
11544 |
const Instruction *CtxI) { |
--- |
| 11545 |
assert(getTypeSizeInBits(LHS->getType()) == |
0 |
11545 |
assert(getTypeSizeInBits(LHS->getType()) == |
0 |
| 11546 |
getTypeSizeInBits(FoundLHS->getType()) && |
--- |
11546 |
getTypeSizeInBits(FoundLHS->getType()) && |
--- |
| 11547 |
"Types should be balanced!"); |
--- |
11547 |
"Types should be balanced!"); |
--- |
| 11548 |
// Canonicalize the query to match the way instcombine will have |
--- |
11548 |
// Canonicalize the query to match the way instcombine will have |
--- |
| 11549 |
// canonicalized the comparison. |
--- |
11549 |
// canonicalized the comparison. |
--- |
| 11550 |
if (SimplifyICmpOperands(Pred, LHS, RHS)) |
0 |
11550 |
if (SimplifyICmpOperands(Pred, LHS, RHS)) |
0 |
| 11551 |
if (LHS == RHS) |
0 |
11551 |
if (LHS == RHS) |
0 |
| 11552 |
return CmpInst::isTrueWhenEqual(Pred); |
0 |
11552 |
return CmpInst::isTrueWhenEqual(Pred); |
0 |
| 11553 |
if (SimplifyICmpOperands(FoundPred, FoundLHS, FoundRHS)) |
0 |
11553 |
if (SimplifyICmpOperands(FoundPred, FoundLHS, FoundRHS)) |
0 |
| 11554 |
if (FoundLHS == FoundRHS) |
0 |
11554 |
if (FoundLHS == FoundRHS) |
0 |
| 11555 |
return CmpInst::isFalseWhenEqual(FoundPred); |
0 |
11555 |
return CmpInst::isFalseWhenEqual(FoundPred); |
0 |
| 11556 |
|
--- |
11556 |
|
--- |
| 11557 |
// Check to see if we can make the LHS or RHS match. |
--- |
11557 |
// Check to see if we can make the LHS or RHS match. |
--- |
| 11558 |
if (LHS == FoundRHS || RHS == FoundLHS) { |
0 |
11558 |
if (LHS == FoundRHS || RHS == FoundLHS) { |
0 |
| 11559 |
if (isa(RHS)) { |
0 |
11559 |
if (isa(RHS)) { |
0 |
| 11560 |
std::swap(FoundLHS, FoundRHS); |
0 |
11560 |
std::swap(FoundLHS, FoundRHS); |
0 |
| 11561 |
FoundPred = ICmpInst::getSwappedPredicate(FoundPred); |
0 |
11561 |
FoundPred = ICmpInst::getSwappedPredicate(FoundPred); |
0 |
| 11562 |
} else { |
--- |
11562 |
} else { |
--- |
| 11563 |
std::swap(LHS, RHS); |
0 |
11563 |
std::swap(LHS, RHS); |
0 |
| 11564 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
11564 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
| 11565 |
} |
--- |
11565 |
} |
--- |
| 11566 |
} |
--- |
11566 |
} |
--- |
| 11567 |
|
--- |
11567 |
|
--- |
| 11568 |
// Check whether the found predicate is the same as the desired predicate. |
--- |
11568 |
// Check whether the found predicate is the same as the desired predicate. |
--- |
| 11569 |
if (FoundPred == Pred) |
0 |
11569 |
if (FoundPred == Pred) |
0 |
| 11570 |
return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI); |
0 |
11570 |
return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI); |
0 |
| 11571 |
|
--- |
11571 |
|
--- |
| 11572 |
// Check whether swapping the found predicate makes it the same as the |
--- |
11572 |
// Check whether swapping the found predicate makes it the same as the |
--- |
| 11573 |
// desired predicate. |
--- |
11573 |
// desired predicate. |
--- |
| 11574 |
if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) { |
0 |
11574 |
if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) { |
0 |
| 11575 |
// We can write the implication |
--- |
11575 |
// We can write the implication |
--- |
| 11576 |
// 0. LHS Pred RHS <- FoundLHS SwapPred FoundRHS |
--- |
11576 |
// 0. LHS Pred RHS <- FoundLHS SwapPred FoundRHS |
--- |
| 11577 |
// using one of the following ways: |
--- |
11577 |
// using one of the following ways: |
--- |
| 11578 |
// 1. LHS Pred RHS <- FoundRHS Pred FoundLHS |
--- |
11578 |
// 1. LHS Pred RHS <- FoundRHS Pred FoundLHS |
--- |
| 11579 |
// 2. RHS SwapPred LHS <- FoundLHS SwapPred FoundRHS |
--- |
11579 |
// 2. RHS SwapPred LHS <- FoundLHS SwapPred FoundRHS |
--- |
| 11580 |
// 3. LHS Pred RHS <- ~FoundLHS Pred ~FoundRHS |
--- |
11580 |
// 3. LHS Pred RHS <- ~FoundLHS Pred ~FoundRHS |
--- |
| 11581 |
// 4. ~LHS SwapPred ~RHS <- FoundLHS SwapPred FoundRHS |
--- |
11581 |
// 4. ~LHS SwapPred ~RHS <- FoundLHS SwapPred FoundRHS |
--- |
| 11582 |
// Forms 1. and 2. require swapping the operands of one condition. Don't |
--- |
11582 |
// Forms 1. and 2. require swapping the operands of one condition. Don't |
--- |
| 11583 |
// do this if it would break canonical constant/addrec ordering. |
--- |
11583 |
// do this if it would break canonical constant/addrec ordering. |
--- |
| 11584 |
if (!isa(RHS) && !isa(LHS)) |
0 |
11584 |
if (!isa(RHS) && !isa(LHS)) |
0 |
| 11585 |
return isImpliedCondOperands(FoundPred, RHS, LHS, FoundLHS, FoundRHS, |
0 |
11585 |
return isImpliedCondOperands(FoundPred, RHS, LHS, FoundLHS, FoundRHS, |
0 |
| 11586 |
CtxI); |
0 |
11586 |
CtxI); |
0 |
| 11587 |
if (!isa(FoundRHS) && !isa(FoundLHS)) |
0 |
11587 |
if (!isa(FoundRHS) && !isa(FoundLHS)) |
0 |
| 11588 |
return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS, CtxI); |
0 |
11588 |
return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS, CtxI); |
0 |
| 11589 |
|
--- |
11589 |
|
--- |
| 11590 |
// There's no clear preference between forms 3. and 4., try both. Avoid |
--- |
11590 |
// There's no clear preference between forms 3. and 4., try both. Avoid |
--- |
| 11591 |
// forming getNotSCEV of pointer values as the resulting subtract is |
--- |
11591 |
// forming getNotSCEV of pointer values as the resulting subtract is |
--- |
| 11592 |
// not legal. |
--- |
11592 |
// not legal. |
--- |
| 11593 |
if (!LHS->getType()->isPointerTy() && !RHS->getType()->isPointerTy() && |
0 |
11593 |
if (!LHS->getType()->isPointerTy() && !RHS->getType()->isPointerTy() && |
0 |
| 11594 |
isImpliedCondOperands(FoundPred, getNotSCEV(LHS), getNotSCEV(RHS), |
0 |
11594 |
isImpliedCondOperands(FoundPred, getNotSCEV(LHS), getNotSCEV(RHS), |
0 |
| 11595 |
FoundLHS, FoundRHS, CtxI)) |
--- |
11595 |
FoundLHS, FoundRHS, CtxI)) |
--- |
| 11596 |
return true; |
0 |
11596 |
return true; |
0 |
| 11597 |
|
--- |
11597 |
|
--- |
| 11598 |
if (!FoundLHS->getType()->isPointerTy() && |
0 |
11598 |
if (!FoundLHS->getType()->isPointerTy() && |
0 |
| 11599 |
!FoundRHS->getType()->isPointerTy() && |
0 |
11599 |
!FoundRHS->getType()->isPointerTy() && |
0 |
| 11600 |
isImpliedCondOperands(Pred, LHS, RHS, getNotSCEV(FoundLHS), |
0 |
11600 |
isImpliedCondOperands(Pred, LHS, RHS, getNotSCEV(FoundLHS), |
0 |
| 11601 |
getNotSCEV(FoundRHS), CtxI)) |
--- |
11601 |
getNotSCEV(FoundRHS), CtxI)) |
--- |
| 11602 |
return true; |
0 |
11602 |
return true; |
0 |
| 11603 |
|
--- |
11603 |
|
--- |
| 11604 |
return false; |
0 |
11604 |
return false; |
0 |
| 11605 |
} |
--- |
11605 |
} |
--- |
| 11606 |
|
--- |
11606 |
|
--- |
| 11607 |
auto IsSignFlippedPredicate = [](CmpInst::Predicate P1, |
0 |
11607 |
auto IsSignFlippedPredicate = [](CmpInst::Predicate P1, |
0 |
| 11608 |
CmpInst::Predicate P2) { |
--- |
11608 |
CmpInst::Predicate P2) { |
--- |
| 11609 |
assert(P1 != P2 && "Handled earlier!"); |
0 |
11609 |
assert(P1 != P2 && "Handled earlier!"); |
0 |
| 11610 |
return CmpInst::isRelational(P2) && |
0 |
11610 |
return CmpInst::isRelational(P2) && |
0 |
| 11611 |
P1 == CmpInst::getFlippedSignednessPredicate(P2); |
0 |
11611 |
P1 == CmpInst::getFlippedSignednessPredicate(P2); |
0 |
| 11612 |
}; |
--- |
11612 |
}; |
--- |
| 11613 |
if (IsSignFlippedPredicate(Pred, FoundPred)) { |
0 |
11613 |
if (IsSignFlippedPredicate(Pred, FoundPred)) { |
0 |
| 11614 |
// Unsigned comparison is the same as signed comparison when both the |
--- |
11614 |
// Unsigned comparison is the same as signed comparison when both the |
--- |
| 11615 |
// operands are non-negative or negative. |
--- |
11615 |
// operands are non-negative or negative. |
--- |
| 11616 |
if ((isKnownNonNegative(FoundLHS) && isKnownNonNegative(FoundRHS)) || |
0 |
11616 |
if ((isKnownNonNegative(FoundLHS) && isKnownNonNegative(FoundRHS)) || |
0 |
| 11617 |
(isKnownNegative(FoundLHS) && isKnownNegative(FoundRHS))) |
0 |
11617 |
(isKnownNegative(FoundLHS) && isKnownNegative(FoundRHS))) |
0 |
| 11618 |
return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI); |
0 |
11618 |
return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI); |
0 |
| 11619 |
// Create local copies that we can freely swap and canonicalize our |
--- |
11619 |
// Create local copies that we can freely swap and canonicalize our |
--- |
| 11620 |
// conditions to "le/lt". |
--- |
11620 |
// conditions to "le/lt". |
--- |
| 11621 |
ICmpInst::Predicate CanonicalPred = Pred, CanonicalFoundPred = FoundPred; |
0 |
11621 |
ICmpInst::Predicate CanonicalPred = Pred, CanonicalFoundPred = FoundPred; |
0 |
| 11622 |
const SCEV *CanonicalLHS = LHS, *CanonicalRHS = RHS, |
0 |
11622 |
const SCEV *CanonicalLHS = LHS, *CanonicalRHS = RHS, |
0 |
| 11623 |
*CanonicalFoundLHS = FoundLHS, *CanonicalFoundRHS = FoundRHS; |
0 |
11623 |
*CanonicalFoundLHS = FoundLHS, *CanonicalFoundRHS = FoundRHS; |
0 |
| 11624 |
if (ICmpInst::isGT(CanonicalPred) || ICmpInst::isGE(CanonicalPred)) { |
0 |
11624 |
if (ICmpInst::isGT(CanonicalPred) || ICmpInst::isGE(CanonicalPred)) { |
0 |
| 11625 |
CanonicalPred = ICmpInst::getSwappedPredicate(CanonicalPred); |
0 |
11625 |
CanonicalPred = ICmpInst::getSwappedPredicate(CanonicalPred); |
0 |
| 11626 |
CanonicalFoundPred = ICmpInst::getSwappedPredicate(CanonicalFoundPred); |
0 |
11626 |
CanonicalFoundPred = ICmpInst::getSwappedPredicate(CanonicalFoundPred); |
0 |
| 11627 |
std::swap(CanonicalLHS, CanonicalRHS); |
0 |
11627 |
std::swap(CanonicalLHS, CanonicalRHS); |
0 |
| 11628 |
std::swap(CanonicalFoundLHS, CanonicalFoundRHS); |
0 |
11628 |
std::swap(CanonicalFoundLHS, CanonicalFoundRHS); |
0 |
| 11629 |
} |
--- |
11629 |
} |
--- |
| 11630 |
assert((ICmpInst::isLT(CanonicalPred) || ICmpInst::isLE(CanonicalPred)) && |
0 |
11630 |
assert((ICmpInst::isLT(CanonicalPred) || ICmpInst::isLE(CanonicalPred)) && |
0 |
| 11631 |
"Must be!"); |
--- |
11631 |
"Must be!"); |
--- |
| 11632 |
assert((ICmpInst::isLT(CanonicalFoundPred) || |
0 |
11632 |
assert((ICmpInst::isLT(CanonicalFoundPred) || |
0 |
| 11633 |
ICmpInst::isLE(CanonicalFoundPred)) && |
--- |
11633 |
ICmpInst::isLE(CanonicalFoundPred)) && |
--- |
| 11634 |
"Must be!"); |
--- |
11634 |
"Must be!"); |
--- |
| 11635 |
if (ICmpInst::isSigned(CanonicalPred) && isKnownNonNegative(CanonicalRHS)) |
0 |
11635 |
if (ICmpInst::isSigned(CanonicalPred) && isKnownNonNegative(CanonicalRHS)) |
0 |
| 11636 |
// Use implication: |
--- |
11636 |
// Use implication: |
--- |
| 11637 |
// x =s 0 --> x
| --- |
11637 |
// x =s 0 --> x
| --- |
| |
| 11638 |
// If we can prove the left part, the right part is also proven. |
--- |
11638 |
// If we can prove the left part, the right part is also proven. |
--- |
| 11639 |
return isImpliedCondOperands(CanonicalFoundPred, CanonicalLHS, |
0 |
11639 |
return isImpliedCondOperands(CanonicalFoundPred, CanonicalLHS, |
0 |
| 11640 |
CanonicalRHS, CanonicalFoundLHS, |
--- |
11640 |
CanonicalRHS, CanonicalFoundLHS, |
--- |
| 11641 |
CanonicalFoundRHS); |
0 |
11641 |
CanonicalFoundRHS); |
0 |
| 11642 |
if (ICmpInst::isUnsigned(CanonicalPred) && isKnownNegative(CanonicalRHS)) |
0 |
11642 |
if (ICmpInst::isUnsigned(CanonicalPred) && isKnownNegative(CanonicalRHS)) |
0 |
| 11643 |
// Use implication: |
--- |
11643 |
// Use implication: |
--- |
| 11644 |
// x x
| --- |
11644 |
// x x
| --- |
| |
| 11645 |
// If we can prove the left part, the right part is also proven. |
--- |
11645 |
// If we can prove the left part, the right part is also proven. |
--- |
| 11646 |
return isImpliedCondOperands(CanonicalFoundPred, CanonicalLHS, |
0 |
11646 |
return isImpliedCondOperands(CanonicalFoundPred, CanonicalLHS, |
0 |
| 11647 |
CanonicalRHS, CanonicalFoundLHS, |
--- |
11647 |
CanonicalRHS, CanonicalFoundLHS, |
--- |
| 11648 |
CanonicalFoundRHS); |
0 |
11648 |
CanonicalFoundRHS); |
0 |
| 11649 |
} |
--- |
11649 |
} |
--- |
| 11650 |
|
--- |
11650 |
|
--- |
| 11651 |
// Check if we can make progress by sharpening ranges. |
--- |
11651 |
// Check if we can make progress by sharpening ranges. |
--- |
| 11652 |
if (FoundPred == ICmpInst::ICMP_NE && |
0 |
11652 |
if (FoundPred == ICmpInst::ICMP_NE && |
0 |
| 11653 |
(isa(FoundLHS) || isa(FoundRHS))) { |
0 |
11653 |
(isa(FoundLHS) || isa(FoundRHS))) { |
0 |
| 11654 |
|
--- |
11654 |
|
--- |
| 11655 |
const SCEVConstant *C = nullptr; |
0 |
11655 |
const SCEVConstant *C = nullptr; |
0 |
| 11656 |
const SCEV *V = nullptr; |
0 |
11656 |
const SCEV *V = nullptr; |
0 |
| 11657 |
|
--- |
11657 |
|
--- |
| 11658 |
if (isa(FoundLHS)) { |
0 |
11658 |
if (isa(FoundLHS)) { |
0 |
| 11659 |
C = cast(FoundLHS); |
0 |
11659 |
C = cast(FoundLHS); |
0 |
| 11660 |
V = FoundRHS; |
0 |
11660 |
V = FoundRHS; |
0 |
| 11661 |
} else { |
--- |
11661 |
} else { |
--- |
| 11662 |
C = cast(FoundRHS); |
0 |
11662 |
C = cast(FoundRHS); |
0 |
| 11663 |
V = FoundLHS; |
0 |
11663 |
V = FoundLHS; |
0 |
| 11664 |
} |
--- |
11664 |
} |
--- |
| 11665 |
|
--- |
11665 |
|
--- |
| 11666 |
// The guarding predicate tells us that C != V. If the known range |
--- |
11666 |
// The guarding predicate tells us that C != V. If the known range |
--- |
| 11667 |
// of V is [C, t), we can sharpen the range to [C + 1, t). The |
--- |
11667 |
// of V is [C, t), we can sharpen the range to [C + 1, t). The |
--- |
| 11668 |
// range we consider has to correspond to same signedness as the |
--- |
11668 |
// range we consider has to correspond to same signedness as the |
--- |
| 11669 |
// predicate we're interested in folding. |
--- |
11669 |
// predicate we're interested in folding. |
--- |
| 11670 |
|
--- |
11670 |
|
--- |
| 11671 |
APInt Min = ICmpInst::isSigned(Pred) ? |
0 |
11671 |
APInt Min = ICmpInst::isSigned(Pred) ? |
0 |
| 11672 |
getSignedRangeMin(V) : getUnsignedRangeMin(V); |
0 |
11672 |
getSignedRangeMin(V) : getUnsignedRangeMin(V); |
0 |
| 11673 |
|
--- |
11673 |
|
--- |
| 11674 |
if (Min == C->getAPInt()) { |
0 |
11674 |
if (Min == C->getAPInt()) { |
0 |
| 11675 |
// Given (V >= Min && V != Min) we conclude V >= (Min + 1). |
--- |
11675 |
// Given (V >= Min && V != Min) we conclude V >= (Min + 1). |
--- |
| 11676 |
// This is true even if (Min + 1) wraps around -- in case of |
--- |
11676 |
// This is true even if (Min + 1) wraps around -- in case of |
--- |
| 11677 |
// wraparound, (Min + 1) < Min, so (V >= Min => V >= (Min + 1)). |
--- |
11677 |
// wraparound, (Min + 1) < Min, so (V >= Min => V >= (Min + 1)). |
--- |
| 11678 |
|
--- |
11678 |
|
--- |
| 11679 |
APInt SharperMin = Min + 1; |
0 |
11679 |
APInt SharperMin = Min + 1; |
0 |
| 11680 |
|
--- |
11680 |
|
--- |
| 11681 |
switch (Pred) { |
0 |
11681 |
switch (Pred) { |
0 |
| 11682 |
case ICmpInst::ICMP_SGE: |
0 |
11682 |
case ICmpInst::ICMP_SGE: |
0 |
| 11683 |
case ICmpInst::ICMP_UGE: |
--- |
11683 |
case ICmpInst::ICMP_UGE: |
--- |
| 11684 |
// We know V `Pred` SharperMin. If this implies LHS `Pred` |
--- |
11684 |
// We know V `Pred` SharperMin. If this implies LHS `Pred` |
--- |
| 11685 |
// RHS, we're done. |
--- |
11685 |
// RHS, we're done. |
--- |
| 11686 |
if (isImpliedCondOperands(Pred, LHS, RHS, V, getConstant(SharperMin), |
0 |
11686 |
if (isImpliedCondOperands(Pred, LHS, RHS, V, getConstant(SharperMin), |
0 |
| 11687 |
CtxI)) |
--- |
11687 |
CtxI)) |
--- |
| 11688 |
return true; |
0 |
11688 |
return true; |
0 |
| 11689 |
[[fallthrough]]; |
--- |
11689 |
[[fallthrough]]; |
--- |
| 11690 |
|
--- |
11690 |
|
--- |
| 11691 |
case ICmpInst::ICMP_SGT: |
--- |
11691 |
case ICmpInst::ICMP_SGT: |
--- |
| 11692 |
case ICmpInst::ICMP_UGT: |
--- |
11692 |
case ICmpInst::ICMP_UGT: |
--- |
| 11693 |
// We know from the range information that (V `Pred` Min || |
--- |
11693 |
// We know from the range information that (V `Pred` Min || |
--- |
| 11694 |
// V == Min). We know from the guarding condition that !(V |
--- |
11694 |
// V == Min). We know from the guarding condition that !(V |
--- |
| 11695 |
// == Min). This gives us |
--- |
11695 |
// == Min). This gives us |
--- |
| 11696 |
// |
--- |
11696 |
// |
--- |
| 11697 |
// V `Pred` Min || V == Min && !(V == Min) |
--- |
11697 |
// V `Pred` Min || V == Min && !(V == Min) |
--- |
| 11698 |
// => V `Pred` Min |
--- |
11698 |
// => V `Pred` Min |
--- |
| 11699 |
// |
--- |
11699 |
// |
--- |
| 11700 |
// If V `Pred` Min implies LHS `Pred` RHS, we're done. |
--- |
11700 |
// If V `Pred` Min implies LHS `Pred` RHS, we're done. |
--- |
| 11701 |
|
--- |
11701 |
|
--- |
| 11702 |
if (isImpliedCondOperands(Pred, LHS, RHS, V, getConstant(Min), CtxI)) |
0 |
11702 |
if (isImpliedCondOperands(Pred, LHS, RHS, V, getConstant(Min), CtxI)) |
0 |
| 11703 |
return true; |
0 |
11703 |
return true; |
0 |
| 11704 |
break; |
0 |
11704 |
break; |
0 |
| 11705 |
|
--- |
11705 |
|
--- |
| 11706 |
// `LHS < RHS` and `LHS <= RHS` are handled in the same way as `RHS > LHS` and `RHS >= LHS` respectively. |
--- |
11706 |
// `LHS < RHS` and `LHS <= RHS` are handled in the same way as `RHS > LHS` and `RHS >= LHS` respectively. |
--- |
| 11707 |
case ICmpInst::ICMP_SLE: |
0 |
11707 |
case ICmpInst::ICMP_SLE: |
0 |
| 11708 |
case ICmpInst::ICMP_ULE: |
--- |
11708 |
case ICmpInst::ICMP_ULE: |
--- |
| 11709 |
if (isImpliedCondOperands(CmpInst::getSwappedPredicate(Pred), RHS, |
0 |
11709 |
if (isImpliedCondOperands(CmpInst::getSwappedPredicate(Pred), RHS, |
0 |
| 11710 |
LHS, V, getConstant(SharperMin), CtxI)) |
--- |
11710 |
LHS, V, getConstant(SharperMin), CtxI)) |
--- |
| 11711 |
return true; |
0 |
11711 |
return true; |
0 |
| 11712 |
[[fallthrough]]; |
--- |
11712 |
[[fallthrough]]; |
--- |
| 11713 |
|
--- |
11713 |
|
--- |
| 11714 |
case ICmpInst::ICMP_SLT: |
--- |
11714 |
case ICmpInst::ICMP_SLT: |
--- |
| 11715 |
case ICmpInst::ICMP_ULT: |
--- |
11715 |
case ICmpInst::ICMP_ULT: |
--- |
| 11716 |
if (isImpliedCondOperands(CmpInst::getSwappedPredicate(Pred), RHS, |
0 |
11716 |
if (isImpliedCondOperands(CmpInst::getSwappedPredicate(Pred), RHS, |
0 |
| 11717 |
LHS, V, getConstant(Min), CtxI)) |
--- |
11717 |
LHS, V, getConstant(Min), CtxI)) |
--- |
| 11718 |
return true; |
0 |
11718 |
return true; |
0 |
| 11719 |
break; |
0 |
11719 |
break; |
0 |
| 11720 |
|
--- |
11720 |
|
--- |
| 11721 |
default: |
0 |
11721 |
default: |
0 |
| 11722 |
// No change |
--- |
11722 |
// No change |
--- |
| 11723 |
break; |
0 |
11723 |
break; |
0 |
| 11724 |
} |
--- |
11724 |
} |
--- |
| 11725 |
} |
0 |
11725 |
} |
0 |
| 11726 |
} |
0 |
11726 |
} |
0 |
| 11727 |
|
--- |
11727 |
|
--- |
| 11728 |
// Check whether the actual condition is beyond sufficient. |
--- |
11728 |
// Check whether the actual condition is beyond sufficient. |
--- |
| 11729 |
if (FoundPred == ICmpInst::ICMP_EQ) |
0 |
11729 |
if (FoundPred == ICmpInst::ICMP_EQ) |
0 |
| 11730 |
if (ICmpInst::isTrueWhenEqual(Pred)) |
0 |
11730 |
if (ICmpInst::isTrueWhenEqual(Pred)) |
0 |
| 11731 |
if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI)) |
0 |
11731 |
if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI)) |
0 |
| 11732 |
return true; |
0 |
11732 |
return true; |
0 |
| 11733 |
if (Pred == ICmpInst::ICMP_NE) |
0 |
11733 |
if (Pred == ICmpInst::ICMP_NE) |
0 |
| 11734 |
if (!ICmpInst::isTrueWhenEqual(FoundPred)) |
0 |
11734 |
if (!ICmpInst::isTrueWhenEqual(FoundPred)) |
0 |
| 11735 |
if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS, CtxI)) |
0 |
11735 |
if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS, CtxI)) |
0 |
| 11736 |
return true; |
0 |
11736 |
return true; |
0 |
| 11737 |
|
--- |
11737 |
|
--- |
| 11738 |
// Otherwise assume the worst. |
--- |
11738 |
// Otherwise assume the worst. |
--- |
| 11739 |
return false; |
0 |
11739 |
return false; |
0 |
| 11740 |
} |
--- |
11740 |
} |
--- |
| 11741 |
|
--- |
11741 |
|
--- |
| 11742 |
bool ScalarEvolution::splitBinaryAdd(const SCEV *Expr, |
0 |
11742 |
bool ScalarEvolution::splitBinaryAdd(const SCEV *Expr, |
0 |
| 11743 |
const SCEV *&L, const SCEV *&R, |
--- |
11743 |
const SCEV *&L, const SCEV *&R, |
--- |
| 11744 |
SCEV::NoWrapFlags &Flags) { |
--- |
11744 |
SCEV::NoWrapFlags &Flags) { |
--- |
| 11745 |
const auto *AE = dyn_cast(Expr); |
0 |
11745 |
const auto *AE = dyn_cast(Expr); |
0 |
| 11746 |
if (!AE || AE->getNumOperands() != 2) |
0 |
11746 |
if (!AE || AE->getNumOperands() != 2) |
0 |
| 11747 |
return false; |
0 |
11747 |
return false; |
0 |
| 11748 |
|
--- |
11748 |
|
--- |
| 11749 |
L = AE->getOperand(0); |
0 |
11749 |
L = AE->getOperand(0); |
0 |
| 11750 |
R = AE->getOperand(1); |
0 |
11750 |
R = AE->getOperand(1); |
0 |
| 11751 |
Flags = AE->getNoWrapFlags(); |
0 |
11751 |
Flags = AE->getNoWrapFlags(); |
0 |
| 11752 |
return true; |
0 |
11752 |
return true; |
0 |
| 11753 |
} |
--- |
11753 |
} |
--- |
| 11754 |
|
--- |
11754 |
|
--- |
| 11755 |
std::optional |
--- |
11755 |
std::optional |
--- |
| 11756 |
ScalarEvolution::computeConstantDifference(const SCEV *More, const SCEV *Less) { |
0 |
11756 |
ScalarEvolution::computeConstantDifference(const SCEV *More, const SCEV *Less) { |
0 |
| 11757 |
// We avoid subtracting expressions here because this function is usually |
--- |
11757 |
// We avoid subtracting expressions here because this function is usually |
--- |
| 11758 |
// fairly deep in the call stack (i.e. is called many times). |
--- |
11758 |
// fairly deep in the call stack (i.e. is called many times). |
--- |
| 11759 |
|
--- |
11759 |
|
--- |
| 11760 |
// X - X = 0. |
--- |
11760 |
// X - X = 0. |
--- |
| 11761 |
if (More == Less) |
0 |
11761 |
if (More == Less) |
0 |
| 11762 |
return APInt(getTypeSizeInBits(More->getType()), 0); |
0 |
11762 |
return APInt(getTypeSizeInBits(More->getType()), 0); |
0 |
| 11763 |
|
--- |
11763 |
|
--- |
| 11764 |
if (isa(Less) && isa(More)) { |
0 |
11764 |
if (isa(Less) && isa(More)) { |
0 |
| 11765 |
const auto *LAR = cast(Less); |
0 |
11765 |
const auto *LAR = cast(Less); |
0 |
| 11766 |
const auto *MAR = cast(More); |
0 |
11766 |
const auto *MAR = cast(More); |
0 |
| 11767 |
|
--- |
11767 |
|
--- |
| 11768 |
if (LAR->getLoop() != MAR->getLoop()) |
0 |
11768 |
if (LAR->getLoop() != MAR->getLoop()) |
0 |
| 11769 |
return std::nullopt; |
0 |
11769 |
return std::nullopt; |
0 |
| 11770 |
|
--- |
11770 |
|
--- |
| 11771 |
// We look at affine expressions only; not for correctness but to keep |
--- |
11771 |
// We look at affine expressions only; not for correctness but to keep |
--- |
| 11772 |
// getStepRecurrence cheap. |
--- |
11772 |
// getStepRecurrence cheap. |
--- |
| 11773 |
if (!LAR->isAffine() || !MAR->isAffine()) |
0 |
11773 |
if (!LAR->isAffine() || !MAR->isAffine()) |
0 |
| 11774 |
return std::nullopt; |
0 |
11774 |
return std::nullopt; |
0 |
| 11775 |
|
--- |
11775 |
|
--- |
| 11776 |
if (LAR->getStepRecurrence(*this) != MAR->getStepRecurrence(*this)) |
0 |
11776 |
if (LAR->getStepRecurrence(*this) != MAR->getStepRecurrence(*this)) |
0 |
| 11777 |
return std::nullopt; |
0 |
11777 |
return std::nullopt; |
0 |
| 11778 |
|
--- |
11778 |
|
--- |
| 11779 |
Less = LAR->getStart(); |
0 |
11779 |
Less = LAR->getStart(); |
0 |
| 11780 |
More = MAR->getStart(); |
0 |
11780 |
More = MAR->getStart(); |
0 |
| 11781 |
|
--- |
11781 |
|
--- |
| 11782 |
// fall through |
--- |
11782 |
// fall through |
--- |
| 11783 |
} |
--- |
11783 |
} |
--- |
| 11784 |
|
--- |
11784 |
|
--- |
| 11785 |
if (isa(Less) && isa(More)) { |
0 |
11785 |
if (isa(Less) && isa(More)) { |
0 |
| 11786 |
const auto &M = cast(More)->getAPInt(); |
0 |
11786 |
const auto &M = cast(More)->getAPInt(); |
0 |
| 11787 |
const auto &L = cast(Less)->getAPInt(); |
0 |
11787 |
const auto &L = cast(Less)->getAPInt(); |
0 |
| 11788 |
return M - L; |
0 |
11788 |
return M - L; |
0 |
| 11789 |
} |
--- |
11789 |
} |
--- |
| 11790 |
|
--- |
11790 |
|
--- |
| 11791 |
SCEV::NoWrapFlags Flags; |
--- |
11791 |
SCEV::NoWrapFlags Flags; |
--- |
| 11792 |
const SCEV *LLess = nullptr, *RLess = nullptr; |
0 |
11792 |
const SCEV *LLess = nullptr, *RLess = nullptr; |
0 |
| 11793 |
const SCEV *LMore = nullptr, *RMore = nullptr; |
0 |
11793 |
const SCEV *LMore = nullptr, *RMore = nullptr; |
0 |
| 11794 |
const SCEVConstant *C1 = nullptr, *C2 = nullptr; |
0 |
11794 |
const SCEVConstant *C1 = nullptr, *C2 = nullptr; |
0 |
| 11795 |
// Compare (X + C1) vs X. |
--- |
11795 |
// Compare (X + C1) vs X. |
--- |
| 11796 |
if (splitBinaryAdd(Less, LLess, RLess, Flags)) |
0 |
11796 |
if (splitBinaryAdd(Less, LLess, RLess, Flags)) |
0 |
| 11797 |
if ((C1 = dyn_cast(LLess))) |
0 |
11797 |
if ((C1 = dyn_cast(LLess))) |
0 |
| 11798 |
if (RLess == More) |
0 |
11798 |
if (RLess == More) |
0 |
| 11799 |
return -(C1->getAPInt()); |
0 |
11799 |
return -(C1->getAPInt()); |
0 |
| 11800 |
|
--- |
11800 |
|
--- |
| 11801 |
// Compare X vs (X + C2). |
--- |
11801 |
// Compare X vs (X + C2). |
--- |
| 11802 |
if (splitBinaryAdd(More, LMore, RMore, Flags)) |
0 |
11802 |
if (splitBinaryAdd(More, LMore, RMore, Flags)) |
0 |
| 11803 |
if ((C2 = dyn_cast(LMore))) |
0 |
11803 |
if ((C2 = dyn_cast(LMore))) |
0 |
| 11804 |
if (RMore == Less) |
0 |
11804 |
if (RMore == Less) |
0 |
| 11805 |
return C2->getAPInt(); |
0 |
11805 |
return C2->getAPInt(); |
0 |
| 11806 |
|
--- |
11806 |
|
--- |
| 11807 |
// Compare (X + C1) vs (X + C2). |
--- |
11807 |
// Compare (X + C1) vs (X + C2). |
--- |
| 11808 |
if (C1 && C2 && RLess == RMore) |
0 |
11808 |
if (C1 && C2 && RLess == RMore) |
0 |
| 11809 |
return C2->getAPInt() - C1->getAPInt(); |
0 |
11809 |
return C2->getAPInt() - C1->getAPInt(); |
0 |
| 11810 |
|
--- |
11810 |
|
--- |
| 11811 |
return std::nullopt; |
0 |
11811 |
return std::nullopt; |
0 |
| 11812 |
} |
--- |
11812 |
} |
--- |
| 11813 |
|
--- |
11813 |
|
--- |
| 11814 |
bool ScalarEvolution::isImpliedCondOperandsViaAddRecStart( |
0 |
11814 |
bool ScalarEvolution::isImpliedCondOperandsViaAddRecStart( |
0 |
| 11815 |
ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, |
--- |
11815 |
ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, |
--- |
| 11816 |
const SCEV *FoundLHS, const SCEV *FoundRHS, const Instruction *CtxI) { |
--- |
11816 |
const SCEV *FoundLHS, const SCEV *FoundRHS, const Instruction *CtxI) { |
--- |
| 11817 |
// Try to recognize the following pattern: |
--- |
11817 |
// Try to recognize the following pattern: |
--- |
| 11818 |
// |
--- |
11818 |
// |
--- |
| 11819 |
// FoundRHS = ... |
--- |
11819 |
// FoundRHS = ... |
--- |
| 11820 |
// ... |
--- |
11820 |
// ... |
--- |
| 11821 |
// loop: |
--- |
11821 |
// loop: |
--- |
| 11822 |
// FoundLHS = {Start,+,W} |
--- |
11822 |
// FoundLHS = {Start,+,W} |
--- |
| 11823 |
// context_bb: // Basic block from the same loop |
--- |
11823 |
// context_bb: // Basic block from the same loop |
--- |
| 11824 |
// known(Pred, FoundLHS, FoundRHS) |
--- |
11824 |
// known(Pred, FoundLHS, FoundRHS) |
--- |
| 11825 |
// |
--- |
11825 |
// |
--- |
| 11826 |
// If some predicate is known in the context of a loop, it is also known on |
--- |
11826 |
// If some predicate is known in the context of a loop, it is also known on |
--- |
| 11827 |
// each iteration of this loop, including the first iteration. Therefore, in |
--- |
11827 |
// each iteration of this loop, including the first iteration. Therefore, in |
--- |
| 11828 |
// this case, `FoundLHS Pred FoundRHS` implies `Start Pred FoundRHS`. Try to |
--- |
11828 |
// this case, `FoundLHS Pred FoundRHS` implies `Start Pred FoundRHS`. Try to |
--- |
| 11829 |
// prove the original pred using this fact. |
--- |
11829 |
// prove the original pred using this fact. |
--- |
| 11830 |
if (!CtxI) |
0 |
11830 |
if (!CtxI) |
0 |
| 11831 |
return false; |
0 |
11831 |
return false; |
0 |
| 11832 |
const BasicBlock *ContextBB = CtxI->getParent(); |
0 |
11832 |
const BasicBlock *ContextBB = CtxI->getParent(); |
0 |
| 11833 |
// Make sure AR varies in the context block. |
--- |
11833 |
// Make sure AR varies in the context block. |
--- |
| 11834 |
if (auto *AR = dyn_cast(FoundLHS)) { |
0 |
11834 |
if (auto *AR = dyn_cast(FoundLHS)) { |
0 |
| 11835 |
const Loop *L = AR->getLoop(); |
0 |
11835 |
const Loop *L = AR->getLoop(); |
0 |
| 11836 |
// Make sure that context belongs to the loop and executes on 1st iteration |
--- |
11836 |
// Make sure that context belongs to the loop and executes on 1st iteration |
--- |
| 11837 |
// (if it ever executes at all). |
--- |
11837 |
// (if it ever executes at all). |
--- |
| 11838 |
if (!L->contains(ContextBB) || !DT.dominates(ContextBB, L->getLoopLatch())) |
0 |
11838 |
if (!L->contains(ContextBB) || !DT.dominates(ContextBB, L->getLoopLatch())) |
0 |
| 11839 |
return false; |
0 |
11839 |
return false; |
0 |
| 11840 |
if (!isAvailableAtLoopEntry(FoundRHS, AR->getLoop())) |
0 |
11840 |
if (!isAvailableAtLoopEntry(FoundRHS, AR->getLoop())) |
0 |
| 11841 |
return false; |
0 |
11841 |
return false; |
0 |
| 11842 |
return isImpliedCondOperands(Pred, LHS, RHS, AR->getStart(), FoundRHS); |
0 |
11842 |
return isImpliedCondOperands(Pred, LHS, RHS, AR->getStart(), FoundRHS); |
0 |
| 11843 |
} |
--- |
11843 |
} |
--- |
| 11844 |
|
--- |
11844 |
|
--- |
| 11845 |
if (auto *AR = dyn_cast(FoundRHS)) { |
0 |
11845 |
if (auto *AR = dyn_cast(FoundRHS)) { |
0 |
| 11846 |
const Loop *L = AR->getLoop(); |
0 |
11846 |
const Loop *L = AR->getLoop(); |
0 |
| 11847 |
// Make sure that context belongs to the loop and executes on 1st iteration |
--- |
11847 |
// Make sure that context belongs to the loop and executes on 1st iteration |
--- |
| 11848 |
// (if it ever executes at all). |
--- |
11848 |
// (if it ever executes at all). |
--- |
| 11849 |
if (!L->contains(ContextBB) || !DT.dominates(ContextBB, L->getLoopLatch())) |
0 |
11849 |
if (!L->contains(ContextBB) || !DT.dominates(ContextBB, L->getLoopLatch())) |
0 |
| 11850 |
return false; |
0 |
11850 |
return false; |
0 |
| 11851 |
if (!isAvailableAtLoopEntry(FoundLHS, AR->getLoop())) |
0 |
11851 |
if (!isAvailableAtLoopEntry(FoundLHS, AR->getLoop())) |
0 |
| 11852 |
return false; |
0 |
11852 |
return false; |
0 |
| 11853 |
return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, AR->getStart()); |
0 |
11853 |
return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, AR->getStart()); |
0 |
| 11854 |
} |
--- |
11854 |
} |
--- |
| 11855 |
|
--- |
11855 |
|
--- |
| 11856 |
return false; |
0 |
11856 |
return false; |
0 |
| 11857 |
} |
--- |
11857 |
} |
--- |
| 11858 |
|
--- |
11858 |
|
--- |
| 11859 |
bool ScalarEvolution::isImpliedCondOperandsViaNoOverflow( |
0 |
11859 |
bool ScalarEvolution::isImpliedCondOperandsViaNoOverflow( |
0 |
| 11860 |
ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, |
--- |
11860 |
ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, |
--- |
| 11861 |
const SCEV *FoundLHS, const SCEV *FoundRHS) { |
--- |
11861 |
const SCEV *FoundLHS, const SCEV *FoundRHS) { |
--- |
| 11862 |
if (Pred != CmpInst::ICMP_SLT && Pred != CmpInst::ICMP_ULT) |
0 |
11862 |
if (Pred != CmpInst::ICMP_SLT && Pred != CmpInst::ICMP_ULT) |
0 |
| 11863 |
return false; |
0 |
11863 |
return false; |
0 |
| 11864 |
|
--- |
11864 |
|
--- |
| 11865 |
const auto *AddRecLHS = dyn_cast(LHS); |
0 |
11865 |
const auto *AddRecLHS = dyn_cast(LHS); |
0 |
| 11866 |
if (!AddRecLHS) |
0 |
11866 |
if (!AddRecLHS) |
0 |
| 11867 |
return false; |
0 |
11867 |
return false; |
0 |
| 11868 |
|
--- |
11868 |
|
--- |
| 11869 |
const auto *AddRecFoundLHS = dyn_cast(FoundLHS); |
0 |
11869 |
const auto *AddRecFoundLHS = dyn_cast(FoundLHS); |
0 |
| 11870 |
if (!AddRecFoundLHS) |
0 |
11870 |
if (!AddRecFoundLHS) |
0 |
| 11871 |
return false; |
0 |
11871 |
return false; |
0 |
| 11872 |
|
--- |
11872 |
|
--- |
| 11873 |
// We'd like to let SCEV reason about control dependencies, so we constrain |
--- |
11873 |
// We'd like to let SCEV reason about control dependencies, so we constrain |
--- |
| 11874 |
// both the inequalities to be about add recurrences on the same loop. This |
--- |
11874 |
// both the inequalities to be about add recurrences on the same loop. This |
--- |
| 11875 |
// way we can use isLoopEntryGuardedByCond later. |
--- |
11875 |
// way we can use isLoopEntryGuardedByCond later. |
--- |
| 11876 |
|
--- |
11876 |
|
--- |
| 11877 |
const Loop *L = AddRecFoundLHS->getLoop(); |
0 |
11877 |
const Loop *L = AddRecFoundLHS->getLoop(); |
0 |
| 11878 |
if (L != AddRecLHS->getLoop()) |
0 |
11878 |
if (L != AddRecLHS->getLoop()) |
0 |
| 11879 |
return false; |
0 |
11879 |
return false; |
0 |
| 11880 |
|
--- |
11880 |
|
--- |
| 11881 |
// FoundLHS u< FoundRHS u< -C => (FoundLHS + C) u< (FoundRHS + C) ... (1) |
--- |
11881 |
// FoundLHS u< FoundRHS u< -C => (FoundLHS + C) u< (FoundRHS + C) ... (1) |
--- |
| 11882 |
// |
--- |
11882 |
// |
--- |
| 11883 |
// FoundLHS s< FoundRHS s< INT_MIN - C => (FoundLHS + C) s< (FoundRHS + C) |
--- |
11883 |
// FoundLHS s< FoundRHS s< INT_MIN - C => (FoundLHS + C) s< (FoundRHS + C) |
--- |
| 11884 |
// ... (2) |
--- |
11884 |
// ... (2) |
--- |
| 11885 |
// |
--- |
11885 |
// |
--- |
| 11886 |
// Informal proof for (2), assuming (1) [*]: |
--- |
11886 |
// Informal proof for (2), assuming (1) [*]: |
--- |
| 11887 |
// |
--- |
11887 |
// |
--- |
| 11888 |
// We'll also assume (A s< B) <=> ((A + INT_MIN) u< (B + INT_MIN)) ... (3)[**] |
--- |
11888 |
// We'll also assume (A s< B) <=> ((A + INT_MIN) u< (B + INT_MIN)) ... (3)[**] |
--- |
| 11889 |
// |
--- |
11889 |
// |
--- |
| 11890 |
// Then |
--- |
11890 |
// Then |
--- |
| 11891 |
// |
--- |
11891 |
// |
--- |
| 11892 |
// FoundLHS s< FoundRHS s< INT_MIN - C |
--- |
11892 |
// FoundLHS s< FoundRHS s< INT_MIN - C |
--- |
| 11893 |
// <=> (FoundLHS + INT_MIN) u< (FoundRHS + INT_MIN) u< -C [ using (3) ] |
--- |
11893 |
// <=> (FoundLHS + INT_MIN) u< (FoundRHS + INT_MIN) u< -C [ using (3) ] |
--- |
| 11894 |
// <=> (FoundLHS + INT_MIN + C) u< (FoundRHS + INT_MIN + C) [ using (1) ] |
--- |
11894 |
// <=> (FoundLHS + INT_MIN + C) u< (FoundRHS + INT_MIN + C) [ using (1) ] |
--- |
| 11895 |
// <=> (FoundLHS + INT_MIN + C + INT_MIN) s< |
--- |
11895 |
// <=> (FoundLHS + INT_MIN + C + INT_MIN) s< |
--- |
| 11896 |
// (FoundRHS + INT_MIN + C + INT_MIN) [ using (3) ] |
--- |
11896 |
// (FoundRHS + INT_MIN + C + INT_MIN) [ using (3) ] |
--- |
| 11897 |
// <=> FoundLHS + C s< FoundRHS + C |
--- |
11897 |
// <=> FoundLHS + C s< FoundRHS + C |
--- |
| 11898 |
// |
--- |
11898 |
// |
--- |
| 11899 |
// [*]: (1) can be proved by ruling out overflow. |
--- |
11899 |
// [*]: (1) can be proved by ruling out overflow. |
--- |
| 11900 |
// |
--- |
11900 |
// |
--- |
| 11901 |
// [**]: This can be proved by analyzing all the four possibilities: |
--- |
11901 |
// [**]: This can be proved by analyzing all the four possibilities: |
--- |
| 11902 |
// (A s< 0, B s< 0), (A s< 0, B s>= 0), (A s>= 0, B s< 0) and |
--- |
11902 |
// (A s< 0, B s< 0), (A s< 0, B s>= 0), (A s>= 0, B s< 0) and |
--- |
| 11903 |
// (A s>= 0, B s>= 0). |
--- |
11903 |
// (A s>= 0, B s>= 0). |
--- |
| 11904 |
// |
--- |
11904 |
// |
--- |
| 11905 |
// Note: |
--- |
11905 |
// Note: |
--- |
| 11906 |
// Despite (2), "FoundRHS s< INT_MIN - C" does not mean that "FoundRHS + C" |
--- |
11906 |
// Despite (2), "FoundRHS s< INT_MIN - C" does not mean that "FoundRHS + C" |
--- |
| 11907 |
// will not sign underflow. For instance, say FoundLHS = (i8 -128), FoundRHS |
--- |
11907 |
// will not sign underflow. For instance, say FoundLHS = (i8 -128), FoundRHS |
--- |
| 11908 |
// = (i8 -127) and C = (i8 -100). Then INT_MIN - C = (i8 -28), and FoundRHS |
--- |
11908 |
// = (i8 -127) and C = (i8 -100). Then INT_MIN - C = (i8 -28), and FoundRHS |
--- |
| 11909 |
// s< (INT_MIN - C). Lack of sign overflow / underflow in "FoundRHS + C" is |
--- |
11909 |
// s< (INT_MIN - C). Lack of sign overflow / underflow in "FoundRHS + C" is |
--- |
| 11910 |
// neither necessary nor sufficient to prove "(FoundLHS + C) s< (FoundRHS + |
--- |
11910 |
// neither necessary nor sufficient to prove "(FoundLHS + C) s< (FoundRHS + |
--- |
| 11911 |
// C)". |
--- |
11911 |
// C)". |
--- |
| 11912 |
|
--- |
11912 |
|
--- |
| 11913 |
std::optional LDiff = computeConstantDifference(LHS, FoundLHS); |
0 |
11913 |
std::optional LDiff = computeConstantDifference(LHS, FoundLHS); |
0 |
| 11914 |
std::optional RDiff = computeConstantDifference(RHS, FoundRHS); |
0 |
11914 |
std::optional RDiff = computeConstantDifference(RHS, FoundRHS); |
0 |
| 11915 |
if (!LDiff || !RDiff || *LDiff != *RDiff) |
0 |
11915 |
if (!LDiff || !RDiff || *LDiff != *RDiff) |
0 |
| 11916 |
return false; |
0 |
11916 |
return false; |
0 |
| 11917 |
|
--- |
11917 |
|
--- |
| 11918 |
if (LDiff->isMinValue()) |
0 |
11918 |
if (LDiff->isMinValue()) |
0 |
| 11919 |
return true; |
0 |
11919 |
return true; |
0 |
| 11920 |
|
--- |
11920 |
|
--- |
| 11921 |
APInt FoundRHSLimit; |
0 |
11921 |
APInt FoundRHSLimit; |
0 |
| 11922 |
|
--- |
11922 |
|
--- |
| 11923 |
if (Pred == CmpInst::ICMP_ULT) { |
0 |
11923 |
if (Pred == CmpInst::ICMP_ULT) { |
0 |
| 11924 |
FoundRHSLimit = -(*RDiff); |
0 |
11924 |
FoundRHSLimit = -(*RDiff); |
0 |
| 11925 |
} else { |
--- |
11925 |
} else { |
--- |
| 11926 |
assert(Pred == CmpInst::ICMP_SLT && "Checked above!"); |
0 |
11926 |
assert(Pred == CmpInst::ICMP_SLT && "Checked above!"); |
0 |
| 11927 |
FoundRHSLimit = APInt::getSignedMinValue(getTypeSizeInBits(RHS->getType())) - *RDiff; |
0 |
11927 |
FoundRHSLimit = APInt::getSignedMinValue(getTypeSizeInBits(RHS->getType())) - *RDiff; |
0 |
| 11928 |
} |
--- |
11928 |
} |
--- |
| 11929 |
|
--- |
11929 |
|
--- |
| 11930 |
// Try to prove (1) or (2), as needed. |
--- |
11930 |
// Try to prove (1) or (2), as needed. |
--- |
| 11931 |
return isAvailableAtLoopEntry(FoundRHS, L) && |
0 |
11931 |
return isAvailableAtLoopEntry(FoundRHS, L) && |
0 |
| 11932 |
isLoopEntryGuardedByCond(L, Pred, FoundRHS, |
0 |
11932 |
isLoopEntryGuardedByCond(L, Pred, FoundRHS, |
0 |
| 11933 |
getConstant(FoundRHSLimit)); |
0 |
11933 |
getConstant(FoundRHSLimit)); |
0 |
| 11934 |
} |
0 |
11934 |
} |
0 |
| 11935 |
|
--- |
11935 |
|
--- |
| 11936 |
bool ScalarEvolution::isImpliedViaMerge(ICmpInst::Predicate Pred, |
0 |
11936 |
bool ScalarEvolution::isImpliedViaMerge(ICmpInst::Predicate Pred, |
0 |
| 11937 |
const SCEV *LHS, const SCEV *RHS, |
--- |
11937 |
const SCEV *LHS, const SCEV *RHS, |
--- |
| 11938 |
const SCEV *FoundLHS, |
--- |
11938 |
const SCEV *FoundLHS, |
--- |
| 11939 |
const SCEV *FoundRHS, unsigned Depth) { |
--- |
11939 |
const SCEV *FoundRHS, unsigned Depth) { |
--- |
| 11940 |
const PHINode *LPhi = nullptr, *RPhi = nullptr; |
0 |
11940 |
const PHINode *LPhi = nullptr, *RPhi = nullptr; |
0 |
| 11941 |
|
--- |
11941 |
|
--- |
| 11942 |
auto ClearOnExit = make_scope_exit([&]() { |
0 |
11942 |
auto ClearOnExit = make_scope_exit([&]() { |
0 |
| 11943 |
if (LPhi) { |
0 |
11943 |
if (LPhi) { |
0 |
| 11944 |
bool Erased = PendingMerges.erase(LPhi); |
0 |
11944 |
bool Erased = PendingMerges.erase(LPhi); |
0 |
| 11945 |
assert(Erased && "Failed to erase LPhi!"); |
0 |
11945 |
assert(Erased && "Failed to erase LPhi!"); |
0 |
| 11946 |
(void)Erased; |
--- |
11946 |
(void)Erased; |
--- |
| 11947 |
} |
--- |
11947 |
} |
--- |
| 11948 |
if (RPhi) { |
0 |
11948 |
if (RPhi) { |
0 |
| 11949 |
bool Erased = PendingMerges.erase(RPhi); |
0 |
11949 |
bool Erased = PendingMerges.erase(RPhi); |
0 |
| 11950 |
assert(Erased && "Failed to erase RPhi!"); |
0 |
11950 |
assert(Erased && "Failed to erase RPhi!"); |
0 |
| 11951 |
(void)Erased; |
--- |
11951 |
(void)Erased; |
--- |
| 11952 |
} |
--- |
11952 |
} |
--- |
| 11953 |
}); |
0 |
11953 |
}); |
0 |
| 11954 |
|
--- |
11954 |
|
--- |
| 11955 |
// Find respective Phis and check that they are not being pending. |
--- |
11955 |
// Find respective Phis and check that they are not being pending. |
--- |
| 11956 |
if (const SCEVUnknown *LU = dyn_cast(LHS)) |
0 |
11956 |
if (const SCEVUnknown *LU = dyn_cast(LHS)) |
0 |
| 11957 |
if (auto *Phi = dyn_cast(LU->getValue())) { |
0 |
11957 |
if (auto *Phi = dyn_cast(LU->getValue())) { |
0 |
| 11958 |
if (!PendingMerges.insert(Phi).second) |
0 |
11958 |
if (!PendingMerges.insert(Phi).second) |
0 |
| 11959 |
return false; |
0 |
11959 |
return false; |
0 |
| 11960 |
LPhi = Phi; |
0 |
11960 |
LPhi = Phi; |
0 |
| 11961 |
} |
--- |
11961 |
} |
--- |
| 11962 |
if (const SCEVUnknown *RU = dyn_cast(RHS)) |
0 |
11962 |
if (const SCEVUnknown *RU = dyn_cast(RHS)) |
0 |
| 11963 |
if (auto *Phi = dyn_cast(RU->getValue())) { |
0 |
11963 |
if (auto *Phi = dyn_cast(RU->getValue())) { |
0 |
| 11964 |
// If we detect a loop of Phi nodes being processed by this method, for |
--- |
11964 |
// If we detect a loop of Phi nodes being processed by this method, for |
--- |
| 11965 |
// example: |
--- |
11965 |
// example: |
--- |
| 11966 |
// |
--- |
11966 |
// |
--- |
| 11967 |
// %a = phi i32 [ %some1, %preheader ], [ %b, %latch ] |
--- |
11967 |
// %a = phi i32 [ %some1, %preheader ], [ %b, %latch ] |
--- |
| 11968 |
// %b = phi i32 [ %some2, %preheader ], [ %a, %latch ] |
--- |
11968 |
// %b = phi i32 [ %some2, %preheader ], [ %a, %latch ] |
--- |
| 11969 |
// |
--- |
11969 |
// |
--- |
| 11970 |
// we don't want to deal with a case that complex, so return conservative |
--- |
11970 |
// we don't want to deal with a case that complex, so return conservative |
--- |
| 11971 |
// answer false. |
--- |
11971 |
// answer false. |
--- |
| 11972 |
if (!PendingMerges.insert(Phi).second) |
0 |
11972 |
if (!PendingMerges.insert(Phi).second) |
0 |
| 11973 |
return false; |
0 |
11973 |
return false; |
0 |
| 11974 |
RPhi = Phi; |
0 |
11974 |
RPhi = Phi; |
0 |
| 11975 |
} |
--- |
11975 |
} |
--- |
| 11976 |
|
--- |
11976 |
|
--- |
| 11977 |
// If none of LHS, RHS is a Phi, nothing to do here. |
--- |
11977 |
// If none of LHS, RHS is a Phi, nothing to do here. |
--- |
| 11978 |
if (!LPhi && !RPhi) |
0 |
11978 |
if (!LPhi && !RPhi) |
0 |
| 11979 |
return false; |
0 |
11979 |
return false; |
0 |
| 11980 |
|
--- |
11980 |
|
--- |
| 11981 |
// If there is a SCEVUnknown Phi we are interested in, make it left. |
--- |
11981 |
// If there is a SCEVUnknown Phi we are interested in, make it left. |
--- |
| 11982 |
if (!LPhi) { |
0 |
11982 |
if (!LPhi) { |
0 |
| 11983 |
std::swap(LHS, RHS); |
0 |
11983 |
std::swap(LHS, RHS); |
0 |
| 11984 |
std::swap(FoundLHS, FoundRHS); |
0 |
11984 |
std::swap(FoundLHS, FoundRHS); |
0 |
| 11985 |
std::swap(LPhi, RPhi); |
0 |
11985 |
std::swap(LPhi, RPhi); |
0 |
| 11986 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
11986 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
| 11987 |
} |
--- |
11987 |
} |
--- |
| 11988 |
|
--- |
11988 |
|
--- |
| 11989 |
assert(LPhi && "LPhi should definitely be a SCEVUnknown Phi!"); |
0 |
11989 |
assert(LPhi && "LPhi should definitely be a SCEVUnknown Phi!"); |
0 |
| 11990 |
const BasicBlock *LBB = LPhi->getParent(); |
0 |
11990 |
const BasicBlock *LBB = LPhi->getParent(); |
0 |
| 11991 |
const SCEVAddRecExpr *RAR = dyn_cast(RHS); |
0 |
11991 |
const SCEVAddRecExpr *RAR = dyn_cast(RHS); |
0 |
| 11992 |
|
--- |
11992 |
|
--- |
| 11993 |
auto ProvedEasily = [&](const SCEV *S1, const SCEV *S2) { |
0 |
11993 |
auto ProvedEasily = [&](const SCEV *S1, const SCEV *S2) { |
0 |
| 11994 |
return isKnownViaNonRecursiveReasoning(Pred, S1, S2) || |
0 |
11994 |
return isKnownViaNonRecursiveReasoning(Pred, S1, S2) || |
0 |
| 11995 |
isImpliedCondOperandsViaRanges(Pred, S1, S2, FoundLHS, FoundRHS) || |
0 |
11995 |
isImpliedCondOperandsViaRanges(Pred, S1, S2, FoundLHS, FoundRHS) || |
0 |
| 11996 |
isImpliedViaOperations(Pred, S1, S2, FoundLHS, FoundRHS, Depth); |
0 |
11996 |
isImpliedViaOperations(Pred, S1, S2, FoundLHS, FoundRHS, Depth); |
0 |
| 11997 |
}; |
0 |
11997 |
}; |
0 |
| 11998 |
|
--- |
11998 |
|
--- |
| 11999 |
if (RPhi && RPhi->getParent() == LBB) { |
0 |
11999 |
if (RPhi && RPhi->getParent() == LBB) { |
0 |
| 12000 |
// Case one: RHS is also a SCEVUnknown Phi from the same basic block. |
--- |
12000 |
// Case one: RHS is also a SCEVUnknown Phi from the same basic block. |
--- |
| 12001 |
// If we compare two Phis from the same block, and for each entry block |
--- |
12001 |
// If we compare two Phis from the same block, and for each entry block |
--- |
| 12002 |
// the predicate is true for incoming values from this block, then the |
--- |
12002 |
// the predicate is true for incoming values from this block, then the |
--- |
| 12003 |
// predicate is also true for the Phis. |
--- |
12003 |
// predicate is also true for the Phis. |
--- |
| 12004 |
for (const BasicBlock *IncBB : predecessors(LBB)) { |
0 |
12004 |
for (const BasicBlock *IncBB : predecessors(LBB)) { |
0 |
| 12005 |
const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB)); |
0 |
12005 |
const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB)); |
0 |
| 12006 |
const SCEV *R = getSCEV(RPhi->getIncomingValueForBlock(IncBB)); |
0 |
12006 |
const SCEV *R = getSCEV(RPhi->getIncomingValueForBlock(IncBB)); |
0 |
| 12007 |
if (!ProvedEasily(L, R)) |
0 |
12007 |
if (!ProvedEasily(L, R)) |
0 |
| 12008 |
return false; |
0 |
12008 |
return false; |
0 |
| 12009 |
} |
--- |
12009 |
} |
--- |
| 12010 |
} else if (RAR && RAR->getLoop()->getHeader() == LBB) { |
0 |
12010 |
} else if (RAR && RAR->getLoop()->getHeader() == LBB) { |
0 |
| 12011 |
// Case two: RHS is also a Phi from the same basic block, and it is an |
--- |
12011 |
// Case two: RHS is also a Phi from the same basic block, and it is an |
--- |
| 12012 |
// AddRec. It means that there is a loop which has both AddRec and Unknown |
--- |
12012 |
// AddRec. It means that there is a loop which has both AddRec and Unknown |
--- |
| 12013 |
// PHIs, for it we can compare incoming values of AddRec from above the loop |
--- |
12013 |
// PHIs, for it we can compare incoming values of AddRec from above the loop |
--- |
| 12014 |
// and latch with their respective incoming values of LPhi. |
--- |
12014 |
// and latch with their respective incoming values of LPhi. |
--- |
| 12015 |
// TODO: Generalize to handle loops with many inputs in a header. |
--- |
12015 |
// TODO: Generalize to handle loops with many inputs in a header. |
--- |
| 12016 |
if (LPhi->getNumIncomingValues() != 2) return false; |
0 |
12016 |
if (LPhi->getNumIncomingValues() != 2) return false; |
0 |
| 12017 |
|
--- |
12017 |
|
--- |
| 12018 |
auto *RLoop = RAR->getLoop(); |
0 |
12018 |
auto *RLoop = RAR->getLoop(); |
0 |
| 12019 |
auto *Predecessor = RLoop->getLoopPredecessor(); |
0 |
12019 |
auto *Predecessor = RLoop->getLoopPredecessor(); |
0 |
| 12020 |
assert(Predecessor && "Loop with AddRec with no predecessor?"); |
0 |
12020 |
assert(Predecessor && "Loop with AddRec with no predecessor?"); |
0 |
| 12021 |
const SCEV *L1 = getSCEV(LPhi->getIncomingValueForBlock(Predecessor)); |
0 |
12021 |
const SCEV *L1 = getSCEV(LPhi->getIncomingValueForBlock(Predecessor)); |
0 |
| 12022 |
if (!ProvedEasily(L1, RAR->getStart())) |
0 |
12022 |
if (!ProvedEasily(L1, RAR->getStart())) |
0 |
| 12023 |
return false; |
0 |
12023 |
return false; |
0 |
| 12024 |
auto *Latch = RLoop->getLoopLatch(); |
0 |
12024 |
auto *Latch = RLoop->getLoopLatch(); |
0 |
| 12025 |
assert(Latch && "Loop with AddRec with no latch?"); |
0 |
12025 |
assert(Latch && "Loop with AddRec with no latch?"); |
0 |
| 12026 |
const SCEV *L2 = getSCEV(LPhi->getIncomingValueForBlock(Latch)); |
0 |
12026 |
const SCEV *L2 = getSCEV(LPhi->getIncomingValueForBlock(Latch)); |
0 |
| 12027 |
if (!ProvedEasily(L2, RAR->getPostIncExpr(*this))) |
0 |
12027 |
if (!ProvedEasily(L2, RAR->getPostIncExpr(*this))) |
0 |
| 12028 |
return false; |
0 |
12028 |
return false; |
0 |
| 12029 |
} else { |
--- |
12029 |
} else { |
--- |
| 12030 |
// In all other cases go over inputs of LHS and compare each of them to RHS, |
--- |
12030 |
// In all other cases go over inputs of LHS and compare each of them to RHS, |
--- |
| 12031 |
// the predicate is true for (LHS, RHS) if it is true for all such pairs. |
--- |
12031 |
// the predicate is true for (LHS, RHS) if it is true for all such pairs. |
--- |
| 12032 |
// At this point RHS is either a non-Phi, or it is a Phi from some block |
--- |
12032 |
// At this point RHS is either a non-Phi, or it is a Phi from some block |
--- |
| 12033 |
// different from LBB. |
--- |
12033 |
// different from LBB. |
--- |
| 12034 |
for (const BasicBlock *IncBB : predecessors(LBB)) { |
0 |
12034 |
for (const BasicBlock *IncBB : predecessors(LBB)) { |
0 |
| 12035 |
// Check that RHS is available in this block. |
--- |
12035 |
// Check that RHS is available in this block. |
--- |
| 12036 |
if (!dominates(RHS, IncBB)) |
0 |
12036 |
if (!dominates(RHS, IncBB)) |
0 |
| 12037 |
return false; |
0 |
12037 |
return false; |
0 |
| 12038 |
const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB)); |
0 |
12038 |
const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB)); |
0 |
| 12039 |
// Make sure L does not refer to a value from a potentially previous |
--- |
12039 |
// Make sure L does not refer to a value from a potentially previous |
--- |
| 12040 |
// iteration of a loop. |
--- |
12040 |
// iteration of a loop. |
--- |
| 12041 |
if (!properlyDominates(L, LBB)) |
0 |
12041 |
if (!properlyDominates(L, LBB)) |
0 |
| 12042 |
return false; |
0 |
12042 |
return false; |
0 |
| 12043 |
if (!ProvedEasily(L, RHS)) |
0 |
12043 |
if (!ProvedEasily(L, RHS)) |
0 |
| 12044 |
return false; |
0 |
12044 |
return false; |
0 |
| 12045 |
} |
--- |
12045 |
} |
--- |
| 12046 |
} |
--- |
12046 |
} |
--- |
| 12047 |
return true; |
0 |
12047 |
return true; |
0 |
| 12048 |
} |
0 |
12048 |
} |
0 |
| 12049 |
|
--- |
12049 |
|
--- |
| 12050 |
bool ScalarEvolution::isImpliedCondOperandsViaShift(ICmpInst::Predicate Pred, |
0 |
12050 |
bool ScalarEvolution::isImpliedCondOperandsViaShift(ICmpInst::Predicate Pred, |
0 |
| 12051 |
const SCEV *LHS, |
--- |
12051 |
const SCEV *LHS, |
--- |
| 12052 |
const SCEV *RHS, |
--- |
12052 |
const SCEV *RHS, |
--- |
| 12053 |
const SCEV *FoundLHS, |
--- |
12053 |
const SCEV *FoundLHS, |
--- |
| 12054 |
const SCEV *FoundRHS) { |
--- |
12054 |
const SCEV *FoundRHS) { |
--- |
| 12055 |
// We want to imply LHS < RHS from LHS < (RHS >> shiftvalue). First, make |
--- |
12055 |
// We want to imply LHS < RHS from LHS < (RHS >> shiftvalue). First, make |
--- |
| 12056 |
// sure that we are dealing with same LHS. |
--- |
12056 |
// sure that we are dealing with same LHS. |
--- |
| 12057 |
if (RHS == FoundRHS) { |
0 |
12057 |
if (RHS == FoundRHS) { |
0 |
| 12058 |
std::swap(LHS, RHS); |
0 |
12058 |
std::swap(LHS, RHS); |
0 |
| 12059 |
std::swap(FoundLHS, FoundRHS); |
0 |
12059 |
std::swap(FoundLHS, FoundRHS); |
0 |
| 12060 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
12060 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
0 |
| 12061 |
} |
--- |
12061 |
} |
--- |
| 12062 |
if (LHS != FoundLHS) |
0 |
12062 |
if (LHS != FoundLHS) |
0 |
| 12063 |
return false; |
0 |
12063 |
return false; |
0 |
| 12064 |
|
--- |
12064 |
|
--- |
| 12065 |
auto *SUFoundRHS = dyn_cast(FoundRHS); |
0 |
12065 |
auto *SUFoundRHS = dyn_cast(FoundRHS); |
0 |
| 12066 |
if (!SUFoundRHS) |
0 |
12066 |
if (!SUFoundRHS) |
0 |
| 12067 |
return false; |
0 |
12067 |
return false; |
0 |
| 12068 |
|
--- |
12068 |
|
--- |
| 12069 |
Value *Shiftee, *ShiftValue; |
--- |
12069 |
Value *Shiftee, *ShiftValue; |
--- |
| 12070 |
|
--- |
12070 |
|
--- |
| 12071 |
using namespace PatternMatch; |
--- |
12071 |
using namespace PatternMatch; |
--- |
| 12072 |
if (match(SUFoundRHS->getValue(), |
0 |
12072 |
if (match(SUFoundRHS->getValue(), |
0 |
| 12073 |
m_LShr(m_Value(Shiftee), m_Value(ShiftValue)))) { |
0 |
12073 |
m_LShr(m_Value(Shiftee), m_Value(ShiftValue)))) { |
0 |
| 12074 |
auto *ShifteeS = getSCEV(Shiftee); |
0 |
12074 |
auto *ShifteeS = getSCEV(Shiftee); |
0 |
| 12075 |
// Prove one of the following: |
--- |
12075 |
// Prove one of the following: |
--- |
| 12076 |
// LHS > shiftvalue) && shiftee <=u RHS ---> LHS
| --- |
12076 |
// LHS > shiftvalue) && shiftee <=u RHS ---> LHS
| --- |
| 12077 |
// LHS <=u (shiftee >> shiftvalue) && shiftee <=u RHS ---> LHS <=u RHS |
--- |
12077 |
// LHS <=u (shiftee >> shiftvalue) && shiftee <=u RHS ---> LHS <=u RHS |
--- |
| 12078 |
// LHS > shiftvalue) && shiftee <=s RHS && shiftee >=s 0 |
--- |
12078 |
// LHS > shiftvalue) && shiftee <=s RHS && shiftee >=s 0 |
--- |
| 12079 |
// ---> LHS
| --- |
12079 |
// ---> LHS
| --- |
| |
| 12080 |
// LHS <=s (shiftee >> shiftvalue) && shiftee <=s RHS && shiftee >=s 0 |
--- |
12080 |
// LHS <=s (shiftee >> shiftvalue) && shiftee <=s RHS && shiftee >=s 0 |
--- |
| 12081 |
// ---> LHS <=s RHS |
--- |
12081 |
// ---> LHS <=s RHS |
--- |
| 12082 |
if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) |
0 |
12082 |
if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) |
0 |
| 12083 |
return isKnownPredicate(ICmpInst::ICMP_ULE, ShifteeS, RHS); |
0 |
12083 |
return isKnownPredicate(ICmpInst::ICMP_ULE, ShifteeS, RHS); |
0 |
| 12084 |
if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) |
0 |
12084 |
if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) |
0 |
| 12085 |
if (isKnownNonNegative(ShifteeS)) |
0 |
12085 |
if (isKnownNonNegative(ShifteeS)) |
0 |
| 12086 |
return isKnownPredicate(ICmpInst::ICMP_SLE, ShifteeS, RHS); |
0 |
12086 |
return isKnownPredicate(ICmpInst::ICMP_SLE, ShifteeS, RHS); |
0 |
| 12087 |
} |
--- |
12087 |
} |
--- |
| 12088 |
|
--- |
12088 |
|
--- |
| 12089 |
return false; |
0 |
12089 |
return false; |
0 |
| 12090 |
} |
--- |
12090 |
} |
--- |
| 12091 |
|
--- |
12091 |
|
--- |
| 12092 |
bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred, |
0 |
12092 |
bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred, |
0 |
| 12093 |
const SCEV *LHS, const SCEV *RHS, |
--- |
12093 |
const SCEV *LHS, const SCEV *RHS, |
--- |
| 12094 |
const SCEV *FoundLHS, |
--- |
12094 |
const SCEV *FoundLHS, |
--- |
| 12095 |
const SCEV *FoundRHS, |
--- |
12095 |
const SCEV *FoundRHS, |
--- |
| 12096 |
const Instruction *CtxI) { |
--- |
12096 |
const Instruction *CtxI) { |
--- |
| 12097 |
if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundLHS, FoundRHS)) |
0 |
12097 |
if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundLHS, FoundRHS)) |
0 |
| 12098 |
return true; |
0 |
12098 |
return true; |
0 |
| 12099 |
|
--- |
12099 |
|
--- |
| 12100 |
if (isImpliedCondOperandsViaNoOverflow(Pred, LHS, RHS, FoundLHS, FoundRHS)) |
0 |
12100 |
if (isImpliedCondOperandsViaNoOverflow(Pred, LHS, RHS, FoundLHS, FoundRHS)) |
0 |
| 12101 |
return true; |
0 |
12101 |
return true; |
0 |
| 12102 |
|
--- |
12102 |
|
--- |
| 12103 |
if (isImpliedCondOperandsViaShift(Pred, LHS, RHS, FoundLHS, FoundRHS)) |
0 |
12103 |
if (isImpliedCondOperandsViaShift(Pred, LHS, RHS, FoundLHS, FoundRHS)) |
0 |
| 12104 |
return true; |
0 |
12104 |
return true; |
0 |
| 12105 |
|
--- |
12105 |
|
--- |
| 12106 |
if (isImpliedCondOperandsViaAddRecStart(Pred, LHS, RHS, FoundLHS, FoundRHS, |
0 |
12106 |
if (isImpliedCondOperandsViaAddRecStart(Pred, LHS, RHS, FoundLHS, FoundRHS, |
0 |
| 12107 |
CtxI)) |
--- |
12107 |
CtxI)) |
--- |
| 12108 |
return true; |
0 |
12108 |
return true; |
0 |
| 12109 |
|
--- |
12109 |
|
--- |
| 12110 |
return isImpliedCondOperandsHelper(Pred, LHS, RHS, |
0 |
12110 |
return isImpliedCondOperandsHelper(Pred, LHS, RHS, |
0 |
| 12111 |
FoundLHS, FoundRHS); |
0 |
12111 |
FoundLHS, FoundRHS); |
0 |
| 12112 |
} |
--- |
12112 |
} |
--- |
| 12113 |
|
--- |
12113 |
|
--- |
| 12114 |
/// Is MaybeMinMaxExpr an (U|S)(Min|Max) of Candidate and some other values? |
--- |
12114 |
/// Is MaybeMinMaxExpr an (U|S)(Min|Max) of Candidate and some other values? |
--- |
| 12115 |
template |
--- |
12115 |
template |
--- |
| 12116 |
static bool IsMinMaxConsistingOf(const SCEV *MaybeMinMaxExpr, |
0 |
12116 |
static bool IsMinMaxConsistingOf(const SCEV *MaybeMinMaxExpr, |
0 |
| 12117 |
const SCEV *Candidate) { |
--- |
12117 |
const SCEV *Candidate) { |
--- |
| 12118 |
const MinMaxExprType *MinMaxExpr = dyn_cast(MaybeMinMaxExpr); |
0 |
12118 |
const MinMaxExprType *MinMaxExpr = dyn_cast(MaybeMinMaxExpr); |
0 |
| 12119 |
if (!MinMaxExpr) |
0 |
12119 |
if (!MinMaxExpr) |
0 |
| 12120 |
return false; |
0 |
12120 |
return false; |
0 |
| 12121 |
|
--- |
12121 |
|
--- |
| 12122 |
return is_contained(MinMaxExpr->operands(), Candidate); |
0 |
12122 |
return is_contained(MinMaxExpr->operands(), Candidate); |
0 |
| 12123 |
} |
--- |
12123 |
} |
--- |
| 12124 |
|
--- |
12124 |
|
--- |
| 12125 |
static bool IsKnownPredicateViaAddRecStart(ScalarEvolution &SE, |
0 |
12125 |
static bool IsKnownPredicateViaAddRecStart(ScalarEvolution &SE, |
0 |
| 12126 |
ICmpInst::Predicate Pred, |
--- |
12126 |
ICmpInst::Predicate Pred, |
--- |
| 12127 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
12127 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
| 12128 |
// If both sides are affine addrecs for the same loop, with equal |
--- |
12128 |
// If both sides are affine addrecs for the same loop, with equal |
--- |
| 12129 |
// steps, and we know the recurrences don't wrap, then we only |
--- |
12129 |
// steps, and we know the recurrences don't wrap, then we only |
--- |
| 12130 |
// need to check the predicate on the starting values. |
--- |
12130 |
// need to check the predicate on the starting values. |
--- |
| 12131 |
|
--- |
12131 |
|
--- |
| 12132 |
if (!ICmpInst::isRelational(Pred)) |
0 |
12132 |
if (!ICmpInst::isRelational(Pred)) |
0 |
| 12133 |
return false; |
0 |
12133 |
return false; |
0 |
| 12134 |
|
--- |
12134 |
|
--- |
| 12135 |
const SCEVAddRecExpr *LAR = dyn_cast(LHS); |
0 |
12135 |
const SCEVAddRecExpr *LAR = dyn_cast(LHS); |
0 |
| 12136 |
if (!LAR) |
0 |
12136 |
if (!LAR) |
0 |
| 12137 |
return false; |
0 |
12137 |
return false; |
0 |
| 12138 |
const SCEVAddRecExpr *RAR = dyn_cast(RHS); |
0 |
12138 |
const SCEVAddRecExpr *RAR = dyn_cast(RHS); |
0 |
| 12139 |
if (!RAR) |
0 |
12139 |
if (!RAR) |
0 |
| 12140 |
return false; |
0 |
12140 |
return false; |
0 |
| 12141 |
if (LAR->getLoop() != RAR->getLoop()) |
0 |
12141 |
if (LAR->getLoop() != RAR->getLoop()) |
0 |
| 12142 |
return false; |
0 |
12142 |
return false; |
0 |
| 12143 |
if (!LAR->isAffine() || !RAR->isAffine()) |
0 |
12143 |
if (!LAR->isAffine() || !RAR->isAffine()) |
0 |
| 12144 |
return false; |
0 |
12144 |
return false; |
0 |
| 12145 |
|
--- |
12145 |
|
--- |
| 12146 |
if (LAR->getStepRecurrence(SE) != RAR->getStepRecurrence(SE)) |
0 |
12146 |
if (LAR->getStepRecurrence(SE) != RAR->getStepRecurrence(SE)) |
0 |
| 12147 |
return false; |
0 |
12147 |
return false; |
0 |
| 12148 |
|
--- |
12148 |
|
--- |
| 12149 |
SCEV::NoWrapFlags NW = ICmpInst::isSigned(Pred) ? |
0 |
12149 |
SCEV::NoWrapFlags NW = ICmpInst::isSigned(Pred) ? |
0 |
| 12150 |
SCEV::FlagNSW : SCEV::FlagNUW; |
0 |
12150 |
SCEV::FlagNSW : SCEV::FlagNUW; |
0 |
| 12151 |
if (!LAR->getNoWrapFlags(NW) || !RAR->getNoWrapFlags(NW)) |
0 |
12151 |
if (!LAR->getNoWrapFlags(NW) || !RAR->getNoWrapFlags(NW)) |
0 |
| 12152 |
return false; |
0 |
12152 |
return false; |
0 |
| 12153 |
|
--- |
12153 |
|
--- |
| 12154 |
return SE.isKnownPredicate(Pred, LAR->getStart(), RAR->getStart()); |
0 |
12154 |
return SE.isKnownPredicate(Pred, LAR->getStart(), RAR->getStart()); |
0 |
| 12155 |
} |
--- |
12155 |
} |
--- |
| 12156 |
|
--- |
12156 |
|
--- |
| 12157 |
/// Is LHS `Pred` RHS true on the virtue of LHS or RHS being a Min or Max |
--- |
12157 |
/// Is LHS `Pred` RHS true on the virtue of LHS or RHS being a Min or Max |
--- |
| 12158 |
/// expression? |
--- |
12158 |
/// expression? |
--- |
| 12159 |
static bool IsKnownPredicateViaMinOrMax(ScalarEvolution &SE, |
0 |
12159 |
static bool IsKnownPredicateViaMinOrMax(ScalarEvolution &SE, |
0 |
| 12160 |
ICmpInst::Predicate Pred, |
--- |
12160 |
ICmpInst::Predicate Pred, |
--- |
| 12161 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
12161 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
| 12162 |
switch (Pred) { |
0 |
12162 |
switch (Pred) { |
0 |
| 12163 |
default: |
0 |
12163 |
default: |
0 |
| 12164 |
return false; |
0 |
12164 |
return false; |
0 |
| 12165 |
|
--- |
12165 |
|
--- |
| 12166 |
case ICmpInst::ICMP_SGE: |
0 |
12166 |
case ICmpInst::ICMP_SGE: |
0 |
| 12167 |
std::swap(LHS, RHS); |
0 |
12167 |
std::swap(LHS, RHS); |
0 |
| 12168 |
[[fallthrough]]; |
--- |
12168 |
[[fallthrough]]; |
--- |
| 12169 |
case ICmpInst::ICMP_SLE: |
0 |
12169 |
case ICmpInst::ICMP_SLE: |
0 |
| 12170 |
return |
--- |
12170 |
return |
--- |
| 12171 |
// min(A, ...) <= A |
--- |
12171 |
// min(A, ...) <= A |
--- |
| 12172 |
IsMinMaxConsistingOf(LHS, RHS) || |
0 |
12172 |
IsMinMaxConsistingOf(LHS, RHS) || |
0 |
| 12173 |
// A <= max(A, ...) |
--- |
12173 |
// A <= max(A, ...) |
--- |
| 12174 |
IsMinMaxConsistingOf(RHS, LHS); |
0 |
12174 |
IsMinMaxConsistingOf(RHS, LHS); |
0 |
| 12175 |
|
--- |
12175 |
|
--- |
| 12176 |
case ICmpInst::ICMP_UGE: |
0 |
12176 |
case ICmpInst::ICMP_UGE: |
0 |
| 12177 |
std::swap(LHS, RHS); |
0 |
12177 |
std::swap(LHS, RHS); |
0 |
| 12178 |
[[fallthrough]]; |
--- |
12178 |
[[fallthrough]]; |
--- |
| 12179 |
case ICmpInst::ICMP_ULE: |
0 |
12179 |
case ICmpInst::ICMP_ULE: |
0 |
| 12180 |
return |
--- |
12180 |
return |
--- |
| 12181 |
// min(A, ...) <= A |
--- |
12181 |
// min(A, ...) <= A |
--- |
| 12182 |
// FIXME: what about umin_seq? |
--- |
12182 |
// FIXME: what about umin_seq? |
--- |
| 12183 |
IsMinMaxConsistingOf(LHS, RHS) || |
0 |
12183 |
IsMinMaxConsistingOf(LHS, RHS) || |
0 |
| 12184 |
// A <= max(A, ...) |
--- |
12184 |
// A <= max(A, ...) |
--- |
| 12185 |
IsMinMaxConsistingOf(RHS, LHS); |
0 |
12185 |
IsMinMaxConsistingOf(RHS, LHS); |
0 |
| 12186 |
} |
--- |
12186 |
} |
--- |
| 12187 |
|
--- |
12187 |
|
--- |
| 12188 |
llvm_unreachable("covered switch fell through?!"); |
--- |
12188 |
llvm_unreachable("covered switch fell through?!"); |
--- |
| 12189 |
} |
--- |
12189 |
} |
--- |
| 12190 |
|
--- |
12190 |
|
--- |
| 12191 |
bool ScalarEvolution::isImpliedViaOperations(ICmpInst::Predicate Pred, |
0 |
12191 |
bool ScalarEvolution::isImpliedViaOperations(ICmpInst::Predicate Pred, |
0 |
| 12192 |
const SCEV *LHS, const SCEV *RHS, |
--- |
12192 |
const SCEV *LHS, const SCEV *RHS, |
--- |
| 12193 |
const SCEV *FoundLHS, |
--- |
12193 |
const SCEV *FoundLHS, |
--- |
| 12194 |
const SCEV *FoundRHS, |
--- |
12194 |
const SCEV *FoundRHS, |
--- |
| 12195 |
unsigned Depth) { |
--- |
12195 |
unsigned Depth) { |
--- |
| 12196 |
assert(getTypeSizeInBits(LHS->getType()) == |
0 |
12196 |
assert(getTypeSizeInBits(LHS->getType()) == |
0 |
| 12197 |
getTypeSizeInBits(RHS->getType()) && |
--- |
12197 |
getTypeSizeInBits(RHS->getType()) && |
--- |
| 12198 |
"LHS and RHS have different sizes?"); |
--- |
12198 |
"LHS and RHS have different sizes?"); |
--- |
| 12199 |
assert(getTypeSizeInBits(FoundLHS->getType()) == |
0 |
12199 |
assert(getTypeSizeInBits(FoundLHS->getType()) == |
0 |
| 12200 |
getTypeSizeInBits(FoundRHS->getType()) && |
--- |
12200 |
getTypeSizeInBits(FoundRHS->getType()) && |
--- |
| 12201 |
"FoundLHS and FoundRHS have different sizes?"); |
--- |
12201 |
"FoundLHS and FoundRHS have different sizes?"); |
--- |
| 12202 |
// We want to avoid hurting the compile time with analysis of too big trees. |
--- |
12202 |
// We want to avoid hurting the compile time with analysis of too big trees. |
--- |
| 12203 |
if (Depth > MaxSCEVOperationsImplicationDepth) |
0 |
12203 |
if (Depth > MaxSCEVOperationsImplicationDepth) |
0 |
| 12204 |
return false; |
0 |
12204 |
return false; |
0 |
| 12205 |
|
--- |
12205 |
|
--- |
| 12206 |
// We only want to work with GT comparison so far. |
--- |
12206 |
// We only want to work with GT comparison so far. |
--- |
| 12207 |
if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT) { |
0 |
12207 |
if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT) { |
0 |
| 12208 |
Pred = CmpInst::getSwappedPredicate(Pred); |
0 |
12208 |
Pred = CmpInst::getSwappedPredicate(Pred); |
0 |
| 12209 |
std::swap(LHS, RHS); |
0 |
12209 |
std::swap(LHS, RHS); |
0 |
| 12210 |
std::swap(FoundLHS, FoundRHS); |
0 |
12210 |
std::swap(FoundLHS, FoundRHS); |
0 |
| 12211 |
} |
--- |
12211 |
} |
--- |
| 12212 |
|
--- |
12212 |
|
--- |
| 12213 |
// For unsigned, try to reduce it to corresponding signed comparison. |
--- |
12213 |
// For unsigned, try to reduce it to corresponding signed comparison. |
--- |
| 12214 |
if (Pred == ICmpInst::ICMP_UGT) |
0 |
12214 |
if (Pred == ICmpInst::ICMP_UGT) |
0 |
| 12215 |
// We can replace unsigned predicate with its signed counterpart if all |
--- |
12215 |
// We can replace unsigned predicate with its signed counterpart if all |
--- |
| 12216 |
// involved values are non-negative. |
--- |
12216 |
// involved values are non-negative. |
--- |
| 12217 |
// TODO: We could have better support for unsigned. |
--- |
12217 |
// TODO: We could have better support for unsigned. |
--- |
| 12218 |
if (isKnownNonNegative(FoundLHS) && isKnownNonNegative(FoundRHS)) { |
0 |
12218 |
if (isKnownNonNegative(FoundLHS) && isKnownNonNegative(FoundRHS)) { |
0 |
| 12219 |
// Knowing that both FoundLHS and FoundRHS are non-negative, and knowing |
--- |
12219 |
// Knowing that both FoundLHS and FoundRHS are non-negative, and knowing |
--- |
| 12220 |
// FoundLHS >u FoundRHS, we also know that FoundLHS >s FoundRHS. Let us |
--- |
12220 |
// FoundLHS >u FoundRHS, we also know that FoundLHS >s FoundRHS. Let us |
--- |
| 12221 |
// use this fact to prove that LHS and RHS are non-negative. |
--- |
12221 |
// use this fact to prove that LHS and RHS are non-negative. |
--- |
| 12222 |
const SCEV *MinusOne = getMinusOne(LHS->getType()); |
0 |
12222 |
const SCEV *MinusOne = getMinusOne(LHS->getType()); |
0 |
| 12223 |
if (isImpliedCondOperands(ICmpInst::ICMP_SGT, LHS, MinusOne, FoundLHS, |
0 |
12223 |
if (isImpliedCondOperands(ICmpInst::ICMP_SGT, LHS, MinusOne, FoundLHS, |
0 |
| 12224 |
FoundRHS) && |
0 |
12224 |
FoundRHS) && |
0 |
| 12225 |
isImpliedCondOperands(ICmpInst::ICMP_SGT, RHS, MinusOne, FoundLHS, |
0 |
12225 |
isImpliedCondOperands(ICmpInst::ICMP_SGT, RHS, MinusOne, FoundLHS, |
0 |
| 12226 |
FoundRHS)) |
--- |
12226 |
FoundRHS)) |
--- |
| 12227 |
Pred = ICmpInst::ICMP_SGT; |
0 |
12227 |
Pred = ICmpInst::ICMP_SGT; |
0 |
| 12228 |
} |
--- |
12228 |
} |
--- |
| 12229 |
|
--- |
12229 |
|
--- |
| 12230 |
if (Pred != ICmpInst::ICMP_SGT) |
0 |
12230 |
if (Pred != ICmpInst::ICMP_SGT) |
0 |
| 12231 |
return false; |
0 |
12231 |
return false; |
0 |
| 12232 |
|
--- |
12232 |
|
--- |
| 12233 |
auto GetOpFromSExt = [&](const SCEV *S) { |
0 |
12233 |
auto GetOpFromSExt = [&](const SCEV *S) { |
0 |
| 12234 |
if (auto *Ext = dyn_cast(S)) |
0 |
12234 |
if (auto *Ext = dyn_cast(S)) |
0 |
| 12235 |
return Ext->getOperand(); |
0 |
12235 |
return Ext->getOperand(); |
0 |
| 12236 |
// TODO: If S is a SCEVConstant then you can cheaply "strip" the sext off |
--- |
12236 |
// TODO: If S is a SCEVConstant then you can cheaply "strip" the sext off |
--- |
| 12237 |
// the constant in some cases. |
--- |
12237 |
// the constant in some cases. |
--- |
| 12238 |
return S; |
0 |
12238 |
return S; |
0 |
| 12239 |
}; |
--- |
12239 |
}; |
--- |
| 12240 |
|
--- |
12240 |
|
--- |
| 12241 |
// Acquire values from extensions. |
--- |
12241 |
// Acquire values from extensions. |
--- |
| 12242 |
auto *OrigLHS = LHS; |
0 |
12242 |
auto *OrigLHS = LHS; |
0 |
| 12243 |
auto *OrigFoundLHS = FoundLHS; |
0 |
12243 |
auto *OrigFoundLHS = FoundLHS; |
0 |
| 12244 |
LHS = GetOpFromSExt(LHS); |
0 |
12244 |
LHS = GetOpFromSExt(LHS); |
0 |
| 12245 |
FoundLHS = GetOpFromSExt(FoundLHS); |
0 |
12245 |
FoundLHS = GetOpFromSExt(FoundLHS); |
0 |
| 12246 |
|
--- |
12246 |
|
--- |
| 12247 |
// Is the SGT predicate can be proved trivially or using the found context. |
--- |
12247 |
// Is the SGT predicate can be proved trivially or using the found context. |
--- |
| 12248 |
auto IsSGTViaContext = [&](const SCEV *S1, const SCEV *S2) { |
0 |
12248 |
auto IsSGTViaContext = [&](const SCEV *S1, const SCEV *S2) { |
0 |
| 12249 |
return isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGT, S1, S2) || |
0 |
12249 |
return isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGT, S1, S2) || |
0 |
| 12250 |
isImpliedViaOperations(ICmpInst::ICMP_SGT, S1, S2, OrigFoundLHS, |
0 |
12250 |
isImpliedViaOperations(ICmpInst::ICMP_SGT, S1, S2, OrigFoundLHS, |
0 |
| 12251 |
FoundRHS, Depth + 1); |
0 |
12251 |
FoundRHS, Depth + 1); |
0 |
| 12252 |
}; |
0 |
12252 |
}; |
0 |
| 12253 |
|
--- |
12253 |
|
--- |
| 12254 |
if (auto *LHSAddExpr = dyn_cast(LHS)) { |
0 |
12254 |
if (auto *LHSAddExpr = dyn_cast(LHS)) { |
0 |
| 12255 |
// We want to avoid creation of any new non-constant SCEV. Since we are |
--- |
12255 |
// We want to avoid creation of any new non-constant SCEV. Since we are |
--- |
| 12256 |
// going to compare the operands to RHS, we should be certain that we don't |
--- |
12256 |
// going to compare the operands to RHS, we should be certain that we don't |
--- |
| 12257 |
// need any size extensions for this. So let's decline all cases when the |
--- |
12257 |
// need any size extensions for this. So let's decline all cases when the |
--- |
| 12258 |
// sizes of types of LHS and RHS do not match. |
--- |
12258 |
// sizes of types of LHS and RHS do not match. |
--- |
| 12259 |
// TODO: Maybe try to get RHS from sext to catch more cases? |
--- |
12259 |
// TODO: Maybe try to get RHS from sext to catch more cases? |
--- |
| 12260 |
if (getTypeSizeInBits(LHS->getType()) != getTypeSizeInBits(RHS->getType())) |
0 |
12260 |
if (getTypeSizeInBits(LHS->getType()) != getTypeSizeInBits(RHS->getType())) |
0 |
| 12261 |
return false; |
0 |
12261 |
return false; |
0 |
| 12262 |
|
--- |
12262 |
|
--- |
| 12263 |
// Should not overflow. |
--- |
12263 |
// Should not overflow. |
--- |
| 12264 |
if (!LHSAddExpr->hasNoSignedWrap()) |
0 |
12264 |
if (!LHSAddExpr->hasNoSignedWrap()) |
0 |
| 12265 |
return false; |
0 |
12265 |
return false; |
0 |
| 12266 |
|
--- |
12266 |
|
--- |
| 12267 |
auto *LL = LHSAddExpr->getOperand(0); |
0 |
12267 |
auto *LL = LHSAddExpr->getOperand(0); |
0 |
| 12268 |
auto *LR = LHSAddExpr->getOperand(1); |
0 |
12268 |
auto *LR = LHSAddExpr->getOperand(1); |
0 |
| 12269 |
auto *MinusOne = getMinusOne(RHS->getType()); |
0 |
12269 |
auto *MinusOne = getMinusOne(RHS->getType()); |
0 |
| 12270 |
|
--- |
12270 |
|
--- |
| 12271 |
// Checks that S1 >= 0 && S2 > RHS, trivially or using the found context. |
--- |
12271 |
// Checks that S1 >= 0 && S2 > RHS, trivially or using the found context. |
--- |
| 12272 |
auto IsSumGreaterThanRHS = [&](const SCEV *S1, const SCEV *S2) { |
0 |
12272 |
auto IsSumGreaterThanRHS = [&](const SCEV *S1, const SCEV *S2) { |
0 |
| 12273 |
return IsSGTViaContext(S1, MinusOne) && IsSGTViaContext(S2, RHS); |
0 |
12273 |
return IsSGTViaContext(S1, MinusOne) && IsSGTViaContext(S2, RHS); |
0 |
| 12274 |
}; |
0 |
12274 |
}; |
0 |
| 12275 |
// Try to prove the following rule: |
--- |
12275 |
// Try to prove the following rule: |
--- |
| 12276 |
// (LHS = LL + LR) && (LL >= 0) && (LR > RHS) => (LHS > RHS). |
--- |
12276 |
// (LHS = LL + LR) && (LL >= 0) && (LR > RHS) => (LHS > RHS). |
--- |
| 12277 |
// (LHS = LL + LR) && (LR >= 0) && (LL > RHS) => (LHS > RHS). |
--- |
12277 |
// (LHS = LL + LR) && (LR >= 0) && (LL > RHS) => (LHS > RHS). |
--- |
| 12278 |
if (IsSumGreaterThanRHS(LL, LR) || IsSumGreaterThanRHS(LR, LL)) |
0 |
12278 |
if (IsSumGreaterThanRHS(LL, LR) || IsSumGreaterThanRHS(LR, LL)) |
0 |
| 12279 |
return true; |
0 |
12279 |
return true; |
0 |
| 12280 |
} else if (auto *LHSUnknownExpr = dyn_cast(LHS)) { |
0 |
12280 |
} else if (auto *LHSUnknownExpr = dyn_cast(LHS)) { |
0 |
| 12281 |
Value *LL, *LR; |
--- |
12281 |
Value *LL, *LR; |
--- |
| 12282 |
// FIXME: Once we have SDiv implemented, we can get rid of this matching. |
--- |
12282 |
// FIXME: Once we have SDiv implemented, we can get rid of this matching. |
--- |
| 12283 |
|
--- |
12283 |
|
--- |
| 12284 |
using namespace llvm::PatternMatch; |
--- |
12284 |
using namespace llvm::PatternMatch; |
--- |
| 12285 |
|
--- |
12285 |
|
--- |
| 12286 |
if (match(LHSUnknownExpr->getValue(), m_SDiv(m_Value(LL), m_Value(LR)))) { |
0 |
12286 |
if (match(LHSUnknownExpr->getValue(), m_SDiv(m_Value(LL), m_Value(LR)))) { |
0 |
| 12287 |
// Rules for division. |
--- |
12287 |
// Rules for division. |
--- |
| 12288 |
// We are going to perform some comparisons with Denominator and its |
--- |
12288 |
// We are going to perform some comparisons with Denominator and its |
--- |
| 12289 |
// derivative expressions. In general case, creating a SCEV for it may |
--- |
12289 |
// derivative expressions. In general case, creating a SCEV for it may |
--- |
| 12290 |
// lead to a complex analysis of the entire graph, and in particular it |
--- |
12290 |
// lead to a complex analysis of the entire graph, and in particular it |
--- |
| 12291 |
// can request trip count recalculation for the same loop. This would |
--- |
12291 |
// can request trip count recalculation for the same loop. This would |
--- |
| 12292 |
// cache as SCEVCouldNotCompute to avoid the infinite recursion. To avoid |
--- |
12292 |
// cache as SCEVCouldNotCompute to avoid the infinite recursion. To avoid |
--- |
| 12293 |
// this, we only want to create SCEVs that are constants in this section. |
--- |
12293 |
// this, we only want to create SCEVs that are constants in this section. |
--- |
| 12294 |
// So we bail if Denominator is not a constant. |
--- |
12294 |
// So we bail if Denominator is not a constant. |
--- |
| 12295 |
if (!isa(LR)) |
0 |
12295 |
if (!isa(LR)) |
0 |
| 12296 |
return false; |
0 |
12296 |
return false; |
0 |
| 12297 |
|
--- |
12297 |
|
--- |
| 12298 |
auto *Denominator = cast(getSCEV(LR)); |
0 |
12298 |
auto *Denominator = cast(getSCEV(LR)); |
0 |
| 12299 |
|
--- |
12299 |
|
--- |
| 12300 |
// We want to make sure that LHS = FoundLHS / Denominator. If it is so, |
--- |
12300 |
// We want to make sure that LHS = FoundLHS / Denominator. If it is so, |
--- |
| 12301 |
// then a SCEV for the numerator already exists and matches with FoundLHS. |
--- |
12301 |
// then a SCEV for the numerator already exists and matches with FoundLHS. |
--- |
| 12302 |
auto *Numerator = getExistingSCEV(LL); |
0 |
12302 |
auto *Numerator = getExistingSCEV(LL); |
0 |
| 12303 |
if (!Numerator || Numerator->getType() != FoundLHS->getType()) |
0 |
12303 |
if (!Numerator || Numerator->getType() != FoundLHS->getType()) |
0 |
| 12304 |
return false; |
0 |
12304 |
return false; |
0 |
| 12305 |
|
--- |
12305 |
|
--- |
| 12306 |
// Make sure that the numerator matches with FoundLHS and the denominator |
--- |
12306 |
// Make sure that the numerator matches with FoundLHS and the denominator |
--- |
| 12307 |
// is positive. |
--- |
12307 |
// is positive. |
--- |
| 12308 |
if (!HasSameValue(Numerator, FoundLHS) || !isKnownPositive(Denominator)) |
0 |
12308 |
if (!HasSameValue(Numerator, FoundLHS) || !isKnownPositive(Denominator)) |
0 |
| 12309 |
return false; |
0 |
12309 |
return false; |
0 |
| 12310 |
|
--- |
12310 |
|
--- |
| 12311 |
auto *DTy = Denominator->getType(); |
0 |
12311 |
auto *DTy = Denominator->getType(); |
0 |
| 12312 |
auto *FRHSTy = FoundRHS->getType(); |
0 |
12312 |
auto *FRHSTy = FoundRHS->getType(); |
0 |
| 12313 |
if (DTy->isPointerTy() != FRHSTy->isPointerTy()) |
0 |
12313 |
if (DTy->isPointerTy() != FRHSTy->isPointerTy()) |
0 |
| 12314 |
// One of types is a pointer and another one is not. We cannot extend |
--- |
12314 |
// One of types is a pointer and another one is not. We cannot extend |
--- |
| 12315 |
// them properly to a wider type, so let us just reject this case. |
--- |
12315 |
// them properly to a wider type, so let us just reject this case. |
--- |
| 12316 |
// TODO: Usage of getEffectiveSCEVType for DTy, FRHSTy etc should help |
--- |
12316 |
// TODO: Usage of getEffectiveSCEVType for DTy, FRHSTy etc should help |
--- |
| 12317 |
// to avoid this check. |
--- |
12317 |
// to avoid this check. |
--- |
| 12318 |
return false; |
0 |
12318 |
return false; |
0 |
| 12319 |
|
--- |
12319 |
|
--- |
| 12320 |
// Given that: |
--- |
12320 |
// Given that: |
--- |
| 12321 |
// FoundLHS > FoundRHS, LHS = FoundLHS / Denominator, Denominator > 0. |
--- |
12321 |
// FoundLHS > FoundRHS, LHS = FoundLHS / Denominator, Denominator > 0. |
--- |
| 12322 |
auto *WTy = getWiderType(DTy, FRHSTy); |
0 |
12322 |
auto *WTy = getWiderType(DTy, FRHSTy); |
0 |
| 12323 |
auto *DenominatorExt = getNoopOrSignExtend(Denominator, WTy); |
0 |
12323 |
auto *DenominatorExt = getNoopOrSignExtend(Denominator, WTy); |
0 |
| 12324 |
auto *FoundRHSExt = getNoopOrSignExtend(FoundRHS, WTy); |
0 |
12324 |
auto *FoundRHSExt = getNoopOrSignExtend(FoundRHS, WTy); |
0 |
| 12325 |
|
--- |
12325 |
|
--- |
| 12326 |
// Try to prove the following rule: |
--- |
12326 |
// Try to prove the following rule: |
--- |
| 12327 |
// (FoundRHS > Denominator - 2) && (RHS <= 0) => (LHS > RHS). |
--- |
12327 |
// (FoundRHS > Denominator - 2) && (RHS <= 0) => (LHS > RHS). |
--- |
| 12328 |
// For example, given that FoundLHS > 2. It means that FoundLHS is at |
--- |
12328 |
// For example, given that FoundLHS > 2. It means that FoundLHS is at |
--- |
| 12329 |
// least 3. If we divide it by Denominator < 4, we will have at least 1. |
--- |
12329 |
// least 3. If we divide it by Denominator < 4, we will have at least 1. |
--- |
| 12330 |
auto *DenomMinusTwo = getMinusSCEV(DenominatorExt, getConstant(WTy, 2)); |
0 |
12330 |
auto *DenomMinusTwo = getMinusSCEV(DenominatorExt, getConstant(WTy, 2)); |
0 |
| 12331 |
if (isKnownNonPositive(RHS) && |
0 |
12331 |
if (isKnownNonPositive(RHS) && |
0 |
| 12332 |
IsSGTViaContext(FoundRHSExt, DenomMinusTwo)) |
0 |
12332 |
IsSGTViaContext(FoundRHSExt, DenomMinusTwo)) |
0 |
| 12333 |
return true; |
0 |
12333 |
return true; |
0 |
| 12334 |
|
--- |
12334 |
|
--- |
| 12335 |
// Try to prove the following rule: |
--- |
12335 |
// Try to prove the following rule: |
--- |
| 12336 |
// (FoundRHS > -1 - Denominator) && (RHS < 0) => (LHS > RHS). |
--- |
12336 |
// (FoundRHS > -1 - Denominator) && (RHS < 0) => (LHS > RHS). |
--- |
| 12337 |
// For example, given that FoundLHS > -3. Then FoundLHS is at least -2. |
--- |
12337 |
// For example, given that FoundLHS > -3. Then FoundLHS is at least -2. |
--- |
| 12338 |
// If we divide it by Denominator > 2, then: |
--- |
12338 |
// If we divide it by Denominator > 2, then: |
--- |
| 12339 |
// 1. If FoundLHS is negative, then the result is 0. |
--- |
12339 |
// 1. If FoundLHS is negative, then the result is 0. |
--- |
| 12340 |
// 2. If FoundLHS is non-negative, then the result is non-negative. |
--- |
12340 |
// 2. If FoundLHS is non-negative, then the result is non-negative. |
--- |
| 12341 |
// Anyways, the result is non-negative. |
--- |
12341 |
// Anyways, the result is non-negative. |
--- |
| 12342 |
auto *MinusOne = getMinusOne(WTy); |
0 |
12342 |
auto *MinusOne = getMinusOne(WTy); |
0 |
| 12343 |
auto *NegDenomMinusOne = getMinusSCEV(MinusOne, DenominatorExt); |
0 |
12343 |
auto *NegDenomMinusOne = getMinusSCEV(MinusOne, DenominatorExt); |
0 |
| 12344 |
if (isKnownNegative(RHS) && |
0 |
12344 |
if (isKnownNegative(RHS) && |
0 |
| 12345 |
IsSGTViaContext(FoundRHSExt, NegDenomMinusOne)) |
0 |
12345 |
IsSGTViaContext(FoundRHSExt, NegDenomMinusOne)) |
0 |
| 12346 |
return true; |
0 |
12346 |
return true; |
0 |
| 12347 |
} |
--- |
12347 |
} |
--- |
| 12348 |
} |
--- |
12348 |
} |
--- |
| 12349 |
|
--- |
12349 |
|
--- |
| 12350 |
// If our expression contained SCEVUnknown Phis, and we split it down and now |
--- |
12350 |
// If our expression contained SCEVUnknown Phis, and we split it down and now |
--- |
| 12351 |
// need to prove something for them, try to prove the predicate for every |
--- |
12351 |
// need to prove something for them, try to prove the predicate for every |
--- |
| 12352 |
// possible incoming values of those Phis. |
--- |
12352 |
// possible incoming values of those Phis. |
--- |
| 12353 |
if (isImpliedViaMerge(Pred, OrigLHS, RHS, OrigFoundLHS, FoundRHS, Depth + 1)) |
0 |
12353 |
if (isImpliedViaMerge(Pred, OrigLHS, RHS, OrigFoundLHS, FoundRHS, Depth + 1)) |
0 |
| 12354 |
return true; |
0 |
12354 |
return true; |
0 |
| 12355 |
|
--- |
12355 |
|
--- |
| 12356 |
return false; |
0 |
12356 |
return false; |
0 |
| 12357 |
} |
--- |
12357 |
} |
--- |
| 12358 |
|
--- |
12358 |
|
--- |
| 12359 |
static bool isKnownPredicateExtendIdiom(ICmpInst::Predicate Pred, |
0 |
12359 |
static bool isKnownPredicateExtendIdiom(ICmpInst::Predicate Pred, |
0 |
| 12360 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
12360 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
| 12361 |
// zext x u<= sext x, sext x s<= zext x |
--- |
12361 |
// zext x u<= sext x, sext x s<= zext x |
--- |
| 12362 |
switch (Pred) { |
0 |
12362 |
switch (Pred) { |
0 |
| 12363 |
case ICmpInst::ICMP_SGE: |
0 |
12363 |
case ICmpInst::ICMP_SGE: |
0 |
| 12364 |
std::swap(LHS, RHS); |
0 |
12364 |
std::swap(LHS, RHS); |
0 |
| 12365 |
[[fallthrough]]; |
--- |
12365 |
[[fallthrough]]; |
--- |
| 12366 |
case ICmpInst::ICMP_SLE: { |
0 |
12366 |
case ICmpInst::ICMP_SLE: { |
0 |
| 12367 |
// If operand >=s 0 then ZExt == SExt. If operand
| --- |
12367 |
// If operand >=s 0 then ZExt == SExt. If operand
| --- |
| |
| 12368 |
const SCEVSignExtendExpr *SExt = dyn_cast(LHS); |
0 |
12368 |
const SCEVSignExtendExpr *SExt = dyn_cast(LHS); |
0 |
| 12369 |
const SCEVZeroExtendExpr *ZExt = dyn_cast(RHS); |
0 |
12369 |
const SCEVZeroExtendExpr *ZExt = dyn_cast(RHS); |
0 |
| 12370 |
if (SExt && ZExt && SExt->getOperand() == ZExt->getOperand()) |
0 |
12370 |
if (SExt && ZExt && SExt->getOperand() == ZExt->getOperand()) |
0 |
| 12371 |
return true; |
0 |
12371 |
return true; |
0 |
| 12372 |
break; |
0 |
12372 |
break; |
0 |
| 12373 |
} |
--- |
12373 |
} |
--- |
| 12374 |
case ICmpInst::ICMP_UGE: |
0 |
12374 |
case ICmpInst::ICMP_UGE: |
0 |
| 12375 |
std::swap(LHS, RHS); |
0 |
12375 |
std::swap(LHS, RHS); |
0 |
| 12376 |
[[fallthrough]]; |
--- |
12376 |
[[fallthrough]]; |
--- |
| 12377 |
case ICmpInst::ICMP_ULE: { |
0 |
12377 |
case ICmpInst::ICMP_ULE: { |
0 |
| 12378 |
// If operand >=s 0 then ZExt == SExt. If operand
| --- |
12378 |
// If operand >=s 0 then ZExt == SExt. If operand
| --- |
| |
| 12379 |
const SCEVZeroExtendExpr *ZExt = dyn_cast(LHS); |
0 |
12379 |
const SCEVZeroExtendExpr *ZExt = dyn_cast(LHS); |
0 |
| 12380 |
const SCEVSignExtendExpr *SExt = dyn_cast(RHS); |
0 |
12380 |
const SCEVSignExtendExpr *SExt = dyn_cast(RHS); |
0 |
| 12381 |
if (SExt && ZExt && SExt->getOperand() == ZExt->getOperand()) |
0 |
12381 |
if (SExt && ZExt && SExt->getOperand() == ZExt->getOperand()) |
0 |
| 12382 |
return true; |
0 |
12382 |
return true; |
0 |
| 12383 |
break; |
0 |
12383 |
break; |
0 |
| 12384 |
} |
--- |
12384 |
} |
--- |
| 12385 |
default: |
0 |
12385 |
default: |
0 |
| 12386 |
break; |
0 |
12386 |
break; |
0 |
| 12387 |
}; |
--- |
12387 |
}; |
--- |
| 12388 |
return false; |
0 |
12388 |
return false; |
0 |
| 12389 |
} |
--- |
12389 |
} |
--- |
| 12390 |
|
--- |
12390 |
|
--- |
| 12391 |
bool |
--- |
12391 |
bool |
--- |
| 12392 |
ScalarEvolution::isKnownViaNonRecursiveReasoning(ICmpInst::Predicate Pred, |
0 |
12392 |
ScalarEvolution::isKnownViaNonRecursiveReasoning(ICmpInst::Predicate Pred, |
0 |
| 12393 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
12393 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
| 12394 |
return isKnownPredicateExtendIdiom(Pred, LHS, RHS) || |
0 |
12394 |
return isKnownPredicateExtendIdiom(Pred, LHS, RHS) || |
0 |
| 12395 |
isKnownPredicateViaConstantRanges(Pred, LHS, RHS) || |
0 |
12395 |
isKnownPredicateViaConstantRanges(Pred, LHS, RHS) || |
0 |
| 12396 |
IsKnownPredicateViaMinOrMax(*this, Pred, LHS, RHS) || |
0 |
12396 |
IsKnownPredicateViaMinOrMax(*this, Pred, LHS, RHS) || |
0 |
| 12397 |
IsKnownPredicateViaAddRecStart(*this, Pred, LHS, RHS) || |
0 |
12397 |
IsKnownPredicateViaAddRecStart(*this, Pred, LHS, RHS) || |
0 |
| 12398 |
isKnownPredicateViaNoOverflow(Pred, LHS, RHS); |
0 |
12398 |
isKnownPredicateViaNoOverflow(Pred, LHS, RHS); |
0 |
| 12399 |
} |
--- |
12399 |
} |
--- |
| 12400 |
|
--- |
12400 |
|
--- |
| 12401 |
bool |
--- |
12401 |
bool |
--- |
| 12402 |
ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, |
0 |
12402 |
ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, |
0 |
| 12403 |
const SCEV *LHS, const SCEV *RHS, |
--- |
12403 |
const SCEV *LHS, const SCEV *RHS, |
--- |
| 12404 |
const SCEV *FoundLHS, |
--- |
12404 |
const SCEV *FoundLHS, |
--- |
| 12405 |
const SCEV *FoundRHS) { |
--- |
12405 |
const SCEV *FoundRHS) { |
--- |
| 12406 |
switch (Pred) { |
0 |
12406 |
switch (Pred) { |
0 |
| 12407 |
default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
0 |
12407 |
default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
0 |
| 12408 |
case ICmpInst::ICMP_EQ: |
0 |
12408 |
case ICmpInst::ICMP_EQ: |
0 |
| 12409 |
case ICmpInst::ICMP_NE: |
--- |
12409 |
case ICmpInst::ICMP_NE: |
--- |
| 12410 |
if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS)) |
0 |
12410 |
if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS)) |
0 |
| 12411 |
return true; |
0 |
12411 |
return true; |
0 |
| 12412 |
break; |
0 |
12412 |
break; |
0 |
| 12413 |
case ICmpInst::ICMP_SLT: |
0 |
12413 |
case ICmpInst::ICMP_SLT: |
0 |
| 12414 |
case ICmpInst::ICMP_SLE: |
--- |
12414 |
case ICmpInst::ICMP_SLE: |
--- |
| 12415 |
if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SLE, LHS, FoundLHS) && |
0 |
12415 |
if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SLE, LHS, FoundLHS) && |
0 |
| 12416 |
isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGE, RHS, FoundRHS)) |
0 |
12416 |
isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGE, RHS, FoundRHS)) |
0 |
| 12417 |
return true; |
0 |
12417 |
return true; |
0 |
| 12418 |
break; |
0 |
12418 |
break; |
0 |
| 12419 |
case ICmpInst::ICMP_SGT: |
0 |
12419 |
case ICmpInst::ICMP_SGT: |
0 |
| 12420 |
case ICmpInst::ICMP_SGE: |
--- |
12420 |
case ICmpInst::ICMP_SGE: |
--- |
| 12421 |
if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGE, LHS, FoundLHS) && |
0 |
12421 |
if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGE, LHS, FoundLHS) && |
0 |
| 12422 |
isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SLE, RHS, FoundRHS)) |
0 |
12422 |
isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SLE, RHS, FoundRHS)) |
0 |
| 12423 |
return true; |
0 |
12423 |
return true; |
0 |
| 12424 |
break; |
0 |
12424 |
break; |
0 |
| 12425 |
case ICmpInst::ICMP_ULT: |
0 |
12425 |
case ICmpInst::ICMP_ULT: |
0 |
| 12426 |
case ICmpInst::ICMP_ULE: |
--- |
12426 |
case ICmpInst::ICMP_ULE: |
--- |
| 12427 |
if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, LHS, FoundLHS) && |
0 |
12427 |
if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, LHS, FoundLHS) && |
0 |
| 12428 |
isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_UGE, RHS, FoundRHS)) |
0 |
12428 |
isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_UGE, RHS, FoundRHS)) |
0 |
| 12429 |
return true; |
0 |
12429 |
return true; |
0 |
| 12430 |
break; |
0 |
12430 |
break; |
0 |
| 12431 |
case ICmpInst::ICMP_UGT: |
0 |
12431 |
case ICmpInst::ICMP_UGT: |
0 |
| 12432 |
case ICmpInst::ICMP_UGE: |
--- |
12432 |
case ICmpInst::ICMP_UGE: |
--- |
| 12433 |
if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_UGE, LHS, FoundLHS) && |
0 |
12433 |
if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_UGE, LHS, FoundLHS) && |
0 |
| 12434 |
isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, RHS, FoundRHS)) |
0 |
12434 |
isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, RHS, FoundRHS)) |
0 |
| 12435 |
return true; |
0 |
12435 |
return true; |
0 |
| 12436 |
break; |
0 |
12436 |
break; |
0 |
| 12437 |
} |
--- |
12437 |
} |
--- |
| 12438 |
|
--- |
12438 |
|
--- |
| 12439 |
// Maybe it can be proved via operations? |
--- |
12439 |
// Maybe it can be proved via operations? |
--- |
| 12440 |
if (isImpliedViaOperations(Pred, LHS, RHS, FoundLHS, FoundRHS)) |
0 |
12440 |
if (isImpliedViaOperations(Pred, LHS, RHS, FoundLHS, FoundRHS)) |
0 |
| 12441 |
return true; |
0 |
12441 |
return true; |
0 |
| 12442 |
|
--- |
12442 |
|
--- |
| 12443 |
return false; |
0 |
12443 |
return false; |
0 |
| 12444 |
} |
--- |
12444 |
} |
--- |
| 12445 |
|
--- |
12445 |
|
--- |
| 12446 |
bool ScalarEvolution::isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred, |
0 |
12446 |
bool ScalarEvolution::isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred, |
0 |
| 12447 |
const SCEV *LHS, |
--- |
12447 |
const SCEV *LHS, |
--- |
| 12448 |
const SCEV *RHS, |
--- |
12448 |
const SCEV *RHS, |
--- |
| 12449 |
const SCEV *FoundLHS, |
--- |
12449 |
const SCEV *FoundLHS, |
--- |
| 12450 |
const SCEV *FoundRHS) { |
--- |
12450 |
const SCEV *FoundRHS) { |
--- |
| 12451 |
if (!isa(RHS) || !isa(FoundRHS)) |
0 |
12451 |
if (!isa(RHS) || !isa(FoundRHS)) |
0 |
| 12452 |
// The restriction on `FoundRHS` be lifted easily -- it exists only to |
--- |
12452 |
// The restriction on `FoundRHS` be lifted easily -- it exists only to |
--- |
| 12453 |
// reduce the compile time impact of this optimization. |
--- |
12453 |
// reduce the compile time impact of this optimization. |
--- |
| 12454 |
return false; |
0 |
12454 |
return false; |
0 |
| 12455 |
|
--- |
12455 |
|
--- |
| 12456 |
std::optional Addend = computeConstantDifference(LHS, FoundLHS); |
0 |
12456 |
std::optional Addend = computeConstantDifference(LHS, FoundLHS); |
0 |
| 12457 |
if (!Addend) |
0 |
12457 |
if (!Addend) |
0 |
| 12458 |
return false; |
0 |
12458 |
return false; |
0 |
| 12459 |
|
--- |
12459 |
|
--- |
| 12460 |
const APInt &ConstFoundRHS = cast(FoundRHS)->getAPInt(); |
0 |
12460 |
const APInt &ConstFoundRHS = cast(FoundRHS)->getAPInt(); |
0 |
| 12461 |
|
--- |
12461 |
|
--- |
| 12462 |
// `FoundLHSRange` is the range we know `FoundLHS` to be in by virtue of the |
--- |
12462 |
// `FoundLHSRange` is the range we know `FoundLHS` to be in by virtue of the |
--- |
| 12463 |
// antecedent "`FoundLHS` `Pred` `FoundRHS`". |
--- |
12463 |
// antecedent "`FoundLHS` `Pred` `FoundRHS`". |
--- |
| 12464 |
ConstantRange FoundLHSRange = |
--- |
12464 |
ConstantRange FoundLHSRange = |
--- |
| 12465 |
ConstantRange::makeExactICmpRegion(Pred, ConstFoundRHS); |
0 |
12465 |
ConstantRange::makeExactICmpRegion(Pred, ConstFoundRHS); |
0 |
| 12466 |
|
--- |
12466 |
|
--- |
| 12467 |
// Since `LHS` is `FoundLHS` + `Addend`, we can compute a range for `LHS`: |
--- |
12467 |
// Since `LHS` is `FoundLHS` + `Addend`, we can compute a range for `LHS`: |
--- |
| 12468 |
ConstantRange LHSRange = FoundLHSRange.add(ConstantRange(*Addend)); |
0 |
12468 |
ConstantRange LHSRange = FoundLHSRange.add(ConstantRange(*Addend)); |
0 |
| 12469 |
|
--- |
12469 |
|
--- |
| 12470 |
// We can also compute the range of values for `LHS` that satisfy the |
--- |
12470 |
// We can also compute the range of values for `LHS` that satisfy the |
--- |
| 12471 |
// consequent, "`LHS` `Pred` `RHS`": |
--- |
12471 |
// consequent, "`LHS` `Pred` `RHS`": |
--- |
| 12472 |
const APInt &ConstRHS = cast(RHS)->getAPInt(); |
0 |
12472 |
const APInt &ConstRHS = cast(RHS)->getAPInt(); |
0 |
| 12473 |
// The antecedent implies the consequent if every value of `LHS` that |
--- |
12473 |
// The antecedent implies the consequent if every value of `LHS` that |
--- |
| 12474 |
// satisfies the antecedent also satisfies the consequent. |
--- |
12474 |
// satisfies the antecedent also satisfies the consequent. |
--- |
| 12475 |
return LHSRange.icmp(Pred, ConstRHS); |
0 |
12475 |
return LHSRange.icmp(Pred, ConstRHS); |
0 |
| 12476 |
} |
0 |
12476 |
} |
0 |
| 12477 |
|
--- |
12477 |
|
--- |
| 12478 |
bool ScalarEvolution::canIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride, |
0 |
12478 |
bool ScalarEvolution::canIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride, |
0 |
| 12479 |
bool IsSigned) { |
--- |
12479 |
bool IsSigned) { |
--- |
| 12480 |
assert(isKnownPositive(Stride) && "Positive stride expected!"); |
0 |
12480 |
assert(isKnownPositive(Stride) && "Positive stride expected!"); |
0 |
| 12481 |
|
--- |
12481 |
|
--- |
| 12482 |
unsigned BitWidth = getTypeSizeInBits(RHS->getType()); |
0 |
12482 |
unsigned BitWidth = getTypeSizeInBits(RHS->getType()); |
0 |
| 12483 |
const SCEV *One = getOne(Stride->getType()); |
0 |
12483 |
const SCEV *One = getOne(Stride->getType()); |
0 |
| 12484 |
|
--- |
12484 |
|
--- |
| 12485 |
if (IsSigned) { |
0 |
12485 |
if (IsSigned) { |
0 |
| 12486 |
APInt MaxRHS = getSignedRangeMax(RHS); |
0 |
12486 |
APInt MaxRHS = getSignedRangeMax(RHS); |
0 |
| 12487 |
APInt MaxValue = APInt::getSignedMaxValue(BitWidth); |
0 |
12487 |
APInt MaxValue = APInt::getSignedMaxValue(BitWidth); |
0 |
| 12488 |
APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One)); |
0 |
12488 |
APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One)); |
0 |
| 12489 |
|
--- |
12489 |
|
--- |
| 12490 |
// SMaxRHS + SMaxStrideMinusOne > SMaxValue => overflow! |
--- |
12490 |
// SMaxRHS + SMaxStrideMinusOne > SMaxValue => overflow! |
--- |
| 12491 |
return (std::move(MaxValue) - MaxStrideMinusOne).slt(MaxRHS); |
0 |
12491 |
return (std::move(MaxValue) - MaxStrideMinusOne).slt(MaxRHS); |
0 |
| 12492 |
} |
0 |
12492 |
} |
0 |
| 12493 |
|
--- |
12493 |
|
--- |
| 12494 |
APInt MaxRHS = getUnsignedRangeMax(RHS); |
0 |
12494 |
APInt MaxRHS = getUnsignedRangeMax(RHS); |
0 |
| 12495 |
APInt MaxValue = APInt::getMaxValue(BitWidth); |
0 |
12495 |
APInt MaxValue = APInt::getMaxValue(BitWidth); |
0 |
| 12496 |
APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One)); |
0 |
12496 |
APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One)); |
0 |
| 12497 |
|
--- |
12497 |
|
--- |
| 12498 |
// UMaxRHS + UMaxStrideMinusOne > UMaxValue => overflow! |
--- |
12498 |
// UMaxRHS + UMaxStrideMinusOne > UMaxValue => overflow! |
--- |
| 12499 |
return (std::move(MaxValue) - MaxStrideMinusOne).ult(MaxRHS); |
0 |
12499 |
return (std::move(MaxValue) - MaxStrideMinusOne).ult(MaxRHS); |
0 |
| 12500 |
} |
0 |
12500 |
} |
0 |
| 12501 |
|
--- |
12501 |
|
--- |
| 12502 |
bool ScalarEvolution::canIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride, |
0 |
12502 |
bool ScalarEvolution::canIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride, |
0 |
| 12503 |
bool IsSigned) { |
--- |
12503 |
bool IsSigned) { |
--- |
| 12504 |
|
--- |
12504 |
|
--- |
| 12505 |
unsigned BitWidth = getTypeSizeInBits(RHS->getType()); |
0 |
12505 |
unsigned BitWidth = getTypeSizeInBits(RHS->getType()); |
0 |
| 12506 |
const SCEV *One = getOne(Stride->getType()); |
0 |
12506 |
const SCEV *One = getOne(Stride->getType()); |
0 |
| 12507 |
|
--- |
12507 |
|
--- |
| 12508 |
if (IsSigned) { |
0 |
12508 |
if (IsSigned) { |
0 |
| 12509 |
APInt MinRHS = getSignedRangeMin(RHS); |
0 |
12509 |
APInt MinRHS = getSignedRangeMin(RHS); |
0 |
| 12510 |
APInt MinValue = APInt::getSignedMinValue(BitWidth); |
0 |
12510 |
APInt MinValue = APInt::getSignedMinValue(BitWidth); |
0 |
| 12511 |
APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One)); |
0 |
12511 |
APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One)); |
0 |
| 12512 |
|
--- |
12512 |
|
--- |
| 12513 |
// SMinRHS - SMaxStrideMinusOne < SMinValue => overflow! |
--- |
12513 |
// SMinRHS - SMaxStrideMinusOne < SMinValue => overflow! |
--- |
| 12514 |
return (std::move(MinValue) + MaxStrideMinusOne).sgt(MinRHS); |
0 |
12514 |
return (std::move(MinValue) + MaxStrideMinusOne).sgt(MinRHS); |
0 |
| 12515 |
} |
0 |
12515 |
} |
0 |
| 12516 |
|
--- |
12516 |
|
--- |
| 12517 |
APInt MinRHS = getUnsignedRangeMin(RHS); |
0 |
12517 |
APInt MinRHS = getUnsignedRangeMin(RHS); |
0 |
| 12518 |
APInt MinValue = APInt::getMinValue(BitWidth); |
0 |
12518 |
APInt MinValue = APInt::getMinValue(BitWidth); |
0 |
| 12519 |
APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One)); |
0 |
12519 |
APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One)); |
0 |
| 12520 |
|
--- |
12520 |
|
--- |
| 12521 |
// UMinRHS - UMaxStrideMinusOne < UMinValue => overflow! |
--- |
12521 |
// UMinRHS - UMaxStrideMinusOne < UMinValue => overflow! |
--- |
| 12522 |
return (std::move(MinValue) + MaxStrideMinusOne).ugt(MinRHS); |
0 |
12522 |
return (std::move(MinValue) + MaxStrideMinusOne).ugt(MinRHS); |
0 |
| 12523 |
} |
0 |
12523 |
} |
0 |
| 12524 |
|
--- |
12524 |
|
--- |
| 12525 |
const SCEV *ScalarEvolution::getUDivCeilSCEV(const SCEV *N, const SCEV *D) { |
0 |
12525 |
const SCEV *ScalarEvolution::getUDivCeilSCEV(const SCEV *N, const SCEV *D) { |
0 |
| 12526 |
// umin(N, 1) + floor((N - umin(N, 1)) / D) |
--- |
12526 |
// umin(N, 1) + floor((N - umin(N, 1)) / D) |
--- |
| 12527 |
// This is equivalent to "1 + floor((N - 1) / D)" for N != 0. The umin |
--- |
12527 |
// This is equivalent to "1 + floor((N - 1) / D)" for N != 0. The umin |
--- |
| 12528 |
// expression fixes the case of N=0. |
--- |
12528 |
// expression fixes the case of N=0. |
--- |
| 12529 |
const SCEV *MinNOne = getUMinExpr(N, getOne(N->getType())); |
0 |
12529 |
const SCEV *MinNOne = getUMinExpr(N, getOne(N->getType())); |
0 |
| 12530 |
const SCEV *NMinusOne = getMinusSCEV(N, MinNOne); |
0 |
12530 |
const SCEV *NMinusOne = getMinusSCEV(N, MinNOne); |
0 |
| 12531 |
return getAddExpr(MinNOne, getUDivExpr(NMinusOne, D)); |
0 |
12531 |
return getAddExpr(MinNOne, getUDivExpr(NMinusOne, D)); |
0 |
| 12532 |
} |
--- |
12532 |
} |
--- |
| 12533 |
|
--- |
12533 |
|
--- |
| 12534 |
const SCEV *ScalarEvolution::computeMaxBECountForLT(const SCEV *Start, |
0 |
12534 |
const SCEV *ScalarEvolution::computeMaxBECountForLT(const SCEV *Start, |
0 |
| 12535 |
const SCEV *Stride, |
--- |
12535 |
const SCEV *Stride, |
--- |
| 12536 |
const SCEV *End, |
--- |
12536 |
const SCEV *End, |
--- |
| 12537 |
unsigned BitWidth, |
--- |
12537 |
unsigned BitWidth, |
--- |
| 12538 |
bool IsSigned) { |
--- |
12538 |
bool IsSigned) { |
--- |
| 12539 |
// The logic in this function assumes we can represent a positive stride. |
--- |
12539 |
// The logic in this function assumes we can represent a positive stride. |
--- |
| 12540 |
// If we can't, the backedge-taken count must be zero. |
--- |
12540 |
// If we can't, the backedge-taken count must be zero. |
--- |
| 12541 |
if (IsSigned && BitWidth == 1) |
0 |
12541 |
if (IsSigned && BitWidth == 1) |
0 |
| 12542 |
return getZero(Stride->getType()); |
0 |
12542 |
return getZero(Stride->getType()); |
0 |
| 12543 |
|
--- |
12543 |
|
--- |
| 12544 |
// This code below only been closely audited for negative strides in the |
--- |
12544 |
// This code below only been closely audited for negative strides in the |
--- |
| 12545 |
// unsigned comparison case, it may be correct for signed comparison, but |
--- |
12545 |
// unsigned comparison case, it may be correct for signed comparison, but |
--- |
| 12546 |
// that needs to be established. |
--- |
12546 |
// that needs to be established. |
--- |
| 12547 |
if (IsSigned && isKnownNegative(Stride)) |
0 |
12547 |
if (IsSigned && isKnownNegative(Stride)) |
0 |
| 12548 |
return getCouldNotCompute(); |
0 |
12548 |
return getCouldNotCompute(); |
0 |
| 12549 |
|
--- |
12549 |
|
--- |
| 12550 |
// Calculate the maximum backedge count based on the range of values |
--- |
12550 |
// Calculate the maximum backedge count based on the range of values |
--- |
| 12551 |
// permitted by Start, End, and Stride. |
--- |
12551 |
// permitted by Start, End, and Stride. |
--- |
| 12552 |
APInt MinStart = |
--- |
12552 |
APInt MinStart = |
--- |
| 12553 |
IsSigned ? getSignedRangeMin(Start) : getUnsignedRangeMin(Start); |
0 |
12553 |
IsSigned ? getSignedRangeMin(Start) : getUnsignedRangeMin(Start); |
0 |
| 12554 |
|
--- |
12554 |
|
--- |
| 12555 |
APInt MinStride = |
--- |
12555 |
APInt MinStride = |
--- |
| 12556 |
IsSigned ? getSignedRangeMin(Stride) : getUnsignedRangeMin(Stride); |
0 |
12556 |
IsSigned ? getSignedRangeMin(Stride) : getUnsignedRangeMin(Stride); |
0 |
| 12557 |
|
--- |
12557 |
|
--- |
| 12558 |
// We assume either the stride is positive, or the backedge-taken count |
--- |
12558 |
// We assume either the stride is positive, or the backedge-taken count |
--- |
| 12559 |
// is zero. So force StrideForMaxBECount to be at least one. |
--- |
12559 |
// is zero. So force StrideForMaxBECount to be at least one. |
--- |
| 12560 |
APInt One(BitWidth, 1); |
0 |
12560 |
APInt One(BitWidth, 1); |
0 |
| 12561 |
APInt StrideForMaxBECount = IsSigned ? APIntOps::smax(One, MinStride) |
0 |
12561 |
APInt StrideForMaxBECount = IsSigned ? APIntOps::smax(One, MinStride) |
0 |
| 12562 |
: APIntOps::umax(One, MinStride); |
0 |
12562 |
: APIntOps::umax(One, MinStride); |
0 |
| 12563 |
|
--- |
12563 |
|
--- |
| 12564 |
APInt MaxValue = IsSigned ? APInt::getSignedMaxValue(BitWidth) |
--- |
12564 |
APInt MaxValue = IsSigned ? APInt::getSignedMaxValue(BitWidth) |
--- |
| 12565 |
: APInt::getMaxValue(BitWidth); |
0 |
12565 |
: APInt::getMaxValue(BitWidth); |
0 |
| 12566 |
APInt Limit = MaxValue - (StrideForMaxBECount - 1); |
0 |
12566 |
APInt Limit = MaxValue - (StrideForMaxBECount - 1); |
0 |
| 12567 |
|
--- |
12567 |
|
--- |
| 12568 |
// Although End can be a MAX expression we estimate MaxEnd considering only |
--- |
12568 |
// Although End can be a MAX expression we estimate MaxEnd considering only |
--- |
| 12569 |
// the case End = RHS of the loop termination condition. This is safe because |
--- |
12569 |
// the case End = RHS of the loop termination condition. This is safe because |
--- |
| 12570 |
// in the other case (End - Start) is zero, leading to a zero maximum backedge |
--- |
12570 |
// in the other case (End - Start) is zero, leading to a zero maximum backedge |
--- |
| 12571 |
// taken count. |
--- |
12571 |
// taken count. |
--- |
| 12572 |
APInt MaxEnd = IsSigned ? APIntOps::smin(getSignedRangeMax(End), Limit) |
0 |
12572 |
APInt MaxEnd = IsSigned ? APIntOps::smin(getSignedRangeMax(End), Limit) |
0 |
| 12573 |
: APIntOps::umin(getUnsignedRangeMax(End), Limit); |
0 |
12573 |
: APIntOps::umin(getUnsignedRangeMax(End), Limit); |
0 |
| 12574 |
|
--- |
12574 |
|
--- |
| 12575 |
// MaxBECount = ceil((max(MaxEnd, MinStart) - MinStart) / Stride) |
--- |
12575 |
// MaxBECount = ceil((max(MaxEnd, MinStart) - MinStart) / Stride) |
--- |
| 12576 |
MaxEnd = IsSigned ? APIntOps::smax(MaxEnd, MinStart) |
0 |
12576 |
MaxEnd = IsSigned ? APIntOps::smax(MaxEnd, MinStart) |
0 |
| 12577 |
: APIntOps::umax(MaxEnd, MinStart); |
0 |
12577 |
: APIntOps::umax(MaxEnd, MinStart); |
0 |
| 12578 |
|
--- |
12578 |
|
--- |
| 12579 |
return getUDivCeilSCEV(getConstant(MaxEnd - MinStart) /* Delta */, |
0 |
12579 |
return getUDivCeilSCEV(getConstant(MaxEnd - MinStart) /* Delta */, |
0 |
| 12580 |
getConstant(StrideForMaxBECount) /* Step */); |
0 |
12580 |
getConstant(StrideForMaxBECount) /* Step */); |
0 |
| 12581 |
} |
0 |
12581 |
} |
0 |
| 12582 |
|
--- |
12582 |
|
--- |
| 12583 |
ScalarEvolution::ExitLimit |
--- |
12583 |
ScalarEvolution::ExitLimit |
--- |
| 12584 |
ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS, |
0 |
12584 |
ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS, |
0 |
| 12585 |
const Loop *L, bool IsSigned, |
--- |
12585 |
const Loop *L, bool IsSigned, |
--- |
| 12586 |
bool ControlsOnlyExit, bool AllowPredicates) { |
--- |
12586 |
bool ControlsOnlyExit, bool AllowPredicates) { |
--- |
| 12587 |
SmallPtrSet Predicates; |
0 |
12587 |
SmallPtrSet Predicates; |
0 |
| 12588 |
|
--- |
12588 |
|
--- |
| 12589 |
const SCEVAddRecExpr *IV = dyn_cast(LHS); |
0 |
12589 |
const SCEVAddRecExpr *IV = dyn_cast(LHS); |
0 |
| 12590 |
bool PredicatedIV = false; |
0 |
12590 |
bool PredicatedIV = false; |
0 |
| 12591 |
|
--- |
12591 |
|
--- |
| 12592 |
auto canAssumeNoSelfWrap = [&](const SCEVAddRecExpr *AR) { |
0 |
12592 |
auto canAssumeNoSelfWrap = [&](const SCEVAddRecExpr *AR) { |
0 |
| 12593 |
// Can we prove this loop *must* be UB if overflow of IV occurs? |
--- |
12593 |
// Can we prove this loop *must* be UB if overflow of IV occurs? |
--- |
| 12594 |
// Reasoning goes as follows: |
--- |
12594 |
// Reasoning goes as follows: |
--- |
| 12595 |
// * Suppose the IV did self wrap. |
--- |
12595 |
// * Suppose the IV did self wrap. |
--- |
| 12596 |
// * If Stride evenly divides the iteration space, then once wrap |
--- |
12596 |
// * If Stride evenly divides the iteration space, then once wrap |
--- |
| 12597 |
// occurs, the loop must revisit the same values. |
--- |
12597 |
// occurs, the loop must revisit the same values. |
--- |
| 12598 |
// * We know that RHS is invariant, and that none of those values |
--- |
12598 |
// * We know that RHS is invariant, and that none of those values |
--- |
| 12599 |
// caused this exit to be taken previously. Thus, this exit is |
--- |
12599 |
// caused this exit to be taken previously. Thus, this exit is |
--- |
| 12600 |
// dynamically dead. |
--- |
12600 |
// dynamically dead. |
--- |
| 12601 |
// * If this is the sole exit, then a dead exit implies the loop |
--- |
12601 |
// * If this is the sole exit, then a dead exit implies the loop |
--- |
| 12602 |
// must be infinite if there are no abnormal exits. |
--- |
12602 |
// must be infinite if there are no abnormal exits. |
--- |
| 12603 |
// * If the loop were infinite, then it must either not be mustprogress |
--- |
12603 |
// * If the loop were infinite, then it must either not be mustprogress |
--- |
| 12604 |
// or have side effects. Otherwise, it must be UB. |
--- |
12604 |
// or have side effects. Otherwise, it must be UB. |
--- |
| 12605 |
// * It can't (by assumption), be UB so we have contradicted our |
--- |
12605 |
// * It can't (by assumption), be UB so we have contradicted our |
--- |
| 12606 |
// premise and can conclude the IV did not in fact self-wrap. |
--- |
12606 |
// premise and can conclude the IV did not in fact self-wrap. |
--- |
| 12607 |
if (!isLoopInvariant(RHS, L)) |
0 |
12607 |
if (!isLoopInvariant(RHS, L)) |
0 |
| 12608 |
return false; |
0 |
12608 |
return false; |
0 |
| 12609 |
|
--- |
12609 |
|
--- |
| 12610 |
auto *StrideC = dyn_cast(AR->getStepRecurrence(*this)); |
0 |
12610 |
auto *StrideC = dyn_cast(AR->getStepRecurrence(*this)); |
0 |
| 12611 |
if (!StrideC || !StrideC->getAPInt().isPowerOf2()) |
0 |
12611 |
if (!StrideC || !StrideC->getAPInt().isPowerOf2()) |
0 |
| 12612 |
return false; |
0 |
12612 |
return false; |
0 |
| 12613 |
|
--- |
12613 |
|
--- |
| 12614 |
if (!ControlsOnlyExit || !loopHasNoAbnormalExits(L)) |
0 |
12614 |
if (!ControlsOnlyExit || !loopHasNoAbnormalExits(L)) |
0 |
| 12615 |
return false; |
0 |
12615 |
return false; |
0 |
| 12616 |
|
--- |
12616 |
|
--- |
| 12617 |
return loopIsFiniteByAssumption(L); |
0 |
12617 |
return loopIsFiniteByAssumption(L); |
0 |
| 12618 |
}; |
0 |
12618 |
}; |
0 |
| 12619 |
|
--- |
12619 |
|
--- |
| 12620 |
if (!IV) { |
0 |
12620 |
if (!IV) { |
0 |
| 12621 |
if (auto *ZExt = dyn_cast(LHS)) { |
0 |
12621 |
if (auto *ZExt = dyn_cast(LHS)) { |
0 |
| 12622 |
const SCEVAddRecExpr *AR = dyn_cast(ZExt->getOperand()); |
0 |
12622 |
const SCEVAddRecExpr *AR = dyn_cast(ZExt->getOperand()); |
0 |
| 12623 |
if (AR && AR->getLoop() == L && AR->isAffine()) { |
0 |
12623 |
if (AR && AR->getLoop() == L && AR->isAffine()) { |
0 |
| 12624 |
auto canProveNUW = [&]() { |
0 |
12624 |
auto canProveNUW = [&]() { |
0 |
| 12625 |
if (!isLoopInvariant(RHS, L)) |
0 |
12625 |
if (!isLoopInvariant(RHS, L)) |
0 |
| 12626 |
return false; |
0 |
12626 |
return false; |
0 |
| 12627 |
|
--- |
12627 |
|
--- |
| 12628 |
if (!isKnownNonZero(AR->getStepRecurrence(*this))) |
0 |
12628 |
if (!isKnownNonZero(AR->getStepRecurrence(*this))) |
0 |
| 12629 |
// We need the sequence defined by AR to strictly increase in the |
--- |
12629 |
// We need the sequence defined by AR to strictly increase in the |
--- |
| 12630 |
// unsigned integer domain for the logic below to hold. |
--- |
12630 |
// unsigned integer domain for the logic below to hold. |
--- |
| 12631 |
return false; |
0 |
12631 |
return false; |
0 |
| 12632 |
|
--- |
12632 |
|
--- |
| 12633 |
const unsigned InnerBitWidth = getTypeSizeInBits(AR->getType()); |
0 |
12633 |
const unsigned InnerBitWidth = getTypeSizeInBits(AR->getType()); |
0 |
| 12634 |
const unsigned OuterBitWidth = getTypeSizeInBits(RHS->getType()); |
0 |
12634 |
const unsigned OuterBitWidth = getTypeSizeInBits(RHS->getType()); |
0 |
| 12635 |
// If RHS <=u Limit, then there must exist a value V in the sequence |
--- |
12635 |
// If RHS <=u Limit, then there must exist a value V in the sequence |
--- |
| 12636 |
// defined by AR (e.g. {Start,+,Step}) such that V >u RHS, and |
--- |
12636 |
// defined by AR (e.g. {Start,+,Step}) such that V >u RHS, and |
--- |
| 12637 |
// V <=u UINT_MAX. Thus, we must exit the loop before unsigned |
--- |
12637 |
// V <=u UINT_MAX. Thus, we must exit the loop before unsigned |
--- |
| 12638 |
// overflow occurs. This limit also implies that a signed comparison |
--- |
12638 |
// overflow occurs. This limit also implies that a signed comparison |
--- |
| 12639 |
// (in the wide bitwidth) is equivalent to an unsigned comparison as |
--- |
12639 |
// (in the wide bitwidth) is equivalent to an unsigned comparison as |
--- |
| 12640 |
// the high bits on both sides must be zero. |
--- |
12640 |
// the high bits on both sides must be zero. |
--- |
| 12641 |
APInt StrideMax = getUnsignedRangeMax(AR->getStepRecurrence(*this)); |
0 |
12641 |
APInt StrideMax = getUnsignedRangeMax(AR->getStepRecurrence(*this)); |
0 |
| 12642 |
APInt Limit = APInt::getMaxValue(InnerBitWidth) - (StrideMax - 1); |
0 |
12642 |
APInt Limit = APInt::getMaxValue(InnerBitWidth) - (StrideMax - 1); |
0 |
| 12643 |
Limit = Limit.zext(OuterBitWidth); |
0 |
12643 |
Limit = Limit.zext(OuterBitWidth); |
0 |
| 12644 |
return getUnsignedRangeMax(applyLoopGuards(RHS, L)).ule(Limit); |
0 |
12644 |
return getUnsignedRangeMax(applyLoopGuards(RHS, L)).ule(Limit); |
0 |
| 12645 |
}; |
0 |
12645 |
}; |
0 |
| 12646 |
auto Flags = AR->getNoWrapFlags(); |
0 |
12646 |
auto Flags = AR->getNoWrapFlags(); |
0 |
| 12647 |
if (!hasFlags(Flags, SCEV::FlagNUW) && canProveNUW()) |
0 |
12647 |
if (!hasFlags(Flags, SCEV::FlagNUW) && canProveNUW()) |
0 |
| 12648 |
Flags = setFlags(Flags, SCEV::FlagNUW); |
0 |
12648 |
Flags = setFlags(Flags, SCEV::FlagNUW); |
0 |
| 12649 |
|
--- |
12649 |
|
--- |
| 12650 |
setNoWrapFlags(const_cast(AR), Flags); |
0 |
12650 |
setNoWrapFlags(const_cast(AR), Flags); |
0 |
| 12651 |
if (AR->hasNoUnsignedWrap()) { |
0 |
12651 |
if (AR->hasNoUnsignedWrap()) { |
0 |
| 12652 |
// Emulate what getZeroExtendExpr would have done during construction |
--- |
12652 |
// Emulate what getZeroExtendExpr would have done during construction |
--- |
| 12653 |
// if we'd been able to infer the fact just above at that time. |
--- |
12653 |
// if we'd been able to infer the fact just above at that time. |
--- |
| 12654 |
const SCEV *Step = AR->getStepRecurrence(*this); |
0 |
12654 |
const SCEV *Step = AR->getStepRecurrence(*this); |
0 |
| 12655 |
Type *Ty = ZExt->getType(); |
0 |
12655 |
Type *Ty = ZExt->getType(); |
0 |
| 12656 |
auto *S = getAddRecExpr( |
0 |
12656 |
auto *S = getAddRecExpr( |
0 |
| 12657 |
getExtendAddRecStart(AR, Ty, this, 0), |
--- |
12657 |
getExtendAddRecStart(AR, Ty, this, 0), |
--- |
| 12658 |
getZeroExtendExpr(Step, Ty, 0), L, AR->getNoWrapFlags()); |
--- |
12658 |
getZeroExtendExpr(Step, Ty, 0), L, AR->getNoWrapFlags()); |
--- |
| 12659 |
IV = dyn_cast(S); |
0 |
12659 |
IV = dyn_cast(S); |
0 |
| 12660 |
} |
--- |
12660 |
} |
--- |
| 12661 |
} |
--- |
12661 |
} |
--- |
| 12662 |
} |
--- |
12662 |
} |
--- |
| 12663 |
} |
--- |
12663 |
} |
--- |
| 12664 |
|
--- |
12664 |
|
--- |
| 12665 |
|
--- |
12665 |
|
--- |
| 12666 |
if (!IV && AllowPredicates) { |
0 |
12666 |
if (!IV && AllowPredicates) { |
0 |
| 12667 |
// Try to make this an AddRec using runtime tests, in the first X |
--- |
12667 |
// Try to make this an AddRec using runtime tests, in the first X |
--- |
| 12668 |
// iterations of this loop, where X is the SCEV expression found by the |
--- |
12668 |
// iterations of this loop, where X is the SCEV expression found by the |
--- |
| 12669 |
// algorithm below. |
--- |
12669 |
// algorithm below. |
--- |
| 12670 |
IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates); |
0 |
12670 |
IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates); |
0 |
| 12671 |
PredicatedIV = true; |
0 |
12671 |
PredicatedIV = true; |
0 |
| 12672 |
} |
--- |
12672 |
} |
--- |
| 12673 |
|
--- |
12673 |
|
--- |
| 12674 |
// Avoid weird loops |
--- |
12674 |
// Avoid weird loops |
--- |
| 12675 |
if (!IV || IV->getLoop() != L || !IV->isAffine()) |
0 |
12675 |
if (!IV || IV->getLoop() != L || !IV->isAffine()) |
0 |
| 12676 |
return getCouldNotCompute(); |
0 |
12676 |
return getCouldNotCompute(); |
0 |
| 12677 |
|
--- |
12677 |
|
--- |
| 12678 |
// A precondition of this method is that the condition being analyzed |
--- |
12678 |
// A precondition of this method is that the condition being analyzed |
--- |
| 12679 |
// reaches an exiting branch which dominates the latch. Given that, we can |
--- |
12679 |
// reaches an exiting branch which dominates the latch. Given that, we can |
--- |
| 12680 |
// assume that an increment which violates the nowrap specification and |
--- |
12680 |
// assume that an increment which violates the nowrap specification and |
--- |
| 12681 |
// produces poison must cause undefined behavior when the resulting poison |
--- |
12681 |
// produces poison must cause undefined behavior when the resulting poison |
--- |
| 12682 |
// value is branched upon and thus we can conclude that the backedge is |
--- |
12682 |
// value is branched upon and thus we can conclude that the backedge is |
--- |
| 12683 |
// taken no more often than would be required to produce that poison value. |
--- |
12683 |
// taken no more often than would be required to produce that poison value. |
--- |
| 12684 |
// Note that a well defined loop can exit on the iteration which violates |
--- |
12684 |
// Note that a well defined loop can exit on the iteration which violates |
--- |
| 12685 |
// the nowrap specification if there is another exit (either explicit or |
--- |
12685 |
// the nowrap specification if there is another exit (either explicit or |
--- |
| 12686 |
// implicit/exceptional) which causes the loop to execute before the |
--- |
12686 |
// implicit/exceptional) which causes the loop to execute before the |
--- |
| 12687 |
// exiting instruction we're analyzing would trigger UB. |
--- |
12687 |
// exiting instruction we're analyzing would trigger UB. |
--- |
| 12688 |
auto WrapType = IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW; |
0 |
12688 |
auto WrapType = IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW; |
0 |
| 12689 |
bool NoWrap = ControlsOnlyExit && IV->getNoWrapFlags(WrapType); |
0 |
12689 |
bool NoWrap = ControlsOnlyExit && IV->getNoWrapFlags(WrapType); |
0 |
| 12690 |
ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; |
0 |
12690 |
ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; |
0 |
| 12691 |
|
--- |
12691 |
|
--- |
| 12692 |
const SCEV *Stride = IV->getStepRecurrence(*this); |
0 |
12692 |
const SCEV *Stride = IV->getStepRecurrence(*this); |
0 |
| 12693 |
|
--- |
12693 |
|
--- |
| 12694 |
bool PositiveStride = isKnownPositive(Stride); |
0 |
12694 |
bool PositiveStride = isKnownPositive(Stride); |
0 |
| 12695 |
|
--- |
12695 |
|
--- |
| 12696 |
// Avoid negative or zero stride values. |
--- |
12696 |
// Avoid negative or zero stride values. |
--- |
| 12697 |
if (!PositiveStride) { |
0 |
12697 |
if (!PositiveStride) { |
0 |
| 12698 |
// We can compute the correct backedge taken count for loops with unknown |
--- |
12698 |
// We can compute the correct backedge taken count for loops with unknown |
--- |
| 12699 |
// strides if we can prove that the loop is not an infinite loop with side |
--- |
12699 |
// strides if we can prove that the loop is not an infinite loop with side |
--- |
| 12700 |
// effects. Here's the loop structure we are trying to handle - |
--- |
12700 |
// effects. Here's the loop structure we are trying to handle - |
--- |
| 12701 |
// |
--- |
12701 |
// |
--- |
| 12702 |
// i = start |
--- |
12702 |
// i = start |
--- |
| 12703 |
// do { |
--- |
12703 |
// do { |
--- |
| 12704 |
// A[i] = i; |
--- |
12704 |
// A[i] = i; |
--- |
| 12705 |
// i += s; |
--- |
12705 |
// i += s; |
--- |
| 12706 |
// } while (i < end); |
--- |
12706 |
// } while (i < end); |
--- |
| 12707 |
// |
--- |
12707 |
// |
--- |
| 12708 |
// The backedge taken count for such loops is evaluated as - |
--- |
12708 |
// The backedge taken count for such loops is evaluated as - |
--- |
| 12709 |
// (max(end, start + stride) - start - 1) /u stride |
--- |
12709 |
// (max(end, start + stride) - start - 1) /u stride |
--- |
| 12710 |
// |
--- |
12710 |
// |
--- |
| 12711 |
// The additional preconditions that we need to check to prove correctness |
--- |
12711 |
// The additional preconditions that we need to check to prove correctness |
--- |
| 12712 |
// of the above formula is as follows - |
--- |
12712 |
// of the above formula is as follows - |
--- |
| 12713 |
// |
--- |
12713 |
// |
--- |
| 12714 |
// a) IV is either nuw or nsw depending upon signedness (indicated by the |
--- |
12714 |
// a) IV is either nuw or nsw depending upon signedness (indicated by the |
--- |
| 12715 |
// NoWrap flag). |
--- |
12715 |
// NoWrap flag). |
--- |
| 12716 |
// b) the loop is guaranteed to be finite (e.g. is mustprogress and has |
--- |
12716 |
// b) the loop is guaranteed to be finite (e.g. is mustprogress and has |
--- |
| 12717 |
// no side effects within the loop) |
--- |
12717 |
// no side effects within the loop) |
--- |
| 12718 |
// c) loop has a single static exit (with no abnormal exits) |
--- |
12718 |
// c) loop has a single static exit (with no abnormal exits) |
--- |
| 12719 |
// |
--- |
12719 |
// |
--- |
| 12720 |
// Precondition a) implies that if the stride is negative, this is a single |
--- |
12720 |
// Precondition a) implies that if the stride is negative, this is a single |
--- |
| 12721 |
// trip loop. The backedge taken count formula reduces to zero in this case. |
--- |
12721 |
// trip loop. The backedge taken count formula reduces to zero in this case. |
--- |
| 12722 |
// |
--- |
12722 |
// |
--- |
| 12723 |
// Precondition b) and c) combine to imply that if rhs is invariant in L, |
--- |
12723 |
// Precondition b) and c) combine to imply that if rhs is invariant in L, |
--- |
| 12724 |
// then a zero stride means the backedge can't be taken without executing |
--- |
12724 |
// then a zero stride means the backedge can't be taken without executing |
--- |
| 12725 |
// undefined behavior. |
--- |
12725 |
// undefined behavior. |
--- |
| 12726 |
// |
--- |
12726 |
// |
--- |
| 12727 |
// The positive stride case is the same as isKnownPositive(Stride) returning |
--- |
12727 |
// The positive stride case is the same as isKnownPositive(Stride) returning |
--- |
| 12728 |
// true (original behavior of the function). |
--- |
12728 |
// true (original behavior of the function). |
--- |
| 12729 |
// |
--- |
12729 |
// |
--- |
| 12730 |
if (PredicatedIV || !NoWrap || !loopIsFiniteByAssumption(L) || |
0 |
12730 |
if (PredicatedIV || !NoWrap || !loopIsFiniteByAssumption(L) || |
0 |
| 12731 |
!loopHasNoAbnormalExits(L)) |
0 |
12731 |
!loopHasNoAbnormalExits(L)) |
0 |
| 12732 |
return getCouldNotCompute(); |
0 |
12732 |
return getCouldNotCompute(); |
0 |
| 12733 |
|
--- |
12733 |
|
--- |
| 12734 |
if (!isKnownNonZero(Stride)) { |
0 |
12734 |
if (!isKnownNonZero(Stride)) { |
0 |
| 12735 |
// If we have a step of zero, and RHS isn't invariant in L, we don't know |
--- |
12735 |
// If we have a step of zero, and RHS isn't invariant in L, we don't know |
--- |
| 12736 |
// if it might eventually be greater than start and if so, on which |
--- |
12736 |
// if it might eventually be greater than start and if so, on which |
--- |
| 12737 |
// iteration. We can't even produce a useful upper bound. |
--- |
12737 |
// iteration. We can't even produce a useful upper bound. |
--- |
| 12738 |
if (!isLoopInvariant(RHS, L)) |
0 |
12738 |
if (!isLoopInvariant(RHS, L)) |
0 |
| 12739 |
return getCouldNotCompute(); |
0 |
12739 |
return getCouldNotCompute(); |
0 |
| 12740 |
|
--- |
12740 |
|
--- |
| 12741 |
// We allow a potentially zero stride, but we need to divide by stride |
--- |
12741 |
// We allow a potentially zero stride, but we need to divide by stride |
--- |
| 12742 |
// below. Since the loop can't be infinite and this check must control |
--- |
12742 |
// below. Since the loop can't be infinite and this check must control |
--- |
| 12743 |
// the sole exit, we can infer the exit must be taken on the first |
--- |
12743 |
// the sole exit, we can infer the exit must be taken on the first |
--- |
| 12744 |
// iteration (e.g. backedge count = 0) if the stride is zero. Given that, |
--- |
12744 |
// iteration (e.g. backedge count = 0) if the stride is zero. Given that, |
--- |
| 12745 |
// we know the numerator in the divides below must be zero, so we can |
--- |
12745 |
// we know the numerator in the divides below must be zero, so we can |
--- |
| 12746 |
// pick an arbitrary non-zero value for the denominator (e.g. stride) |
--- |
12746 |
// pick an arbitrary non-zero value for the denominator (e.g. stride) |
--- |
| 12747 |
// and produce the right result. |
--- |
12747 |
// and produce the right result. |
--- |
| 12748 |
// FIXME: Handle the case where Stride is poison? |
--- |
12748 |
// FIXME: Handle the case where Stride is poison? |
--- |
| 12749 |
auto wouldZeroStrideBeUB = [&]() { |
0 |
12749 |
auto wouldZeroStrideBeUB = [&]() { |
0 |
| 12750 |
// Proof by contradiction. Suppose the stride were zero. If we can |
--- |
12750 |
// Proof by contradiction. Suppose the stride were zero. If we can |
--- |
| 12751 |
// prove that the backedge *is* taken on the first iteration, then since |
--- |
12751 |
// prove that the backedge *is* taken on the first iteration, then since |
--- |
| 12752 |
// we know this condition controls the sole exit, we must have an |
--- |
12752 |
// we know this condition controls the sole exit, we must have an |
--- |
| 12753 |
// infinite loop. We can't have a (well defined) infinite loop per |
--- |
12753 |
// infinite loop. We can't have a (well defined) infinite loop per |
--- |
| 12754 |
// check just above. |
--- |
12754 |
// check just above. |
--- |
| 12755 |
// Note: The (Start - Stride) term is used to get the start' term from |
--- |
12755 |
// Note: The (Start - Stride) term is used to get the start' term from |
--- |
| 12756 |
// (start' + stride,+,stride). Remember that we only care about the |
--- |
12756 |
// (start' + stride,+,stride). Remember that we only care about the |
--- |
| 12757 |
// result of this expression when stride == 0 at runtime. |
--- |
12757 |
// result of this expression when stride == 0 at runtime. |
--- |
| 12758 |
auto *StartIfZero = getMinusSCEV(IV->getStart(), Stride); |
0 |
12758 |
auto *StartIfZero = getMinusSCEV(IV->getStart(), Stride); |
0 |
| 12759 |
return isLoopEntryGuardedByCond(L, Cond, StartIfZero, RHS); |
0 |
12759 |
return isLoopEntryGuardedByCond(L, Cond, StartIfZero, RHS); |
0 |
| 12760 |
}; |
0 |
12760 |
}; |
0 |
| 12761 |
if (!wouldZeroStrideBeUB()) { |
0 |
12761 |
if (!wouldZeroStrideBeUB()) { |
0 |
| 12762 |
Stride = getUMaxExpr(Stride, getOne(Stride->getType())); |
0 |
12762 |
Stride = getUMaxExpr(Stride, getOne(Stride->getType())); |
0 |
| 12763 |
} |
--- |
12763 |
} |
--- |
| 12764 |
} |
--- |
12764 |
} |
--- |
| 12765 |
} else if (!Stride->isOne() && !NoWrap) { |
0 |
12765 |
} else if (!Stride->isOne() && !NoWrap) { |
0 |
| 12766 |
auto isUBOnWrap = [&]() { |
0 |
12766 |
auto isUBOnWrap = [&]() { |
0 |
| 12767 |
// From no-self-wrap, we need to then prove no-(un)signed-wrap. This |
--- |
12767 |
// From no-self-wrap, we need to then prove no-(un)signed-wrap. This |
--- |
| 12768 |
// follows trivially from the fact that every (un)signed-wrapped, but |
--- |
12768 |
// follows trivially from the fact that every (un)signed-wrapped, but |
--- |
| 12769 |
// not self-wrapped value must be LT than the last value before |
--- |
12769 |
// not self-wrapped value must be LT than the last value before |
--- |
| 12770 |
// (un)signed wrap. Since we know that last value didn't exit, nor |
--- |
12770 |
// (un)signed wrap. Since we know that last value didn't exit, nor |
--- |
| 12771 |
// will any smaller one. |
--- |
12771 |
// will any smaller one. |
--- |
| 12772 |
return canAssumeNoSelfWrap(IV); |
0 |
12772 |
return canAssumeNoSelfWrap(IV); |
0 |
| 12773 |
}; |
0 |
12773 |
}; |
0 |
| 12774 |
|
--- |
12774 |
|
--- |
| 12775 |
// Avoid proven overflow cases: this will ensure that the backedge taken |
--- |
12775 |
// Avoid proven overflow cases: this will ensure that the backedge taken |
--- |
| 12776 |
// count will not generate any unsigned overflow. Relaxed no-overflow |
--- |
12776 |
// count will not generate any unsigned overflow. Relaxed no-overflow |
--- |
| 12777 |
// conditions exploit NoWrapFlags, allowing to optimize in presence of |
--- |
12777 |
// conditions exploit NoWrapFlags, allowing to optimize in presence of |
--- |
| 12778 |
// undefined behaviors like the case of C language. |
--- |
12778 |
// undefined behaviors like the case of C language. |
--- |
| 12779 |
if (canIVOverflowOnLT(RHS, Stride, IsSigned) && !isUBOnWrap()) |
0 |
12779 |
if (canIVOverflowOnLT(RHS, Stride, IsSigned) && !isUBOnWrap()) |
0 |
| 12780 |
return getCouldNotCompute(); |
0 |
12780 |
return getCouldNotCompute(); |
0 |
| 12781 |
} |
--- |
12781 |
} |
--- |
| 12782 |
|
--- |
12782 |
|
--- |
| 12783 |
// On all paths just preceeding, we established the following invariant: |
--- |
12783 |
// On all paths just preceeding, we established the following invariant: |
--- |
| 12784 |
// IV can be assumed not to overflow up to and including the exiting |
--- |
12784 |
// IV can be assumed not to overflow up to and including the exiting |
--- |
| 12785 |
// iteration. We proved this in one of two ways: |
--- |
12785 |
// iteration. We proved this in one of two ways: |
--- |
| 12786 |
// 1) We can show overflow doesn't occur before the exiting iteration |
--- |
12786 |
// 1) We can show overflow doesn't occur before the exiting iteration |
--- |
| 12787 |
// 1a) canIVOverflowOnLT, and b) step of one |
--- |
12787 |
// 1a) canIVOverflowOnLT, and b) step of one |
--- |
| 12788 |
// 2) We can show that if overflow occurs, the loop must execute UB |
--- |
12788 |
// 2) We can show that if overflow occurs, the loop must execute UB |
--- |
| 12789 |
// before any possible exit. |
--- |
12789 |
// before any possible exit. |
--- |
| 12790 |
// Note that we have not yet proved RHS invariant (in general). |
--- |
12790 |
// Note that we have not yet proved RHS invariant (in general). |
--- |
| 12791 |
|
--- |
12791 |
|
--- |
| 12792 |
const SCEV *Start = IV->getStart(); |
0 |
12792 |
const SCEV *Start = IV->getStart(); |
0 |
| 12793 |
|
--- |
12793 |
|
--- |
| 12794 |
// Preserve pointer-typed Start/RHS to pass to isLoopEntryGuardedByCond. |
--- |
12794 |
// Preserve pointer-typed Start/RHS to pass to isLoopEntryGuardedByCond. |
--- |
| 12795 |
// If we convert to integers, isLoopEntryGuardedByCond will miss some cases. |
--- |
12795 |
// If we convert to integers, isLoopEntryGuardedByCond will miss some cases. |
--- |
| 12796 |
// Use integer-typed versions for actual computation; we can't subtract |
--- |
12796 |
// Use integer-typed versions for actual computation; we can't subtract |
--- |
| 12797 |
// pointers in general. |
--- |
12797 |
// pointers in general. |
--- |
| 12798 |
const SCEV *OrigStart = Start; |
0 |
12798 |
const SCEV *OrigStart = Start; |
0 |
| 12799 |
const SCEV *OrigRHS = RHS; |
0 |
12799 |
const SCEV *OrigRHS = RHS; |
0 |
| 12800 |
if (Start->getType()->isPointerTy()) { |
0 |
12800 |
if (Start->getType()->isPointerTy()) { |
0 |
| 12801 |
Start = getLosslessPtrToIntExpr(Start); |
0 |
12801 |
Start = getLosslessPtrToIntExpr(Start); |
0 |
| 12802 |
if (isa(Start)) |
0 |
12802 |
if (isa(Start)) |
0 |
| 12803 |
return Start; |
0 |
12803 |
return Start; |
0 |
| 12804 |
} |
--- |
12804 |
} |
--- |
| 12805 |
if (RHS->getType()->isPointerTy()) { |
0 |
12805 |
if (RHS->getType()->isPointerTy()) { |
0 |
| 12806 |
RHS = getLosslessPtrToIntExpr(RHS); |
0 |
12806 |
RHS = getLosslessPtrToIntExpr(RHS); |
0 |
| 12807 |
if (isa(RHS)) |
0 |
12807 |
if (isa(RHS)) |
0 |
| 12808 |
return RHS; |
0 |
12808 |
return RHS; |
0 |
| 12809 |
} |
--- |
12809 |
} |
--- |
| 12810 |
|
--- |
12810 |
|
--- |
| 12811 |
// When the RHS is not invariant, we do not know the end bound of the loop and |
--- |
12811 |
// When the RHS is not invariant, we do not know the end bound of the loop and |
--- |
| 12812 |
// cannot calculate the ExactBECount needed by ExitLimit. However, we can |
--- |
12812 |
// cannot calculate the ExactBECount needed by ExitLimit. However, we can |
--- |
| 12813 |
// calculate the MaxBECount, given the start, stride and max value for the end |
--- |
12813 |
// calculate the MaxBECount, given the start, stride and max value for the end |
--- |
| 12814 |
// bound of the loop (RHS), and the fact that IV does not overflow (which is |
--- |
12814 |
// bound of the loop (RHS), and the fact that IV does not overflow (which is |
--- |
| 12815 |
// checked above). |
--- |
12815 |
// checked above). |
--- |
| 12816 |
if (!isLoopInvariant(RHS, L)) { |
0 |
12816 |
if (!isLoopInvariant(RHS, L)) { |
0 |
| 12817 |
const SCEV *MaxBECount = computeMaxBECountForLT( |
0 |
12817 |
const SCEV *MaxBECount = computeMaxBECountForLT( |
0 |
| 12818 |
Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned); |
0 |
12818 |
Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned); |
0 |
| 12819 |
return ExitLimit(getCouldNotCompute() /* ExactNotTaken */, MaxBECount, |
--- |
12819 |
return ExitLimit(getCouldNotCompute() /* ExactNotTaken */, MaxBECount, |
--- |
| 12820 |
MaxBECount, false /*MaxOrZero*/, Predicates); |
0 |
12820 |
MaxBECount, false /*MaxOrZero*/, Predicates); |
0 |
| 12821 |
} |
--- |
12821 |
} |
--- |
| 12822 |
|
--- |
12822 |
|
--- |
| 12823 |
// We use the expression (max(End,Start)-Start)/Stride to describe the |
--- |
12823 |
// We use the expression (max(End,Start)-Start)/Stride to describe the |
--- |
| 12824 |
// backedge count, as if the backedge is taken at least once max(End,Start) |
--- |
12824 |
// backedge count, as if the backedge is taken at least once max(End,Start) |
--- |
| 12825 |
// is End and so the result is as above, and if not max(End,Start) is Start |
--- |
12825 |
// is End and so the result is as above, and if not max(End,Start) is Start |
--- |
| 12826 |
// so we get a backedge count of zero. |
--- |
12826 |
// so we get a backedge count of zero. |
--- |
| 12827 |
const SCEV *BECount = nullptr; |
0 |
12827 |
const SCEV *BECount = nullptr; |
0 |
| 12828 |
auto *OrigStartMinusStride = getMinusSCEV(OrigStart, Stride); |
0 |
12828 |
auto *OrigStartMinusStride = getMinusSCEV(OrigStart, Stride); |
0 |
| 12829 |
assert(isAvailableAtLoopEntry(OrigStartMinusStride, L) && "Must be!"); |
0 |
12829 |
assert(isAvailableAtLoopEntry(OrigStartMinusStride, L) && "Must be!"); |
0 |
| 12830 |
assert(isAvailableAtLoopEntry(OrigStart, L) && "Must be!"); |
0 |
12830 |
assert(isAvailableAtLoopEntry(OrigStart, L) && "Must be!"); |
0 |
| 12831 |
assert(isAvailableAtLoopEntry(OrigRHS, L) && "Must be!"); |
0 |
12831 |
assert(isAvailableAtLoopEntry(OrigRHS, L) && "Must be!"); |
0 |
| 12832 |
// Can we prove (max(RHS,Start) > Start - Stride? |
--- |
12832 |
// Can we prove (max(RHS,Start) > Start - Stride? |
--- |
| 12833 |
if (isLoopEntryGuardedByCond(L, Cond, OrigStartMinusStride, OrigStart) && |
0 |
12833 |
if (isLoopEntryGuardedByCond(L, Cond, OrigStartMinusStride, OrigStart) && |
0 |
| 12834 |
isLoopEntryGuardedByCond(L, Cond, OrigStartMinusStride, OrigRHS)) { |
0 |
12834 |
isLoopEntryGuardedByCond(L, Cond, OrigStartMinusStride, OrigRHS)) { |
0 |
| 12835 |
// In this case, we can use a refined formula for computing backedge taken |
--- |
12835 |
// In this case, we can use a refined formula for computing backedge taken |
--- |
| 12836 |
// count. The general formula remains: |
--- |
12836 |
// count. The general formula remains: |
--- |
| 12837 |
// "End-Start /uceiling Stride" where "End = max(RHS,Start)" |
--- |
12837 |
// "End-Start /uceiling Stride" where "End = max(RHS,Start)" |
--- |
| 12838 |
// We want to use the alternate formula: |
--- |
12838 |
// We want to use the alternate formula: |
--- |
| 12839 |
// "((End - 1) - (Start - Stride)) /u Stride" |
--- |
12839 |
// "((End - 1) - (Start - Stride)) /u Stride" |
--- |
| 12840 |
// Let's do a quick case analysis to show these are equivalent under |
--- |
12840 |
// Let's do a quick case analysis to show these are equivalent under |
--- |
| 12841 |
// our precondition that max(RHS,Start) > Start - Stride. |
--- |
12841 |
// our precondition that max(RHS,Start) > Start - Stride. |
--- |
| 12842 |
// * For RHS <= Start, the backedge-taken count must be zero. |
--- |
12842 |
// * For RHS <= Start, the backedge-taken count must be zero. |
--- |
| 12843 |
// "((End - 1) - (Start - Stride)) /u Stride" reduces to |
--- |
12843 |
// "((End - 1) - (Start - Stride)) /u Stride" reduces to |
--- |
| 12844 |
// "((Start - 1) - (Start - Stride)) /u Stride" which simplies to |
--- |
12844 |
// "((Start - 1) - (Start - Stride)) /u Stride" which simplies to |
--- |
| 12845 |
// "Stride - 1 /u Stride" which is indeed zero for all non-zero values |
--- |
12845 |
// "Stride - 1 /u Stride" which is indeed zero for all non-zero values |
--- |
| 12846 |
// of Stride. For 0 stride, we've use umin(1,Stride) above, reducing |
--- |
12846 |
// of Stride. For 0 stride, we've use umin(1,Stride) above, reducing |
--- |
| 12847 |
// this to the stride of 1 case. |
--- |
12847 |
// this to the stride of 1 case. |
--- |
| 12848 |
// * For RHS >= Start, the backedge count must be "RHS-Start /uceil Stride". |
--- |
12848 |
// * For RHS >= Start, the backedge count must be "RHS-Start /uceil Stride". |
--- |
| 12849 |
// "((End - 1) - (Start - Stride)) /u Stride" reduces to |
--- |
12849 |
// "((End - 1) - (Start - Stride)) /u Stride" reduces to |
--- |
| 12850 |
// "((RHS - 1) - (Start - Stride)) /u Stride" reassociates to |
--- |
12850 |
// "((RHS - 1) - (Start - Stride)) /u Stride" reassociates to |
--- |
| 12851 |
// "((RHS - (Start - Stride) - 1) /u Stride". |
--- |
12851 |
// "((RHS - (Start - Stride) - 1) /u Stride". |
--- |
| 12852 |
// Our preconditions trivially imply no overflow in that form. |
--- |
12852 |
// Our preconditions trivially imply no overflow in that form. |
--- |
| 12853 |
const SCEV *MinusOne = getMinusOne(Stride->getType()); |
0 |
12853 |
const SCEV *MinusOne = getMinusOne(Stride->getType()); |
0 |
| 12854 |
const SCEV *Numerator = |
--- |
12854 |
const SCEV *Numerator = |
--- |
| 12855 |
getMinusSCEV(getAddExpr(RHS, MinusOne), getMinusSCEV(Start, Stride)); |
0 |
12855 |
getMinusSCEV(getAddExpr(RHS, MinusOne), getMinusSCEV(Start, Stride)); |
0 |
| 12856 |
BECount = getUDivExpr(Numerator, Stride); |
0 |
12856 |
BECount = getUDivExpr(Numerator, Stride); |
0 |
| 12857 |
} |
--- |
12857 |
} |
--- |
| 12858 |
|
--- |
12858 |
|
--- |
| 12859 |
const SCEV *BECountIfBackedgeTaken = nullptr; |
0 |
12859 |
const SCEV *BECountIfBackedgeTaken = nullptr; |
0 |
| 12860 |
if (!BECount) { |
0 |
12860 |
if (!BECount) { |
0 |
| 12861 |
auto canProveRHSGreaterThanEqualStart = [&]() { |
0 |
12861 |
auto canProveRHSGreaterThanEqualStart = [&]() { |
0 |
| 12862 |
auto CondGE = IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; |
0 |
12862 |
auto CondGE = IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; |
0 |
| 12863 |
if (isLoopEntryGuardedByCond(L, CondGE, OrigRHS, OrigStart)) |
0 |
12863 |
if (isLoopEntryGuardedByCond(L, CondGE, OrigRHS, OrigStart)) |
0 |
| 12864 |
return true; |
0 |
12864 |
return true; |
0 |
| 12865 |
|
--- |
12865 |
|
--- |
| 12866 |
// (RHS > Start - 1) implies RHS >= Start. |
--- |
12866 |
// (RHS > Start - 1) implies RHS >= Start. |
--- |
| 12867 |
// * "RHS >= Start" is trivially equivalent to "RHS > Start - 1" if |
--- |
12867 |
// * "RHS >= Start" is trivially equivalent to "RHS > Start - 1" if |
--- |
| 12868 |
// "Start - 1" doesn't overflow. |
--- |
12868 |
// "Start - 1" doesn't overflow. |
--- |
| 12869 |
// * For signed comparison, if Start - 1 does overflow, it's equal |
--- |
12869 |
// * For signed comparison, if Start - 1 does overflow, it's equal |
--- |
| 12870 |
// to INT_MAX, and "RHS >s INT_MAX" is trivially false. |
--- |
12870 |
// to INT_MAX, and "RHS >s INT_MAX" is trivially false. |
--- |
| 12871 |
// * For unsigned comparison, if Start - 1 does overflow, it's equal |
--- |
12871 |
// * For unsigned comparison, if Start - 1 does overflow, it's equal |
--- |
| 12872 |
// to UINT_MAX, and "RHS >u UINT_MAX" is trivially false. |
--- |
12872 |
// to UINT_MAX, and "RHS >u UINT_MAX" is trivially false. |
--- |
| 12873 |
// |
--- |
12873 |
// |
--- |
| 12874 |
// FIXME: Should isLoopEntryGuardedByCond do this for us? |
--- |
12874 |
// FIXME: Should isLoopEntryGuardedByCond do this for us? |
--- |
| 12875 |
auto CondGT = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
0 |
12875 |
auto CondGT = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
0 |
| 12876 |
auto *StartMinusOne = getAddExpr(OrigStart, |
0 |
12876 |
auto *StartMinusOne = getAddExpr(OrigStart, |
0 |
| 12877 |
getMinusOne(OrigStart->getType())); |
--- |
12877 |
getMinusOne(OrigStart->getType())); |
--- |
| 12878 |
return isLoopEntryGuardedByCond(L, CondGT, OrigRHS, StartMinusOne); |
0 |
12878 |
return isLoopEntryGuardedByCond(L, CondGT, OrigRHS, StartMinusOne); |
0 |
| 12879 |
}; |
0 |
12879 |
}; |
0 |
| 12880 |
|
--- |
12880 |
|
--- |
| 12881 |
// If we know that RHS >= Start in the context of loop, then we know that |
--- |
12881 |
// If we know that RHS >= Start in the context of loop, then we know that |
--- |
| 12882 |
// max(RHS, Start) = RHS at this point. |
--- |
12882 |
// max(RHS, Start) = RHS at this point. |
--- |
| 12883 |
const SCEV *End; |
--- |
12883 |
const SCEV *End; |
--- |
| 12884 |
if (canProveRHSGreaterThanEqualStart()) { |
0 |
12884 |
if (canProveRHSGreaterThanEqualStart()) { |
0 |
| 12885 |
End = RHS; |
0 |
12885 |
End = RHS; |
0 |
| 12886 |
} else { |
--- |
12886 |
} else { |
--- |
| 12887 |
// If RHS < Start, the backedge will be taken zero times. So in |
--- |
12887 |
// If RHS < Start, the backedge will be taken zero times. So in |
--- |
| 12888 |
// general, we can write the backedge-taken count as: |
--- |
12888 |
// general, we can write the backedge-taken count as: |
--- |
| 12889 |
// |
--- |
12889 |
// |
--- |
| 12890 |
// RHS >= Start ? ceil(RHS - Start) / Stride : 0 |
--- |
12890 |
// RHS >= Start ? ceil(RHS - Start) / Stride : 0 |
--- |
| 12891 |
// |
--- |
12891 |
// |
--- |
| 12892 |
// We convert it to the following to make it more convenient for SCEV: |
--- |
12892 |
// We convert it to the following to make it more convenient for SCEV: |
--- |
| 12893 |
// |
--- |
12893 |
// |
--- |
| 12894 |
// ceil(max(RHS, Start) - Start) / Stride |
--- |
12894 |
// ceil(max(RHS, Start) - Start) / Stride |
--- |
| 12895 |
End = IsSigned ? getSMaxExpr(RHS, Start) : getUMaxExpr(RHS, Start); |
0 |
12895 |
End = IsSigned ? getSMaxExpr(RHS, Start) : getUMaxExpr(RHS, Start); |
0 |
| 12896 |
|
--- |
12896 |
|
--- |
| 12897 |
// See what would happen if we assume the backedge is taken. This is |
--- |
12897 |
// See what would happen if we assume the backedge is taken. This is |
--- |
| 12898 |
// used to compute MaxBECount. |
--- |
12898 |
// used to compute MaxBECount. |
--- |
| 12899 |
BECountIfBackedgeTaken = getUDivCeilSCEV(getMinusSCEV(RHS, Start), Stride); |
0 |
12899 |
BECountIfBackedgeTaken = getUDivCeilSCEV(getMinusSCEV(RHS, Start), Stride); |
0 |
| 12900 |
} |
--- |
12900 |
} |
--- |
| 12901 |
|
--- |
12901 |
|
--- |
| 12902 |
// At this point, we know: |
--- |
12902 |
// At this point, we know: |
--- |
| 12903 |
// |
--- |
12903 |
// |
--- |
| 12904 |
// 1. If IsSigned, Start <=s End; otherwise, Start <=u End |
--- |
12904 |
// 1. If IsSigned, Start <=s End; otherwise, Start <=u End |
--- |
| 12905 |
// 2. The index variable doesn't overflow. |
--- |
12905 |
// 2. The index variable doesn't overflow. |
--- |
| 12906 |
// |
--- |
12906 |
// |
--- |
| 12907 |
// Therefore, we know N exists such that |
--- |
12907 |
// Therefore, we know N exists such that |
--- |
| 12908 |
// (Start + Stride * N) >= End, and computing "(Start + Stride * N)" |
--- |
12908 |
// (Start + Stride * N) >= End, and computing "(Start + Stride * N)" |
--- |
| 12909 |
// doesn't overflow. |
--- |
12909 |
// doesn't overflow. |
--- |
| 12910 |
// |
--- |
12910 |
// |
--- |
| 12911 |
// Using this information, try to prove whether the addition in |
--- |
12911 |
// Using this information, try to prove whether the addition in |
--- |
| 12912 |
// "(Start - End) + (Stride - 1)" has unsigned overflow. |
--- |
12912 |
// "(Start - End) + (Stride - 1)" has unsigned overflow. |
--- |
| 12913 |
const SCEV *One = getOne(Stride->getType()); |
0 |
12913 |
const SCEV *One = getOne(Stride->getType()); |
0 |
| 12914 |
bool MayAddOverflow = [&] { |
0 |
12914 |
bool MayAddOverflow = [&] { |
0 |
| 12915 |
if (auto *StrideC = dyn_cast(Stride)) { |
0 |
12915 |
if (auto *StrideC = dyn_cast(Stride)) { |
0 |
| 12916 |
if (StrideC->getAPInt().isPowerOf2()) { |
0 |
12916 |
if (StrideC->getAPInt().isPowerOf2()) { |
0 |
| 12917 |
// Suppose Stride is a power of two, and Start/End are unsigned |
--- |
12917 |
// Suppose Stride is a power of two, and Start/End are unsigned |
--- |
| 12918 |
// integers. Let UMAX be the largest representable unsigned |
--- |
12918 |
// integers. Let UMAX be the largest representable unsigned |
--- |
| 12919 |
// integer. |
--- |
12919 |
// integer. |
--- |
| 12920 |
// |
--- |
12920 |
// |
--- |
| 12921 |
// By the preconditions of this function, we know |
--- |
12921 |
// By the preconditions of this function, we know |
--- |
| 12922 |
// "(Start + Stride * N) >= End", and this doesn't overflow. |
--- |
12922 |
// "(Start + Stride * N) >= End", and this doesn't overflow. |
--- |
| 12923 |
// As a formula: |
--- |
12923 |
// As a formula: |
--- |
| 12924 |
// |
--- |
12924 |
// |
--- |
| 12925 |
// End <= (Start + Stride * N) <= UMAX |
--- |
12925 |
// End <= (Start + Stride * N) <= UMAX |
--- |
| 12926 |
// |
--- |
12926 |
// |
--- |
| 12927 |
// Subtracting Start from all the terms: |
--- |
12927 |
// Subtracting Start from all the terms: |
--- |
| 12928 |
// |
--- |
12928 |
// |
--- |
| 12929 |
// End - Start <= Stride * N <= UMAX - Start |
--- |
12929 |
// End - Start <= Stride * N <= UMAX - Start |
--- |
| 12930 |
// |
--- |
12930 |
// |
--- |
| 12931 |
// Since Start is unsigned, UMAX - Start <= UMAX. Therefore: |
--- |
12931 |
// Since Start is unsigned, UMAX - Start <= UMAX. Therefore: |
--- |
| 12932 |
// |
--- |
12932 |
// |
--- |
| 12933 |
// End - Start <= Stride * N <= UMAX |
--- |
12933 |
// End - Start <= Stride * N <= UMAX |
--- |
| 12934 |
// |
--- |
12934 |
// |
--- |
| 12935 |
// Stride * N is a multiple of Stride. Therefore, |
--- |
12935 |
// Stride * N is a multiple of Stride. Therefore, |
--- |
| 12936 |
// |
--- |
12936 |
// |
--- |
| 12937 |
// End - Start <= Stride * N <= UMAX - (UMAX mod Stride) |
--- |
12937 |
// End - Start <= Stride * N <= UMAX - (UMAX mod Stride) |
--- |
| 12938 |
// |
--- |
12938 |
// |
--- |
| 12939 |
// Since Stride is a power of two, UMAX + 1 is divisible by Stride. |
--- |
12939 |
// Since Stride is a power of two, UMAX + 1 is divisible by Stride. |
--- |
| 12940 |
// Therefore, UMAX mod Stride == Stride - 1. So we can write: |
--- |
12940 |
// Therefore, UMAX mod Stride == Stride - 1. So we can write: |
--- |
| 12941 |
// |
--- |
12941 |
// |
--- |
| 12942 |
// End - Start <= Stride * N <= UMAX - Stride - 1 |
--- |
12942 |
// End - Start <= Stride * N <= UMAX - Stride - 1 |
--- |
| 12943 |
// |
--- |
12943 |
// |
--- |
| 12944 |
// Dropping the middle term: |
--- |
12944 |
// Dropping the middle term: |
--- |
| 12945 |
// |
--- |
12945 |
// |
--- |
| 12946 |
// End - Start <= UMAX - Stride - 1 |
--- |
12946 |
// End - Start <= UMAX - Stride - 1 |
--- |
| 12947 |
// |
--- |
12947 |
// |
--- |
| 12948 |
// Adding Stride - 1 to both sides: |
--- |
12948 |
// Adding Stride - 1 to both sides: |
--- |
| 12949 |
// |
--- |
12949 |
// |
--- |
| 12950 |
// (End - Start) + (Stride - 1) <= UMAX |
--- |
12950 |
// (End - Start) + (Stride - 1) <= UMAX |
--- |
| 12951 |
// |
--- |
12951 |
// |
--- |
| 12952 |
// In other words, the addition doesn't have unsigned overflow. |
--- |
12952 |
// In other words, the addition doesn't have unsigned overflow. |
--- |
| 12953 |
// |
--- |
12953 |
// |
--- |
| 12954 |
// A similar proof works if we treat Start/End as signed values. |
--- |
12954 |
// A similar proof works if we treat Start/End as signed values. |
--- |
| 12955 |
// Just rewrite steps before "End - Start <= Stride * N <= UMAX" to |
--- |
12955 |
// Just rewrite steps before "End - Start <= Stride * N <= UMAX" to |
--- |
| 12956 |
// use signed max instead of unsigned max. Note that we're trying |
--- |
12956 |
// use signed max instead of unsigned max. Note that we're trying |
--- |
| 12957 |
// to prove a lack of unsigned overflow in either case. |
--- |
12957 |
// to prove a lack of unsigned overflow in either case. |
--- |
| 12958 |
return false; |
0 |
12958 |
return false; |
0 |
| 12959 |
} |
--- |
12959 |
} |
--- |
| 12960 |
} |
--- |
12960 |
} |
--- |
| 12961 |
if (Start == Stride || Start == getMinusSCEV(Stride, One)) { |
0 |
12961 |
if (Start == Stride || Start == getMinusSCEV(Stride, One)) { |
0 |
| 12962 |
// If Start is equal to Stride, (End - Start) + (Stride - 1) == End - 1. |
--- |
12962 |
// If Start is equal to Stride, (End - Start) + (Stride - 1) == End - 1. |
--- |
| 12963 |
// If !IsSigned, 0
| --- |
12963 |
// If !IsSigned, 0
| --- |
| 12964 |
// If IsSigned, 0
| --- |
12964 |
// If IsSigned, 0
| --- |
| |
| 12965 |
// |
--- |
12965 |
// |
--- |
| 12966 |
// If Start is equal to Stride - 1, (End - Start) + Stride - 1 == End. |
--- |
12966 |
// If Start is equal to Stride - 1, (End - Start) + Stride - 1 == End. |
--- |
| 12967 |
return false; |
0 |
12967 |
return false; |
0 |
| 12968 |
} |
--- |
12968 |
} |
--- |
| 12969 |
return true; |
0 |
12969 |
return true; |
0 |
| 12970 |
}(); |
0 |
12970 |
}(); |
0 |
| 12971 |
|
--- |
12971 |
|
--- |
| 12972 |
const SCEV *Delta = getMinusSCEV(End, Start); |
0 |
12972 |
const SCEV *Delta = getMinusSCEV(End, Start); |
0 |
| 12973 |
if (!MayAddOverflow) { |
0 |
12973 |
if (!MayAddOverflow) { |
0 |
| 12974 |
// floor((D + (S - 1)) / S) |
--- |
12974 |
// floor((D + (S - 1)) / S) |
--- |
| 12975 |
// We prefer this formulation if it's legal because it's fewer operations. |
--- |
12975 |
// We prefer this formulation if it's legal because it's fewer operations. |
--- |
| 12976 |
BECount = |
0 |
12976 |
BECount = |
0 |
| 12977 |
getUDivExpr(getAddExpr(Delta, getMinusSCEV(Stride, One)), Stride); |
0 |
12977 |
getUDivExpr(getAddExpr(Delta, getMinusSCEV(Stride, One)), Stride); |
0 |
| 12978 |
} else { |
--- |
12978 |
} else { |
--- |
| 12979 |
BECount = getUDivCeilSCEV(Delta, Stride); |
0 |
12979 |
BECount = getUDivCeilSCEV(Delta, Stride); |
0 |
| 12980 |
} |
--- |
12980 |
} |
--- |
| 12981 |
} |
--- |
12981 |
} |
--- |
| 12982 |
|
--- |
12982 |
|
--- |
| 12983 |
const SCEV *ConstantMaxBECount; |
--- |
12983 |
const SCEV *ConstantMaxBECount; |
--- |
| 12984 |
bool MaxOrZero = false; |
0 |
12984 |
bool MaxOrZero = false; |
0 |
| 12985 |
if (isa(BECount)) { |
0 |
12985 |
if (isa(BECount)) { |
0 |
| 12986 |
ConstantMaxBECount = BECount; |
0 |
12986 |
ConstantMaxBECount = BECount; |
0 |
| 12987 |
} else if (BECountIfBackedgeTaken && |
0 |
12987 |
} else if (BECountIfBackedgeTaken && |
0 |
| 12988 |
isa(BECountIfBackedgeTaken)) { |
0 |
12988 |
isa(BECountIfBackedgeTaken)) { |
0 |
| 12989 |
// If we know exactly how many times the backedge will be taken if it's |
--- |
12989 |
// If we know exactly how many times the backedge will be taken if it's |
--- |
| 12990 |
// taken at least once, then the backedge count will either be that or |
--- |
12990 |
// taken at least once, then the backedge count will either be that or |
--- |
| 12991 |
// zero. |
--- |
12991 |
// zero. |
--- |
| 12992 |
ConstantMaxBECount = BECountIfBackedgeTaken; |
0 |
12992 |
ConstantMaxBECount = BECountIfBackedgeTaken; |
0 |
| 12993 |
MaxOrZero = true; |
0 |
12993 |
MaxOrZero = true; |
0 |
| 12994 |
} else { |
--- |
12994 |
} else { |
--- |
| 12995 |
ConstantMaxBECount = computeMaxBECountForLT( |
0 |
12995 |
ConstantMaxBECount = computeMaxBECountForLT( |
0 |
| 12996 |
Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned); |
0 |
12996 |
Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned); |
0 |
| 12997 |
} |
--- |
12997 |
} |
--- |
| 12998 |
|
--- |
12998 |
|
--- |
| 12999 |
if (isa(ConstantMaxBECount) && |
0 |
12999 |
if (isa(ConstantMaxBECount) && |
0 |
| 13000 |
!isa(BECount)) |
0 |
13000 |
!isa(BECount)) |
0 |
| 13001 |
ConstantMaxBECount = getConstant(getUnsignedRangeMax(BECount)); |
0 |
13001 |
ConstantMaxBECount = getConstant(getUnsignedRangeMax(BECount)); |
0 |
| 13002 |
|
--- |
13002 |
|
--- |
| 13003 |
const SCEV *SymbolicMaxBECount = |
--- |
13003 |
const SCEV *SymbolicMaxBECount = |
--- |
| 13004 |
isa(BECount) ? ConstantMaxBECount : BECount; |
0 |
13004 |
isa(BECount) ? ConstantMaxBECount : BECount; |
0 |
| 13005 |
return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount, MaxOrZero, |
--- |
13005 |
return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount, MaxOrZero, |
--- |
| 13006 |
Predicates); |
0 |
13006 |
Predicates); |
0 |
| 13007 |
} |
0 |
13007 |
} |
0 |
| 13008 |
|
--- |
13008 |
|
--- |
| 13009 |
ScalarEvolution::ExitLimit ScalarEvolution::howManyGreaterThans( |
0 |
13009 |
ScalarEvolution::ExitLimit ScalarEvolution::howManyGreaterThans( |
0 |
| 13010 |
const SCEV *LHS, const SCEV *RHS, const Loop *L, bool IsSigned, |
--- |
13010 |
const SCEV *LHS, const SCEV *RHS, const Loop *L, bool IsSigned, |
--- |
| 13011 |
bool ControlsOnlyExit, bool AllowPredicates) { |
--- |
13011 |
bool ControlsOnlyExit, bool AllowPredicates) { |
--- |
| 13012 |
SmallPtrSet Predicates; |
0 |
13012 |
SmallPtrSet Predicates; |
0 |
| 13013 |
// We handle only IV > Invariant |
--- |
13013 |
// We handle only IV > Invariant |
--- |
| 13014 |
if (!isLoopInvariant(RHS, L)) |
0 |
13014 |
if (!isLoopInvariant(RHS, L)) |
0 |
| 13015 |
return getCouldNotCompute(); |
0 |
13015 |
return getCouldNotCompute(); |
0 |
| 13016 |
|
--- |
13016 |
|
--- |
| 13017 |
const SCEVAddRecExpr *IV = dyn_cast(LHS); |
0 |
13017 |
const SCEVAddRecExpr *IV = dyn_cast(LHS); |
0 |
| 13018 |
if (!IV && AllowPredicates) |
0 |
13018 |
if (!IV && AllowPredicates) |
0 |
| 13019 |
// Try to make this an AddRec using runtime tests, in the first X |
--- |
13019 |
// Try to make this an AddRec using runtime tests, in the first X |
--- |
| 13020 |
// iterations of this loop, where X is the SCEV expression found by the |
--- |
13020 |
// iterations of this loop, where X is the SCEV expression found by the |
--- |
| 13021 |
// algorithm below. |
--- |
13021 |
// algorithm below. |
--- |
| 13022 |
IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates); |
0 |
13022 |
IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates); |
0 |
| 13023 |
|
--- |
13023 |
|
--- |
| 13024 |
// Avoid weird loops |
--- |
13024 |
// Avoid weird loops |
--- |
| 13025 |
if (!IV || IV->getLoop() != L || !IV->isAffine()) |
0 |
13025 |
if (!IV || IV->getLoop() != L || !IV->isAffine()) |
0 |
| 13026 |
return getCouldNotCompute(); |
0 |
13026 |
return getCouldNotCompute(); |
0 |
| 13027 |
|
--- |
13027 |
|
--- |
| 13028 |
auto WrapType = IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW; |
0 |
13028 |
auto WrapType = IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW; |
0 |
| 13029 |
bool NoWrap = ControlsOnlyExit && IV->getNoWrapFlags(WrapType); |
0 |
13029 |
bool NoWrap = ControlsOnlyExit && IV->getNoWrapFlags(WrapType); |
0 |
| 13030 |
ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
0 |
13030 |
ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
0 |
| 13031 |
|
--- |
13031 |
|
--- |
| 13032 |
const SCEV *Stride = getNegativeSCEV(IV->getStepRecurrence(*this)); |
0 |
13032 |
const SCEV *Stride = getNegativeSCEV(IV->getStepRecurrence(*this)); |
0 |
| 13033 |
|
--- |
13033 |
|
--- |
| 13034 |
// Avoid negative or zero stride values |
--- |
13034 |
// Avoid negative or zero stride values |
--- |
| 13035 |
if (!isKnownPositive(Stride)) |
0 |
13035 |
if (!isKnownPositive(Stride)) |
0 |
| 13036 |
return getCouldNotCompute(); |
0 |
13036 |
return getCouldNotCompute(); |
0 |
| 13037 |
|
--- |
13037 |
|
--- |
| 13038 |
// Avoid proven overflow cases: this will ensure that the backedge taken count |
--- |
13038 |
// Avoid proven overflow cases: this will ensure that the backedge taken count |
--- |
| 13039 |
// will not generate any unsigned overflow. Relaxed no-overflow conditions |
--- |
13039 |
// will not generate any unsigned overflow. Relaxed no-overflow conditions |
--- |
| 13040 |
// exploit NoWrapFlags, allowing to optimize in presence of undefined |
--- |
13040 |
// exploit NoWrapFlags, allowing to optimize in presence of undefined |
--- |
| 13041 |
// behaviors like the case of C language. |
--- |
13041 |
// behaviors like the case of C language. |
--- |
| 13042 |
if (!Stride->isOne() && !NoWrap) |
0 |
13042 |
if (!Stride->isOne() && !NoWrap) |
0 |
| 13043 |
if (canIVOverflowOnGT(RHS, Stride, IsSigned)) |
0 |
13043 |
if (canIVOverflowOnGT(RHS, Stride, IsSigned)) |
0 |
| 13044 |
return getCouldNotCompute(); |
0 |
13044 |
return getCouldNotCompute(); |
0 |
| 13045 |
|
--- |
13045 |
|
--- |
| 13046 |
const SCEV *Start = IV->getStart(); |
0 |
13046 |
const SCEV *Start = IV->getStart(); |
0 |
| 13047 |
const SCEV *End = RHS; |
0 |
13047 |
const SCEV *End = RHS; |
0 |
| 13048 |
if (!isLoopEntryGuardedByCond(L, Cond, getAddExpr(Start, Stride), RHS)) { |
0 |
13048 |
if (!isLoopEntryGuardedByCond(L, Cond, getAddExpr(Start, Stride), RHS)) { |
0 |
| 13049 |
// If we know that Start >= RHS in the context of loop, then we know that |
--- |
13049 |
// If we know that Start >= RHS in the context of loop, then we know that |
--- |
| 13050 |
// min(RHS, Start) = RHS at this point. |
--- |
13050 |
// min(RHS, Start) = RHS at this point. |
--- |
| 13051 |
if (isLoopEntryGuardedByCond( |
0 |
13051 |
if (isLoopEntryGuardedByCond( |
0 |
| 13052 |
L, IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, Start, RHS)) |
--- |
13052 |
L, IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, Start, RHS)) |
--- |
| 13053 |
End = RHS; |
0 |
13053 |
End = RHS; |
0 |
| 13054 |
else |
--- |
13054 |
else |
--- |
| 13055 |
End = IsSigned ? getSMinExpr(RHS, Start) : getUMinExpr(RHS, Start); |
0 |
13055 |
End = IsSigned ? getSMinExpr(RHS, Start) : getUMinExpr(RHS, Start); |
0 |
| 13056 |
} |
--- |
13056 |
} |
--- |
| 13057 |
|
--- |
13057 |
|
--- |
| 13058 |
if (Start->getType()->isPointerTy()) { |
0 |
13058 |
if (Start->getType()->isPointerTy()) { |
0 |
| 13059 |
Start = getLosslessPtrToIntExpr(Start); |
0 |
13059 |
Start = getLosslessPtrToIntExpr(Start); |
0 |
| 13060 |
if (isa(Start)) |
0 |
13060 |
if (isa(Start)) |
0 |
| 13061 |
return Start; |
0 |
13061 |
return Start; |
0 |
| 13062 |
} |
--- |
13062 |
} |
--- |
| 13063 |
if (End->getType()->isPointerTy()) { |
0 |
13063 |
if (End->getType()->isPointerTy()) { |
0 |
| 13064 |
End = getLosslessPtrToIntExpr(End); |
0 |
13064 |
End = getLosslessPtrToIntExpr(End); |
0 |
| 13065 |
if (isa(End)) |
0 |
13065 |
if (isa(End)) |
0 |
| 13066 |
return End; |
0 |
13066 |
return End; |
0 |
| 13067 |
} |
--- |
13067 |
} |
--- |
| 13068 |
|
--- |
13068 |
|
--- |
| 13069 |
// Compute ((Start - End) + (Stride - 1)) / Stride. |
--- |
13069 |
// Compute ((Start - End) + (Stride - 1)) / Stride. |
--- |
| 13070 |
// FIXME: This can overflow. Holding off on fixing this for now; |
--- |
13070 |
// FIXME: This can overflow. Holding off on fixing this for now; |
--- |
| 13071 |
// howManyGreaterThans will hopefully be gone soon. |
--- |
13071 |
// howManyGreaterThans will hopefully be gone soon. |
--- |
| 13072 |
const SCEV *One = getOne(Stride->getType()); |
0 |
13072 |
const SCEV *One = getOne(Stride->getType()); |
0 |
| 13073 |
const SCEV *BECount = getUDivExpr( |
0 |
13073 |
const SCEV *BECount = getUDivExpr( |
0 |
| 13074 |
getAddExpr(getMinusSCEV(Start, End), getMinusSCEV(Stride, One)), Stride); |
0 |
13074 |
getAddExpr(getMinusSCEV(Start, End), getMinusSCEV(Stride, One)), Stride); |
0 |
| 13075 |
|
--- |
13075 |
|
--- |
| 13076 |
APInt MaxStart = IsSigned ? getSignedRangeMax(Start) |
--- |
13076 |
APInt MaxStart = IsSigned ? getSignedRangeMax(Start) |
--- |
| 13077 |
: getUnsignedRangeMax(Start); |
0 |
13077 |
: getUnsignedRangeMax(Start); |
0 |
| 13078 |
|
--- |
13078 |
|
--- |
| 13079 |
APInt MinStride = IsSigned ? getSignedRangeMin(Stride) |
--- |
13079 |
APInt MinStride = IsSigned ? getSignedRangeMin(Stride) |
--- |
| 13080 |
: getUnsignedRangeMin(Stride); |
0 |
13080 |
: getUnsignedRangeMin(Stride); |
0 |
| 13081 |
|
--- |
13081 |
|
--- |
| 13082 |
unsigned BitWidth = getTypeSizeInBits(LHS->getType()); |
0 |
13082 |
unsigned BitWidth = getTypeSizeInBits(LHS->getType()); |
0 |
| 13083 |
APInt Limit = IsSigned ? APInt::getSignedMinValue(BitWidth) + (MinStride - 1) |
0 |
13083 |
APInt Limit = IsSigned ? APInt::getSignedMinValue(BitWidth) + (MinStride - 1) |
0 |
| 13084 |
: APInt::getMinValue(BitWidth) + (MinStride - 1); |
0 |
13084 |
: APInt::getMinValue(BitWidth) + (MinStride - 1); |
0 |
| 13085 |
|
--- |
13085 |
|
--- |
| 13086 |
// Although End can be a MIN expression we estimate MinEnd considering only |
--- |
13086 |
// Although End can be a MIN expression we estimate MinEnd considering only |
--- |
| 13087 |
// the case End = RHS. This is safe because in the other case (Start - End) |
--- |
13087 |
// the case End = RHS. This is safe because in the other case (Start - End) |
--- |
| 13088 |
// is zero, leading to a zero maximum backedge taken count. |
--- |
13088 |
// is zero, leading to a zero maximum backedge taken count. |
--- |
| 13089 |
APInt MinEnd = |
--- |
13089 |
APInt MinEnd = |
--- |
| 13090 |
IsSigned ? APIntOps::smax(getSignedRangeMin(RHS), Limit) |
0 |
13090 |
IsSigned ? APIntOps::smax(getSignedRangeMin(RHS), Limit) |
0 |
| 13091 |
: APIntOps::umax(getUnsignedRangeMin(RHS), Limit); |
0 |
13091 |
: APIntOps::umax(getUnsignedRangeMin(RHS), Limit); |
0 |
| 13092 |
|
--- |
13092 |
|
--- |
| 13093 |
const SCEV *ConstantMaxBECount = |
--- |
13093 |
const SCEV *ConstantMaxBECount = |
--- |
| 13094 |
isa(BECount) |
0 |
13094 |
isa(BECount) |
0 |
| 13095 |
? BECount |
0 |
13095 |
? BECount |
0 |
| 13096 |
: getUDivCeilSCEV(getConstant(MaxStart - MinEnd), |
0 |
13096 |
: getUDivCeilSCEV(getConstant(MaxStart - MinEnd), |
0 |
| 13097 |
getConstant(MinStride)); |
0 |
13097 |
getConstant(MinStride)); |
0 |
| 13098 |
|
--- |
13098 |
|
--- |
| 13099 |
if (isa(ConstantMaxBECount)) |
0 |
13099 |
if (isa(ConstantMaxBECount)) |
0 |
| 13100 |
ConstantMaxBECount = BECount; |
0 |
13100 |
ConstantMaxBECount = BECount; |
0 |
| 13101 |
const SCEV *SymbolicMaxBECount = |
--- |
13101 |
const SCEV *SymbolicMaxBECount = |
--- |
| 13102 |
isa(BECount) ? ConstantMaxBECount : BECount; |
0 |
13102 |
isa(BECount) ? ConstantMaxBECount : BECount; |
0 |
| 13103 |
|
--- |
13103 |
|
--- |
| 13104 |
return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount, false, |
--- |
13104 |
return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount, false, |
--- |
| 13105 |
Predicates); |
0 |
13105 |
Predicates); |
0 |
| 13106 |
} |
0 |
13106 |
} |
0 |
| 13107 |
|
--- |
13107 |
|
--- |
| 13108 |
const SCEV *SCEVAddRecExpr::getNumIterationsInRange(const ConstantRange &Range, |
0 |
13108 |
const SCEV *SCEVAddRecExpr::getNumIterationsInRange(const ConstantRange &Range, |
0 |
| 13109 |
ScalarEvolution &SE) const { |
--- |
13109 |
ScalarEvolution &SE) const { |
--- |
| 13110 |
if (Range.isFullSet()) // Infinite loop. |
0 |
13110 |
if (Range.isFullSet()) // Infinite loop. |
0 |
| 13111 |
return SE.getCouldNotCompute(); |
0 |
13111 |
return SE.getCouldNotCompute(); |
0 |
| 13112 |
|
--- |
13112 |
|
--- |
| 13113 |
// If the start is a non-zero constant, shift the range to simplify things. |
--- |
13113 |
// If the start is a non-zero constant, shift the range to simplify things. |
--- |
| 13114 |
if (const SCEVConstant *SC = dyn_cast(getStart())) |
0 |
13114 |
if (const SCEVConstant *SC = dyn_cast(getStart())) |
0 |
| 13115 |
if (!SC->getValue()->isZero()) { |
0 |
13115 |
if (!SC->getValue()->isZero()) { |
0 |
| 13116 |
SmallVector Operands(operands()); |
0 |
13116 |
SmallVector Operands(operands()); |
0 |
| 13117 |
Operands[0] = SE.getZero(SC->getType()); |
0 |
13117 |
Operands[0] = SE.getZero(SC->getType()); |
0 |
| 13118 |
const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop(), |
0 |
13118 |
const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop(), |
0 |
| 13119 |
getNoWrapFlags(FlagNW)); |
--- |
13119 |
getNoWrapFlags(FlagNW)); |
--- |
| 13120 |
if (const auto *ShiftedAddRec = dyn_cast(Shifted)) |
0 |
13120 |
if (const auto *ShiftedAddRec = dyn_cast(Shifted)) |
0 |
| 13121 |
return ShiftedAddRec->getNumIterationsInRange( |
0 |
13121 |
return ShiftedAddRec->getNumIterationsInRange( |
0 |
| 13122 |
Range.subtract(SC->getAPInt()), SE); |
0 |
13122 |
Range.subtract(SC->getAPInt()), SE); |
0 |
| 13123 |
// This is strange and shouldn't happen. |
--- |
13123 |
// This is strange and shouldn't happen. |
--- |
| 13124 |
return SE.getCouldNotCompute(); |
0 |
13124 |
return SE.getCouldNotCompute(); |
0 |
| 13125 |
} |
0 |
13125 |
} |
0 |
| 13126 |
|
--- |
13126 |
|
--- |
| 13127 |
// The only time we can solve this is when we have all constant indices. |
--- |
13127 |
// The only time we can solve this is when we have all constant indices. |
--- |
| 13128 |
// Otherwise, we cannot determine the overflow conditions. |
--- |
13128 |
// Otherwise, we cannot determine the overflow conditions. |
--- |
| 13129 |
if (any_of(operands(), [](const SCEV *Op) { return !isa(Op); })) |
0 |
13129 |
if (any_of(operands(), [](const SCEV *Op) { return !isa(Op); })) |
0 |
| 13130 |
return SE.getCouldNotCompute(); |
0 |
13130 |
return SE.getCouldNotCompute(); |
0 |
| 13131 |
|
--- |
13131 |
|
--- |
| 13132 |
// Okay at this point we know that all elements of the chrec are constants and |
--- |
13132 |
// Okay at this point we know that all elements of the chrec are constants and |
--- |
| 13133 |
// that the start element is zero. |
--- |
13133 |
// that the start element is zero. |
--- |
| 13134 |
|
--- |
13134 |
|
--- |
| 13135 |
// First check to see if the range contains zero. If not, the first |
--- |
13135 |
// First check to see if the range contains zero. If not, the first |
--- |
| 13136 |
// iteration exits. |
--- |
13136 |
// iteration exits. |
--- |
| 13137 |
unsigned BitWidth = SE.getTypeSizeInBits(getType()); |
0 |
13137 |
unsigned BitWidth = SE.getTypeSizeInBits(getType()); |
0 |
| 13138 |
if (!Range.contains(APInt(BitWidth, 0))) |
0 |
13138 |
if (!Range.contains(APInt(BitWidth, 0))) |
0 |
| 13139 |
return SE.getZero(getType()); |
0 |
13139 |
return SE.getZero(getType()); |
0 |
| 13140 |
|
--- |
13140 |
|
--- |
| 13141 |
if (isAffine()) { |
0 |
13141 |
if (isAffine()) { |
0 |
| 13142 |
// If this is an affine expression then we have this situation: |
--- |
13142 |
// If this is an affine expression then we have this situation: |
--- |
| 13143 |
// Solve {0,+,A} in Range === Ax in Range |
--- |
13143 |
// Solve {0,+,A} in Range === Ax in Range |
--- |
| 13144 |
|
--- |
13144 |
|
--- |
| 13145 |
// We know that zero is in the range. If A is positive then we know that |
--- |
13145 |
// We know that zero is in the range. If A is positive then we know that |
--- |
| 13146 |
// the upper value of the range must be the first possible exit value. |
--- |
13146 |
// the upper value of the range must be the first possible exit value. |
--- |
| 13147 |
// If A is negative then the lower of the range is the last possible loop |
--- |
13147 |
// If A is negative then the lower of the range is the last possible loop |
--- |
| 13148 |
// value. Also note that we already checked for a full range. |
--- |
13148 |
// value. Also note that we already checked for a full range. |
--- |
| 13149 |
APInt A = cast(getOperand(1))->getAPInt(); |
0 |
13149 |
APInt A = cast(getOperand(1))->getAPInt(); |
0 |
| 13150 |
APInt End = A.sge(1) ? (Range.getUpper() - 1) : Range.getLower(); |
0 |
13150 |
APInt End = A.sge(1) ? (Range.getUpper() - 1) : Range.getLower(); |
0 |
| 13151 |
|
--- |
13151 |
|
--- |
| 13152 |
// The exit value should be (End+A)/A. |
--- |
13152 |
// The exit value should be (End+A)/A. |
--- |
| 13153 |
APInt ExitVal = (End + A).udiv(A); |
0 |
13153 |
APInt ExitVal = (End + A).udiv(A); |
0 |
| 13154 |
ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal); |
0 |
13154 |
ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal); |
0 |
| 13155 |
|
--- |
13155 |
|
--- |
| 13156 |
// Evaluate at the exit value. If we really did fall out of the valid |
--- |
13156 |
// Evaluate at the exit value. If we really did fall out of the valid |
--- |
| 13157 |
// range, then we computed our trip count, otherwise wrap around or other |
--- |
13157 |
// range, then we computed our trip count, otherwise wrap around or other |
--- |
| 13158 |
// things must have happened. |
--- |
13158 |
// things must have happened. |
--- |
| 13159 |
ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); |
0 |
13159 |
ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); |
0 |
| 13160 |
if (Range.contains(Val->getValue())) |
0 |
13160 |
if (Range.contains(Val->getValue())) |
0 |
| 13161 |
return SE.getCouldNotCompute(); // Something strange happened |
0 |
13161 |
return SE.getCouldNotCompute(); // Something strange happened |
0 |
| 13162 |
|
--- |
13162 |
|
--- |
| 13163 |
// Ensure that the previous value is in the range. |
--- |
13163 |
// Ensure that the previous value is in the range. |
--- |
| 13164 |
assert(Range.contains( |
0 |
13164 |
assert(Range.contains( |
0 |
| 13165 |
EvaluateConstantChrecAtConstant(this, |
--- |
13165 |
EvaluateConstantChrecAtConstant(this, |
--- |
| 13166 |
ConstantInt::get(SE.getContext(), ExitVal - 1), SE)->getValue()) && |
--- |
13166 |
ConstantInt::get(SE.getContext(), ExitVal - 1), SE)->getValue()) && |
--- |
| 13167 |
"Linear scev computation is off in a bad way!"); |
--- |
13167 |
"Linear scev computation is off in a bad way!"); |
--- |
| 13168 |
return SE.getConstant(ExitValue); |
0 |
13168 |
return SE.getConstant(ExitValue); |
0 |
| 13169 |
} |
0 |
13169 |
} |
0 |
| 13170 |
|
--- |
13170 |
|
--- |
| 13171 |
if (isQuadratic()) { |
0 |
13171 |
if (isQuadratic()) { |
0 |
| 13172 |
if (auto S = SolveQuadraticAddRecRange(this, Range, SE)) |
0 |
13172 |
if (auto S = SolveQuadraticAddRecRange(this, Range, SE)) |
0 |
| 13173 |
return SE.getConstant(*S); |
0 |
13173 |
return SE.getConstant(*S); |
0 |
| 13174 |
} |
--- |
13174 |
} |
--- |
| 13175 |
|
--- |
13175 |
|
--- |
| 13176 |
return SE.getCouldNotCompute(); |
0 |
13176 |
return SE.getCouldNotCompute(); |
0 |
| 13177 |
} |
--- |
13177 |
} |
--- |
| 13178 |
|
--- |
13178 |
|
--- |
| 13179 |
const SCEVAddRecExpr * |
--- |
13179 |
const SCEVAddRecExpr * |
--- |
| 13180 |
SCEVAddRecExpr::getPostIncExpr(ScalarEvolution &SE) const { |
0 |
13180 |
SCEVAddRecExpr::getPostIncExpr(ScalarEvolution &SE) const { |
0 |
| 13181 |
assert(getNumOperands() > 1 && "AddRec with zero step?"); |
0 |
13181 |
assert(getNumOperands() > 1 && "AddRec with zero step?"); |
0 |
| 13182 |
// There is a temptation to just call getAddExpr(this, getStepRecurrence(SE)), |
--- |
13182 |
// There is a temptation to just call getAddExpr(this, getStepRecurrence(SE)), |
--- |
| 13183 |
// but in this case we cannot guarantee that the value returned will be an |
--- |
13183 |
// but in this case we cannot guarantee that the value returned will be an |
--- |
| 13184 |
// AddRec because SCEV does not have a fixed point where it stops |
--- |
13184 |
// AddRec because SCEV does not have a fixed point where it stops |
--- |
| 13185 |
// simplification: it is legal to return ({rec1} + {rec2}). For example, it |
--- |
13185 |
// simplification: it is legal to return ({rec1} + {rec2}). For example, it |
--- |
| 13186 |
// may happen if we reach arithmetic depth limit while simplifying. So we |
--- |
13186 |
// may happen if we reach arithmetic depth limit while simplifying. So we |
--- |
| 13187 |
// construct the returned value explicitly. |
--- |
13187 |
// construct the returned value explicitly. |
--- |
| 13188 |
SmallVector Ops; |
0 |
13188 |
SmallVector Ops; |
0 |
| 13189 |
// If this is {A,+,B,+,C,...,+,N}, then its step is {B,+,C,+,...,+,N}, and |
--- |
13189 |
// If this is {A,+,B,+,C,...,+,N}, then its step is {B,+,C,+,...,+,N}, and |
--- |
| 13190 |
// (this + Step) is {A+B,+,B+C,+...,+,N}. |
--- |
13190 |
// (this + Step) is {A+B,+,B+C,+...,+,N}. |
--- |
| 13191 |
for (unsigned i = 0, e = getNumOperands() - 1; i < e; ++i) |
0 |
13191 |
for (unsigned i = 0, e = getNumOperands() - 1; i < e; ++i) |
0 |
| 13192 |
Ops.push_back(SE.getAddExpr(getOperand(i), getOperand(i + 1))); |
0 |
13192 |
Ops.push_back(SE.getAddExpr(getOperand(i), getOperand(i + 1))); |
0 |
| 13193 |
// We know that the last operand is not a constant zero (otherwise it would |
--- |
13193 |
// We know that the last operand is not a constant zero (otherwise it would |
--- |
| 13194 |
// have been popped out earlier). This guarantees us that if the result has |
--- |
13194 |
// have been popped out earlier). This guarantees us that if the result has |
--- |
| 13195 |
// the same last operand, then it will also not be popped out, meaning that |
--- |
13195 |
// the same last operand, then it will also not be popped out, meaning that |
--- |
| 13196 |
// the returned value will be an AddRec. |
--- |
13196 |
// the returned value will be an AddRec. |
--- |
| 13197 |
const SCEV *Last = getOperand(getNumOperands() - 1); |
0 |
13197 |
const SCEV *Last = getOperand(getNumOperands() - 1); |
0 |
| 13198 |
assert(!Last->isZero() && "Recurrency with zero step?"); |
0 |
13198 |
assert(!Last->isZero() && "Recurrency with zero step?"); |
0 |
| 13199 |
Ops.push_back(Last); |
0 |
13199 |
Ops.push_back(Last); |
0 |
| 13200 |
return cast(SE.getAddRecExpr(Ops, getLoop(), |
0 |
13200 |
return cast(SE.getAddRecExpr(Ops, getLoop(), |
0 |
| 13201 |
SCEV::FlagAnyWrap)); |
0 |
13201 |
SCEV::FlagAnyWrap)); |
0 |
| 13202 |
} |
0 |
13202 |
} |
0 |
| 13203 |
|
--- |
13203 |
|
--- |
| 13204 |
// Return true when S contains at least an undef value. |
--- |
13204 |
// Return true when S contains at least an undef value. |
--- |
| 13205 |
bool ScalarEvolution::containsUndefs(const SCEV *S) const { |
0 |
13205 |
bool ScalarEvolution::containsUndefs(const SCEV *S) const { |
0 |
| 13206 |
return SCEVExprContains(S, [](const SCEV *S) { |
0 |
13206 |
return SCEVExprContains(S, [](const SCEV *S) { |
0 |
| 13207 |
if (const auto *SU = dyn_cast(S)) |
0 |
13207 |
if (const auto *SU = dyn_cast(S)) |
0 |
| 13208 |
return isa(SU->getValue()); |
0 |
13208 |
return isa(SU->getValue()); |
0 |
| 13209 |
return false; |
0 |
13209 |
return false; |
0 |
| 13210 |
}); |
0 |
13210 |
}); |
0 |
| 13211 |
} |
--- |
13211 |
} |
--- |
| 13212 |
|
--- |
13212 |
|
--- |
| 13213 |
// Return true when S contains a value that is a nullptr. |
--- |
13213 |
// Return true when S contains a value that is a nullptr. |
--- |
| 13214 |
bool ScalarEvolution::containsErasedValue(const SCEV *S) const { |
0 |
13214 |
bool ScalarEvolution::containsErasedValue(const SCEV *S) const { |
0 |
| 13215 |
return SCEVExprContains(S, [](const SCEV *S) { |
0 |
13215 |
return SCEVExprContains(S, [](const SCEV *S) { |
0 |
| 13216 |
if (const auto *SU = dyn_cast(S)) |
0 |
13216 |
if (const auto *SU = dyn_cast(S)) |
0 |
| 13217 |
return SU->getValue() == nullptr; |
0 |
13217 |
return SU->getValue() == nullptr; |
0 |
| 13218 |
return false; |
0 |
13218 |
return false; |
0 |
| 13219 |
}); |
0 |
13219 |
}); |
0 |
| 13220 |
} |
--- |
13220 |
} |
--- |
| 13221 |
|
--- |
13221 |
|
--- |
| 13222 |
/// Return the size of an element read or written by Inst. |
--- |
13222 |
/// Return the size of an element read or written by Inst. |
--- |
| 13223 |
const SCEV *ScalarEvolution::getElementSize(Instruction *Inst) { |
0 |
13223 |
const SCEV *ScalarEvolution::getElementSize(Instruction *Inst) { |
0 |
| 13224 |
Type *Ty; |
--- |
13224 |
Type *Ty; |
--- |
| 13225 |
if (StoreInst *Store = dyn_cast(Inst)) |
0 |
13225 |
if (StoreInst *Store = dyn_cast(Inst)) |
0 |
| 13226 |
Ty = Store->getValueOperand()->getType(); |
0 |
13226 |
Ty = Store->getValueOperand()->getType(); |
0 |
| 13227 |
else if (LoadInst *Load = dyn_cast(Inst)) |
0 |
13227 |
else if (LoadInst *Load = dyn_cast(Inst)) |
0 |
| 13228 |
Ty = Load->getType(); |
0 |
13228 |
Ty = Load->getType(); |
0 |
| 13229 |
else |
--- |
13229 |
else |
--- |
| 13230 |
return nullptr; |
0 |
13230 |
return nullptr; |
0 |
| 13231 |
|
--- |
13231 |
|
--- |
| 13232 |
Type *ETy = getEffectiveSCEVType(PointerType::getUnqual(Ty)); |
0 |
13232 |
Type *ETy = getEffectiveSCEVType(PointerType::getUnqual(Ty)); |
0 |
| 13233 |
return getSizeOfExpr(ETy, Ty); |
0 |
13233 |
return getSizeOfExpr(ETy, Ty); |
0 |
| 13234 |
} |
--- |
13234 |
} |
--- |
| 13235 |
|
--- |
13235 |
|
--- |
| 13236 |
//===----------------------------------------------------------------------===// |
--- |
13236 |
//===----------------------------------------------------------------------===// |
--- |
| 13237 |
// SCEVCallbackVH Class Implementation |
--- |
13237 |
// SCEVCallbackVH Class Implementation |
--- |
| 13238 |
//===----------------------------------------------------------------------===// |
--- |
13238 |
//===----------------------------------------------------------------------===// |
--- |
| 13239 |
|
--- |
13239 |
|
--- |
| 13240 |
void ScalarEvolution::SCEVCallbackVH::deleted() { |
0 |
13240 |
void ScalarEvolution::SCEVCallbackVH::deleted() { |
0 |
| 13241 |
assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
0 |
13241 |
assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
0 |
| 13242 |
if (PHINode *PN = dyn_cast(getValPtr())) |
0 |
13242 |
if (PHINode *PN = dyn_cast(getValPtr())) |
0 |
| 13243 |
SE->ConstantEvolutionLoopExitValue.erase(PN); |
0 |
13243 |
SE->ConstantEvolutionLoopExitValue.erase(PN); |
0 |
| 13244 |
SE->eraseValueFromMap(getValPtr()); |
0 |
13244 |
SE->eraseValueFromMap(getValPtr()); |
0 |
| 13245 |
// this now dangles! |
--- |
13245 |
// this now dangles! |
--- |
| 13246 |
} |
0 |
13246 |
} |
0 |
| 13247 |
|
--- |
13247 |
|
--- |
| 13248 |
void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *V) { |
0 |
13248 |
void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *V) { |
0 |
| 13249 |
assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
0 |
13249 |
assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
0 |
| 13250 |
|
--- |
13250 |
|
--- |
| 13251 |
// Forget all the expressions associated with users of the old value, |
--- |
13251 |
// Forget all the expressions associated with users of the old value, |
--- |
| 13252 |
// so that future queries will recompute the expressions using the new |
--- |
13252 |
// so that future queries will recompute the expressions using the new |
--- |
| 13253 |
// value. |
--- |
13253 |
// value. |
--- |
| 13254 |
Value *Old = getValPtr(); |
0 |
13254 |
Value *Old = getValPtr(); |
0 |
| 13255 |
SmallVector Worklist(Old->users()); |
0 |
13255 |
SmallVector Worklist(Old->users()); |
0 |
| 13256 |
SmallPtrSet Visited; |
0 |
13256 |
SmallPtrSet Visited; |
0 |
| 13257 |
while (!Worklist.empty()) { |
0 |
13257 |
while (!Worklist.empty()) { |
0 |
| 13258 |
User *U = Worklist.pop_back_val(); |
0 |
13258 |
User *U = Worklist.pop_back_val(); |
0 |
| 13259 |
// Deleting the Old value will cause this to dangle. Postpone |
--- |
13259 |
// Deleting the Old value will cause this to dangle. Postpone |
--- |
| 13260 |
// that until everything else is done. |
--- |
13260 |
// that until everything else is done. |
--- |
| 13261 |
if (U == Old) |
0 |
13261 |
if (U == Old) |
0 |
| 13262 |
continue; |
0 |
13262 |
continue; |
0 |
| 13263 |
if (!Visited.insert(U).second) |
0 |
13263 |
if (!Visited.insert(U).second) |
0 |
| 13264 |
continue; |
0 |
13264 |
continue; |
0 |
| 13265 |
if (PHINode *PN = dyn_cast(U)) |
0 |
13265 |
if (PHINode *PN = dyn_cast(U)) |
0 |
| 13266 |
SE->ConstantEvolutionLoopExitValue.erase(PN); |
0 |
13266 |
SE->ConstantEvolutionLoopExitValue.erase(PN); |
0 |
| 13267 |
SE->eraseValueFromMap(U); |
0 |
13267 |
SE->eraseValueFromMap(U); |
0 |
| 13268 |
llvm::append_range(Worklist, U->users()); |
0 |
13268 |
llvm::append_range(Worklist, U->users()); |
0 |
| 13269 |
} |
--- |
13269 |
} |
--- |
| 13270 |
// Delete the Old value. |
--- |
13270 |
// Delete the Old value. |
--- |
| 13271 |
if (PHINode *PN = dyn_cast(Old)) |
0 |
13271 |
if (PHINode *PN = dyn_cast(Old)) |
0 |
| 13272 |
SE->ConstantEvolutionLoopExitValue.erase(PN); |
0 |
13272 |
SE->ConstantEvolutionLoopExitValue.erase(PN); |
0 |
| 13273 |
SE->eraseValueFromMap(Old); |
0 |
13273 |
SE->eraseValueFromMap(Old); |
0 |
| 13274 |
// this now dangles! |
--- |
13274 |
// this now dangles! |
--- |
| 13275 |
} |
0 |
13275 |
} |
0 |
| 13276 |
|
--- |
13276 |
|
--- |
| 13277 |
ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) |
0 |
13277 |
ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) |
0 |
| 13278 |
: CallbackVH(V), SE(se) {} |
0 |
13278 |
: CallbackVH(V), SE(se) {} |
0 |
| 13279 |
|
--- |
13279 |
|
--- |
| 13280 |
//===----------------------------------------------------------------------===// |
--- |
13280 |
//===----------------------------------------------------------------------===// |
--- |
| 13281 |
// ScalarEvolution Class Implementation |
--- |
13281 |
// ScalarEvolution Class Implementation |
--- |
| 13282 |
//===----------------------------------------------------------------------===// |
--- |
13282 |
//===----------------------------------------------------------------------===// |
--- |
| 13283 |
|
--- |
13283 |
|
--- |
| 13284 |
ScalarEvolution::ScalarEvolution(Function &F, TargetLibraryInfo &TLI, |
1 |
13284 |
ScalarEvolution::ScalarEvolution(Function &F, TargetLibraryInfo &TLI, |
1 |
| 13285 |
AssumptionCache &AC, DominatorTree &DT, |
--- |
13285 |
AssumptionCache &AC, DominatorTree &DT, |
--- |
| 13286 |
LoopInfo &LI) |
--- |
13286 |
LoopInfo &LI) |
--- |
| 13287 |
: F(F), TLI(TLI), AC(AC), DT(DT), LI(LI), |
1 |
13287 |
: F(F), TLI(TLI), AC(AC), DT(DT), LI(LI), |
1 |
| 13288 |
CouldNotCompute(new SCEVCouldNotCompute()), ValuesAtScopes(64), |
2 |
13288 |
CouldNotCompute(new SCEVCouldNotCompute()), ValuesAtScopes(64), |
2 |
| 13289 |
LoopDispositions(64), BlockDispositions(64) { |
2 |
13289 |
LoopDispositions(64), BlockDispositions(64) { |
2 |
| 13290 |
// To use guards for proving predicates, we need to scan every instruction in |
--- |
13290 |
// To use guards for proving predicates, we need to scan every instruction in |
--- |
| 13291 |
// relevant basic blocks, and not just terminators. Doing this is a waste of |
--- |
13291 |
// relevant basic blocks, and not just terminators. Doing this is a waste of |
--- |
| 13292 |
// time if the IR does not actually contain any calls to |
--- |
13292 |
// time if the IR does not actually contain any calls to |
--- |
| 13293 |
// @llvm.experimental.guard, so do a quick check and remember this beforehand. |
--- |
13293 |
// @llvm.experimental.guard, so do a quick check and remember this beforehand. |
--- |
| 13294 |
// |
--- |
13294 |
// |
--- |
| 13295 |
// This pessimizes the case where a pass that preserves ScalarEvolution wants |
--- |
13295 |
// This pessimizes the case where a pass that preserves ScalarEvolution wants |
--- |
| 13296 |
// to _add_ guards to the module when there weren't any before, and wants |
--- |
13296 |
// to _add_ guards to the module when there weren't any before, and wants |
--- |
| 13297 |
// ScalarEvolution to optimize based on those guards. For now we prefer to be |
--- |
13297 |
// ScalarEvolution to optimize based on those guards. For now we prefer to be |
--- |
| 13298 |
// efficient in lieu of being smart in that rather obscure case. |
--- |
13298 |
// efficient in lieu of being smart in that rather obscure case. |
--- |
| 13299 |
|
--- |
13299 |
|
--- |
| 13300 |
auto *GuardDecl = F.getParent()->getFunction( |
1 |
13300 |
auto *GuardDecl = F.getParent()->getFunction( |
1 |
| 13301 |
Intrinsic::getName(Intrinsic::experimental_guard)); |
--- |
13301 |
Intrinsic::getName(Intrinsic::experimental_guard)); |
--- |
| 13302 |
HasGuards = GuardDecl && !GuardDecl->use_empty(); |
1 |
13302 |
HasGuards = GuardDecl && !GuardDecl->use_empty(); |
1 |
| 13303 |
} |
1 |
13303 |
} |
1 |
| 13304 |
|
--- |
13304 |
|
--- |
| 13305 |
ScalarEvolution::ScalarEvolution(ScalarEvolution &&Arg) |
0 |
13305 |
ScalarEvolution::ScalarEvolution(ScalarEvolution &&Arg) |
0 |
| 13306 |
: F(Arg.F), HasGuards(Arg.HasGuards), TLI(Arg.TLI), AC(Arg.AC), DT(Arg.DT), |
0 |
13306 |
: F(Arg.F), HasGuards(Arg.HasGuards), TLI(Arg.TLI), AC(Arg.AC), DT(Arg.DT), |
0 |
| 13307 |
LI(Arg.LI), CouldNotCompute(std::move(Arg.CouldNotCompute)), |
0 |
13307 |
LI(Arg.LI), CouldNotCompute(std::move(Arg.CouldNotCompute)), |
0 |
| 13308 |
ValueExprMap(std::move(Arg.ValueExprMap)), |
0 |
13308 |
ValueExprMap(std::move(Arg.ValueExprMap)), |
0 |
| 13309 |
PendingLoopPredicates(std::move(Arg.PendingLoopPredicates)), |
0 |
13309 |
PendingLoopPredicates(std::move(Arg.PendingLoopPredicates)), |
0 |
| 13310 |
PendingPhiRanges(std::move(Arg.PendingPhiRanges)), |
0 |
13310 |
PendingPhiRanges(std::move(Arg.PendingPhiRanges)), |
0 |
| 13311 |
PendingMerges(std::move(Arg.PendingMerges)), |
0 |
13311 |
PendingMerges(std::move(Arg.PendingMerges)), |
0 |
| 13312 |
ConstantMultipleCache(std::move(Arg.ConstantMultipleCache)), |
0 |
13312 |
ConstantMultipleCache(std::move(Arg.ConstantMultipleCache)), |
0 |
| 13313 |
BackedgeTakenCounts(std::move(Arg.BackedgeTakenCounts)), |
0 |
13313 |
BackedgeTakenCounts(std::move(Arg.BackedgeTakenCounts)), |
0 |
| 13314 |
PredicatedBackedgeTakenCounts( |
0 |
13314 |
PredicatedBackedgeTakenCounts( |
0 |
| 13315 |
std::move(Arg.PredicatedBackedgeTakenCounts)), |
0 |
13315 |
std::move(Arg.PredicatedBackedgeTakenCounts)), |
0 |
| 13316 |
BECountUsers(std::move(Arg.BECountUsers)), |
0 |
13316 |
BECountUsers(std::move(Arg.BECountUsers)), |
0 |
| 13317 |
ConstantEvolutionLoopExitValue( |
0 |
13317 |
ConstantEvolutionLoopExitValue( |
0 |
| 13318 |
std::move(Arg.ConstantEvolutionLoopExitValue)), |
0 |
13318 |
std::move(Arg.ConstantEvolutionLoopExitValue)), |
0 |
| 13319 |
ValuesAtScopes(std::move(Arg.ValuesAtScopes)), |
0 |
13319 |
ValuesAtScopes(std::move(Arg.ValuesAtScopes)), |
0 |
| 13320 |
ValuesAtScopesUsers(std::move(Arg.ValuesAtScopesUsers)), |
0 |
13320 |
ValuesAtScopesUsers(std::move(Arg.ValuesAtScopesUsers)), |
0 |
| 13321 |
LoopDispositions(std::move(Arg.LoopDispositions)), |
0 |
13321 |
LoopDispositions(std::move(Arg.LoopDispositions)), |
0 |
| 13322 |
LoopPropertiesCache(std::move(Arg.LoopPropertiesCache)), |
0 |
13322 |
LoopPropertiesCache(std::move(Arg.LoopPropertiesCache)), |
0 |
| 13323 |
BlockDispositions(std::move(Arg.BlockDispositions)), |
0 |
13323 |
BlockDispositions(std::move(Arg.BlockDispositions)), |
0 |
| 13324 |
SCEVUsers(std::move(Arg.SCEVUsers)), |
0 |
13324 |
SCEVUsers(std::move(Arg.SCEVUsers)), |
0 |
| 13325 |
UnsignedRanges(std::move(Arg.UnsignedRanges)), |
0 |
13325 |
UnsignedRanges(std::move(Arg.UnsignedRanges)), |
0 |
| 13326 |
SignedRanges(std::move(Arg.SignedRanges)), |
0 |
13326 |
SignedRanges(std::move(Arg.SignedRanges)), |
0 |
| 13327 |
UniqueSCEVs(std::move(Arg.UniqueSCEVs)), |
0 |
13327 |
UniqueSCEVs(std::move(Arg.UniqueSCEVs)), |
0 |
| 13328 |
UniquePreds(std::move(Arg.UniquePreds)), |
0 |
13328 |
UniquePreds(std::move(Arg.UniquePreds)), |
0 |
| 13329 |
SCEVAllocator(std::move(Arg.SCEVAllocator)), |
0 |
13329 |
SCEVAllocator(std::move(Arg.SCEVAllocator)), |
0 |
| 13330 |
LoopUsers(std::move(Arg.LoopUsers)), |
0 |
13330 |
LoopUsers(std::move(Arg.LoopUsers)), |
0 |
| 13331 |
PredicatedSCEVRewrites(std::move(Arg.PredicatedSCEVRewrites)), |
0 |
13331 |
PredicatedSCEVRewrites(std::move(Arg.PredicatedSCEVRewrites)), |
0 |
| 13332 |
FirstUnknown(Arg.FirstUnknown) { |
0 |
13332 |
FirstUnknown(Arg.FirstUnknown) { |
0 |
| 13333 |
Arg.FirstUnknown = nullptr; |
0 |
13333 |
Arg.FirstUnknown = nullptr; |
0 |
| 13334 |
} |
0 |
13334 |
} |
0 |
| 13335 |
|
--- |
13335 |
|
--- |
| 13336 |
ScalarEvolution::~ScalarEvolution() { |
1 |
13336 |
ScalarEvolution::~ScalarEvolution() { |
1 |
| 13337 |
// Iterate through all the SCEVUnknown instances and call their |
--- |
13337 |
// Iterate through all the SCEVUnknown instances and call their |
--- |
| 13338 |
// destructors, so that they release their references to their values. |
--- |
13338 |
// destructors, so that they release their references to their values. |
--- |
| 13339 |
for (SCEVUnknown *U = FirstUnknown; U;) { |
1 |
13339 |
for (SCEVUnknown *U = FirstUnknown; U;) { |
1 |
| 13340 |
SCEVUnknown *Tmp = U; |
0 |
13340 |
SCEVUnknown *Tmp = U; |
0 |
| 13341 |
U = U->Next; |
0 |
13341 |
U = U->Next; |
0 |
| 13342 |
Tmp->~SCEVUnknown(); |
0 |
13342 |
Tmp->~SCEVUnknown(); |
0 |
| 13343 |
} |
--- |
13343 |
} |
--- |
| 13344 |
FirstUnknown = nullptr; |
1 |
13344 |
FirstUnknown = nullptr; |
1 |
| 13345 |
|
--- |
13345 |
|
--- |
| 13346 |
ExprValueMap.clear(); |
1 |
13346 |
ExprValueMap.clear(); |
1 |
| 13347 |
ValueExprMap.clear(); |
1 |
13347 |
ValueExprMap.clear(); |
1 |
| 13348 |
HasRecMap.clear(); |
1 |
13348 |
HasRecMap.clear(); |
1 |
| 13349 |
BackedgeTakenCounts.clear(); |
1 |
13349 |
BackedgeTakenCounts.clear(); |
1 |
| 13350 |
PredicatedBackedgeTakenCounts.clear(); |
1 |
13350 |
PredicatedBackedgeTakenCounts.clear(); |
1 |
| 13351 |
|
--- |
13351 |
|
--- |
| 13352 |
assert(PendingLoopPredicates.empty() && "isImpliedCond garbage"); |
1 |
13352 |
assert(PendingLoopPredicates.empty() && "isImpliedCond garbage"); |
1 |
| 13353 |
assert(PendingPhiRanges.empty() && "getRangeRef garbage"); |
1 |
13353 |
assert(PendingPhiRanges.empty() && "getRangeRef garbage"); |
1 |
| 13354 |
assert(PendingMerges.empty() && "isImpliedViaMerge garbage"); |
1 |
13354 |
assert(PendingMerges.empty() && "isImpliedViaMerge garbage"); |
1 |
| 13355 |
assert(!WalkingBEDominatingConds && "isLoopBackedgeGuardedByCond garbage!"); |
1 |
13355 |
assert(!WalkingBEDominatingConds && "isLoopBackedgeGuardedByCond garbage!"); |
1 |
| 13356 |
assert(!ProvingSplitPredicate && "ProvingSplitPredicate garbage!"); |
1 |
13356 |
assert(!ProvingSplitPredicate && "ProvingSplitPredicate garbage!"); |
1 |
| 13357 |
} |
1 |
13357 |
} |
1 |
| 13358 |
|
--- |
13358 |
|
--- |
| 13359 |
bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { |
0 |
13359 |
bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { |
0 |
| 13360 |
return !isa(getBackedgeTakenCount(L)); |
0 |
13360 |
return !isa(getBackedgeTakenCount(L)); |
0 |
| 13361 |
} |
--- |
13361 |
} |
--- |
| 13362 |
|
--- |
13362 |
|
--- |
| 13363 |
static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, |
0 |
13363 |
static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, |
0 |
| 13364 |
const Loop *L) { |
--- |
13364 |
const Loop *L) { |
--- |
| 13365 |
// Print all inner loops first |
--- |
13365 |
// Print all inner loops first |
--- |
| 13366 |
for (Loop *I : *L) |
0 |
13366 |
for (Loop *I : *L) |
0 |
| 13367 |
PrintLoopInfo(OS, SE, I); |
0 |
13367 |
PrintLoopInfo(OS, SE, I); |
0 |
| 13368 |
|
--- |
13368 |
|
--- |
| 13369 |
OS << "Loop "; |
0 |
13369 |
OS << "Loop "; |
0 |
| 13370 |
L->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
13370 |
L->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
| 13371 |
OS << ": "; |
0 |
13371 |
OS << ": "; |
0 |
| 13372 |
|
--- |
13372 |
|
--- |
| 13373 |
SmallVector ExitingBlocks; |
0 |
13373 |
SmallVector ExitingBlocks; |
0 |
| 13374 |
L->getExitingBlocks(ExitingBlocks); |
0 |
13374 |
L->getExitingBlocks(ExitingBlocks); |
0 |
| 13375 |
if (ExitingBlocks.size() != 1) |
0 |
13375 |
if (ExitingBlocks.size() != 1) |
0 |
| 13376 |
OS << " "; |
0 |
13376 |
OS << " "; |
0 |
| 13377 |
|
--- |
13377 |
|
--- |
| 13378 |
if (SE->hasLoopInvariantBackedgeTakenCount(L)) |
0 |
13378 |
if (SE->hasLoopInvariantBackedgeTakenCount(L)) |
0 |
| 13379 |
OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L) << "\n"; |
0 |
13379 |
OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L) << "\n"; |
0 |
| 13380 |
else |
--- |
13380 |
else |
--- |
| 13381 |
OS << "Unpredictable backedge-taken count.\n"; |
0 |
13381 |
OS << "Unpredictable backedge-taken count.\n"; |
0 |
| 13382 |
|
--- |
13382 |
|
--- |
| 13383 |
if (ExitingBlocks.size() > 1) |
0 |
13383 |
if (ExitingBlocks.size() > 1) |
0 |
| 13384 |
for (BasicBlock *ExitingBlock : ExitingBlocks) { |
0 |
13384 |
for (BasicBlock *ExitingBlock : ExitingBlocks) { |
0 |
| 13385 |
OS << " exit count for " << ExitingBlock->getName() << ": " |
0 |
13385 |
OS << " exit count for " << ExitingBlock->getName() << ": " |
0 |
| 13386 |
<< *SE->getExitCount(L, ExitingBlock) << "\n"; |
0 |
13386 |
<< *SE->getExitCount(L, ExitingBlock) << "\n"; |
0 |
| 13387 |
} |
--- |
13387 |
} |
--- |
| 13388 |
|
--- |
13388 |
|
--- |
| 13389 |
OS << "Loop "; |
0 |
13389 |
OS << "Loop "; |
0 |
| 13390 |
L->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
13390 |
L->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
| 13391 |
OS << ": "; |
0 |
13391 |
OS << ": "; |
0 |
| 13392 |
|
--- |
13392 |
|
--- |
| 13393 |
auto *ConstantBTC = SE->getConstantMaxBackedgeTakenCount(L); |
0 |
13393 |
auto *ConstantBTC = SE->getConstantMaxBackedgeTakenCount(L); |
0 |
| 13394 |
if (!isa(ConstantBTC)) { |
0 |
13394 |
if (!isa(ConstantBTC)) { |
0 |
| 13395 |
OS << "constant max backedge-taken count is " << *ConstantBTC; |
0 |
13395 |
OS << "constant max backedge-taken count is " << *ConstantBTC; |
0 |
| 13396 |
if (SE->isBackedgeTakenCountMaxOrZero(L)) |
0 |
13396 |
if (SE->isBackedgeTakenCountMaxOrZero(L)) |
0 |
| 13397 |
OS << ", actual taken count either this or zero."; |
0 |
13397 |
OS << ", actual taken count either this or zero."; |
0 |
| 13398 |
} else { |
--- |
13398 |
} else { |
--- |
| 13399 |
OS << "Unpredictable constant max backedge-taken count. "; |
0 |
13399 |
OS << "Unpredictable constant max backedge-taken count. "; |
0 |
| 13400 |
} |
--- |
13400 |
} |
--- |
| 13401 |
|
--- |
13401 |
|
--- |
| 13402 |
OS << "\n" |
--- |
13402 |
OS << "\n" |
--- |
| 13403 |
"Loop "; |
0 |
13403 |
"Loop "; |
0 |
| 13404 |
L->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
13404 |
L->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
| 13405 |
OS << ": "; |
0 |
13405 |
OS << ": "; |
0 |
| 13406 |
|
--- |
13406 |
|
--- |
| 13407 |
auto *SymbolicBTC = SE->getSymbolicMaxBackedgeTakenCount(L); |
0 |
13407 |
auto *SymbolicBTC = SE->getSymbolicMaxBackedgeTakenCount(L); |
0 |
| 13408 |
if (!isa(SymbolicBTC)) { |
0 |
13408 |
if (!isa(SymbolicBTC)) { |
0 |
| 13409 |
OS << "symbolic max backedge-taken count is " << *SymbolicBTC; |
0 |
13409 |
OS << "symbolic max backedge-taken count is " << *SymbolicBTC; |
0 |
| 13410 |
if (SE->isBackedgeTakenCountMaxOrZero(L)) |
0 |
13410 |
if (SE->isBackedgeTakenCountMaxOrZero(L)) |
0 |
| 13411 |
OS << ", actual taken count either this or zero."; |
0 |
13411 |
OS << ", actual taken count either this or zero."; |
0 |
| 13412 |
} else { |
--- |
13412 |
} else { |
--- |
| 13413 |
OS << "Unpredictable symbolic max backedge-taken count. "; |
0 |
13413 |
OS << "Unpredictable symbolic max backedge-taken count. "; |
0 |
| 13414 |
} |
--- |
13414 |
} |
--- |
| 13415 |
|
--- |
13415 |
|
--- |
| 13416 |
OS << "\n"; |
0 |
13416 |
OS << "\n"; |
0 |
| 13417 |
if (ExitingBlocks.size() > 1) |
0 |
13417 |
if (ExitingBlocks.size() > 1) |
0 |
| 13418 |
for (BasicBlock *ExitingBlock : ExitingBlocks) { |
0 |
13418 |
for (BasicBlock *ExitingBlock : ExitingBlocks) { |
0 |
| 13419 |
OS << " symbolic max exit count for " << ExitingBlock->getName() << ": " |
0 |
13419 |
OS << " symbolic max exit count for " << ExitingBlock->getName() << ": " |
0 |
| 13420 |
<< *SE->getExitCount(L, ExitingBlock, ScalarEvolution::SymbolicMaximum) |
0 |
13420 |
<< *SE->getExitCount(L, ExitingBlock, ScalarEvolution::SymbolicMaximum) |
0 |
| 13421 |
<< "\n"; |
0 |
13421 |
<< "\n"; |
0 |
| 13422 |
} |
--- |
13422 |
} |
--- |
| 13423 |
|
--- |
13423 |
|
--- |
| 13424 |
OS << "Loop "; |
0 |
13424 |
OS << "Loop "; |
0 |
| 13425 |
L->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
13425 |
L->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
| 13426 |
OS << ": "; |
0 |
13426 |
OS << ": "; |
0 |
| 13427 |
|
--- |
13427 |
|
--- |
| 13428 |
SmallVector Preds; |
0 |
13428 |
SmallVector Preds; |
0 |
| 13429 |
auto PBT = SE->getPredicatedBackedgeTakenCount(L, Preds); |
0 |
13429 |
auto PBT = SE->getPredicatedBackedgeTakenCount(L, Preds); |
0 |
| 13430 |
if (!isa(PBT)) { |
0 |
13430 |
if (!isa(PBT)) { |
0 |
| 13431 |
OS << "Predicated backedge-taken count is " << *PBT << "\n"; |
0 |
13431 |
OS << "Predicated backedge-taken count is " << *PBT << "\n"; |
0 |
| 13432 |
OS << " Predicates:\n"; |
0 |
13432 |
OS << " Predicates:\n"; |
0 |
| 13433 |
for (const auto *P : Preds) |
0 |
13433 |
for (const auto *P : Preds) |
0 |
| 13434 |
P->print(OS, 4); |
0 |
13434 |
P->print(OS, 4); |
0 |
| 13435 |
} else { |
--- |
13435 |
} else { |
--- |
| 13436 |
OS << "Unpredictable predicated backedge-taken count. "; |
0 |
13436 |
OS << "Unpredictable predicated backedge-taken count. "; |
0 |
| 13437 |
} |
--- |
13437 |
} |
--- |
| 13438 |
OS << "\n"; |
0 |
13438 |
OS << "\n"; |
0 |
| 13439 |
|
--- |
13439 |
|
--- |
| 13440 |
if (SE->hasLoopInvariantBackedgeTakenCount(L)) { |
0 |
13440 |
if (SE->hasLoopInvariantBackedgeTakenCount(L)) { |
0 |
| 13441 |
OS << "Loop "; |
0 |
13441 |
OS << "Loop "; |
0 |
| 13442 |
L->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
13442 |
L->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
| 13443 |
OS << ": "; |
0 |
13443 |
OS << ": "; |
0 |
| 13444 |
OS << "Trip multiple is " << SE->getSmallConstantTripMultiple(L) << "\n"; |
0 |
13444 |
OS << "Trip multiple is " << SE->getSmallConstantTripMultiple(L) << "\n"; |
0 |
| 13445 |
} |
--- |
13445 |
} |
--- |
| 13446 |
} |
0 |
13446 |
} |
0 |
| 13447 |
|
--- |
13447 |
|
--- |
| 13448 |
namespace llvm { |
--- |
13448 |
namespace llvm { |
--- |
| 13449 |
raw_ostream &operator<<(raw_ostream &OS, ScalarEvolution::LoopDisposition LD) { |
0 |
13449 |
raw_ostream &operator<<(raw_ostream &OS, ScalarEvolution::LoopDisposition LD) { |
0 |
| 13450 |
switch (LD) { |
0 |
13450 |
switch (LD) { |
0 |
| 13451 |
case ScalarEvolution::LoopVariant: |
0 |
13451 |
case ScalarEvolution::LoopVariant: |
0 |
| 13452 |
OS << "Variant"; |
0 |
13452 |
OS << "Variant"; |
0 |
| 13453 |
break; |
0 |
13453 |
break; |
0 |
| 13454 |
case ScalarEvolution::LoopInvariant: |
0 |
13454 |
case ScalarEvolution::LoopInvariant: |
0 |
| 13455 |
OS << "Invariant"; |
0 |
13455 |
OS << "Invariant"; |
0 |
| 13456 |
break; |
0 |
13456 |
break; |
0 |
| 13457 |
case ScalarEvolution::LoopComputable: |
0 |
13457 |
case ScalarEvolution::LoopComputable: |
0 |
| 13458 |
OS << "Computable"; |
0 |
13458 |
OS << "Computable"; |
0 |
| 13459 |
break; |
0 |
13459 |
break; |
0 |
| 13460 |
} |
--- |
13460 |
} |
--- |
| 13461 |
return OS; |
0 |
13461 |
return OS; |
0 |
| 13462 |
} |
--- |
13462 |
} |
--- |
| 13463 |
|
--- |
13463 |
|
--- |
| 13464 |
raw_ostream &operator<<(raw_ostream &OS, ScalarEvolution::BlockDisposition BD) { |
0 |
13464 |
raw_ostream &operator<<(raw_ostream &OS, ScalarEvolution::BlockDisposition BD) { |
0 |
| 13465 |
switch (BD) { |
0 |
13465 |
switch (BD) { |
0 |
| 13466 |
case ScalarEvolution::DoesNotDominateBlock: |
0 |
13466 |
case ScalarEvolution::DoesNotDominateBlock: |
0 |
| 13467 |
OS << "DoesNotDominate"; |
0 |
13467 |
OS << "DoesNotDominate"; |
0 |
| 13468 |
break; |
0 |
13468 |
break; |
0 |
| 13469 |
case ScalarEvolution::DominatesBlock: |
0 |
13469 |
case ScalarEvolution::DominatesBlock: |
0 |
| 13470 |
OS << "Dominates"; |
0 |
13470 |
OS << "Dominates"; |
0 |
| 13471 |
break; |
0 |
13471 |
break; |
0 |
| 13472 |
case ScalarEvolution::ProperlyDominatesBlock: |
0 |
13472 |
case ScalarEvolution::ProperlyDominatesBlock: |
0 |
| 13473 |
OS << "ProperlyDominates"; |
0 |
13473 |
OS << "ProperlyDominates"; |
0 |
| 13474 |
break; |
0 |
13474 |
break; |
0 |
| 13475 |
} |
--- |
13475 |
} |
--- |
| 13476 |
return OS; |
0 |
13476 |
return OS; |
0 |
| 13477 |
} |
--- |
13477 |
} |
--- |
| 13478 |
} |
--- |
13478 |
} |
--- |
| 13479 |
|
--- |
13479 |
|
--- |
| 13480 |
void ScalarEvolution::print(raw_ostream &OS) const { |
0 |
13480 |
void ScalarEvolution::print(raw_ostream &OS) const { |
0 |
| 13481 |
// ScalarEvolution's implementation of the print method is to print |
--- |
13481 |
// ScalarEvolution's implementation of the print method is to print |
--- |
| 13482 |
// out SCEV values of all instructions that are interesting. Doing |
--- |
13482 |
// out SCEV values of all instructions that are interesting. Doing |
--- |
| 13483 |
// this potentially causes it to create new SCEV objects though, |
--- |
13483 |
// this potentially causes it to create new SCEV objects though, |
--- |
| 13484 |
// which technically conflicts with the const qualifier. This isn't |
--- |
13484 |
// which technically conflicts with the const qualifier. This isn't |
--- |
| 13485 |
// observable from outside the class though, so casting away the |
--- |
13485 |
// observable from outside the class though, so casting away the |
--- |
| 13486 |
// const isn't dangerous. |
--- |
13486 |
// const isn't dangerous. |
--- |
| 13487 |
ScalarEvolution &SE = *const_cast(this); |
0 |
13487 |
ScalarEvolution &SE = *const_cast(this); |
0 |
| 13488 |
|
--- |
13488 |
|
--- |
| 13489 |
if (ClassifyExpressions) { |
0 |
13489 |
if (ClassifyExpressions) { |
0 |
| 13490 |
OS << "Classifying expressions for: "; |
0 |
13490 |
OS << "Classifying expressions for: "; |
0 |
| 13491 |
F.printAsOperand(OS, /*PrintType=*/false); |
0 |
13491 |
F.printAsOperand(OS, /*PrintType=*/false); |
0 |
| 13492 |
OS << "\n"; |
0 |
13492 |
OS << "\n"; |
0 |
| 13493 |
for (Instruction &I : instructions(F)) |
0 |
13493 |
for (Instruction &I : instructions(F)) |
0 |
| 13494 |
if (isSCEVable(I.getType()) && !isa(I)) { |
0 |
13494 |
if (isSCEVable(I.getType()) && !isa(I)) { |
0 |
| 13495 |
OS << I << '\n'; |
0 |
13495 |
OS << I << '\n'; |
0 |
| 13496 |
OS << " --> "; |
0 |
13496 |
OS << " --> "; |
0 |
| 13497 |
const SCEV *SV = SE.getSCEV(&I); |
0 |
13497 |
const SCEV *SV = SE.getSCEV(&I); |
0 |
| 13498 |
SV->print(OS); |
0 |
13498 |
SV->print(OS); |
0 |
| 13499 |
if (!isa(SV)) { |
0 |
13499 |
if (!isa(SV)) { |
0 |
| 13500 |
OS << " U: "; |
0 |
13500 |
OS << " U: "; |
0 |
| 13501 |
SE.getUnsignedRange(SV).print(OS); |
0 |
13501 |
SE.getUnsignedRange(SV).print(OS); |
0 |
| 13502 |
OS << " S: "; |
0 |
13502 |
OS << " S: "; |
0 |
| 13503 |
SE.getSignedRange(SV).print(OS); |
0 |
13503 |
SE.getSignedRange(SV).print(OS); |
0 |
| 13504 |
} |
--- |
13504 |
} |
--- |
| 13505 |
|
--- |
13505 |
|
--- |
| 13506 |
const Loop *L = LI.getLoopFor(I.getParent()); |
0 |
13506 |
const Loop *L = LI.getLoopFor(I.getParent()); |
0 |
| 13507 |
|
--- |
13507 |
|
--- |
| 13508 |
const SCEV *AtUse = SE.getSCEVAtScope(SV, L); |
0 |
13508 |
const SCEV *AtUse = SE.getSCEVAtScope(SV, L); |
0 |
| 13509 |
if (AtUse != SV) { |
0 |
13509 |
if (AtUse != SV) { |
0 |
| 13510 |
OS << " --> "; |
0 |
13510 |
OS << " --> "; |
0 |
| 13511 |
AtUse->print(OS); |
0 |
13511 |
AtUse->print(OS); |
0 |
| 13512 |
if (!isa(AtUse)) { |
0 |
13512 |
if (!isa(AtUse)) { |
0 |
| 13513 |
OS << " U: "; |
0 |
13513 |
OS << " U: "; |
0 |
| 13514 |
SE.getUnsignedRange(AtUse).print(OS); |
0 |
13514 |
SE.getUnsignedRange(AtUse).print(OS); |
0 |
| 13515 |
OS << " S: "; |
0 |
13515 |
OS << " S: "; |
0 |
| 13516 |
SE.getSignedRange(AtUse).print(OS); |
0 |
13516 |
SE.getSignedRange(AtUse).print(OS); |
0 |
| 13517 |
} |
--- |
13517 |
} |
--- |
| 13518 |
} |
--- |
13518 |
} |
--- |
| 13519 |
|
--- |
13519 |
|
--- |
| 13520 |
if (L) { |
0 |
13520 |
if (L) { |
0 |
| 13521 |
OS << "\t\t" "Exits: "; |
0 |
13521 |
OS << "\t\t" "Exits: "; |
0 |
| 13522 |
const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); |
0 |
13522 |
const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); |
0 |
| 13523 |
if (!SE.isLoopInvariant(ExitValue, L)) { |
0 |
13523 |
if (!SE.isLoopInvariant(ExitValue, L)) { |
0 |
| 13524 |
OS << "<>"; |
0 |
13524 |
OS << "<>"; |
0 |
| 13525 |
} else { |
--- |
13525 |
} else { |
--- |
| 13526 |
OS << *ExitValue; |
0 |
13526 |
OS << *ExitValue; |
0 |
| 13527 |
} |
--- |
13527 |
} |
--- |
| 13528 |
|
--- |
13528 |
|
--- |
| 13529 |
bool First = true; |
0 |
13529 |
bool First = true; |
0 |
| 13530 |
for (const auto *Iter = L; Iter; Iter = Iter->getParentLoop()) { |
0 |
13530 |
for (const auto *Iter = L; Iter; Iter = Iter->getParentLoop()) { |
0 |
| 13531 |
if (First) { |
0 |
13531 |
if (First) { |
0 |
| 13532 |
OS << "\t\t" "LoopDispositions: { "; |
0 |
13532 |
OS << "\t\t" "LoopDispositions: { "; |
0 |
| 13533 |
First = false; |
0 |
13533 |
First = false; |
0 |
| 13534 |
} else { |
--- |
13534 |
} else { |
--- |
| 13535 |
OS << ", "; |
0 |
13535 |
OS << ", "; |
0 |
| 13536 |
} |
--- |
13536 |
} |
--- |
| 13537 |
|
--- |
13537 |
|
--- |
| 13538 |
Iter->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
13538 |
Iter->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
| 13539 |
OS << ": " << SE.getLoopDisposition(SV, Iter); |
0 |
13539 |
OS << ": " << SE.getLoopDisposition(SV, Iter); |
0 |
| 13540 |
} |
--- |
13540 |
} |
--- |
| 13541 |
|
--- |
13541 |
|
--- |
| 13542 |
for (const auto *InnerL : depth_first(L)) { |
0 |
13542 |
for (const auto *InnerL : depth_first(L)) { |
0 |
| 13543 |
if (InnerL == L) |
0 |
13543 |
if (InnerL == L) |
0 |
| 13544 |
continue; |
0 |
13544 |
continue; |
0 |
| 13545 |
if (First) { |
0 |
13545 |
if (First) { |
0 |
| 13546 |
OS << "\t\t" "LoopDispositions: { "; |
0 |
13546 |
OS << "\t\t" "LoopDispositions: { "; |
0 |
| 13547 |
First = false; |
0 |
13547 |
First = false; |
0 |
| 13548 |
} else { |
--- |
13548 |
} else { |
--- |
| 13549 |
OS << ", "; |
0 |
13549 |
OS << ", "; |
0 |
| 13550 |
} |
--- |
13550 |
} |
--- |
| 13551 |
|
--- |
13551 |
|
--- |
| 13552 |
InnerL->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
13552 |
InnerL->getHeader()->printAsOperand(OS, /*PrintType=*/false); |
0 |
| 13553 |
OS << ": " << SE.getLoopDisposition(SV, InnerL); |
0 |
13553 |
OS << ": " << SE.getLoopDisposition(SV, InnerL); |
0 |
| 13554 |
} |
0 |
13554 |
} |
0 |
| 13555 |
|
--- |
13555 |
|
--- |
| 13556 |
OS << " }"; |
0 |
13556 |
OS << " }"; |
0 |
| 13557 |
} |
--- |
13557 |
} |
--- |
| 13558 |
|
--- |
13558 |
|
--- |
| 13559 |
OS << "\n"; |
0 |
13559 |
OS << "\n"; |
0 |
| 13560 |
} |
--- |
13560 |
} |
--- |
| 13561 |
} |
--- |
13561 |
} |
--- |
| 13562 |
|
--- |
13562 |
|
--- |
| 13563 |
OS << "Determining loop execution counts for: "; |
0 |
13563 |
OS << "Determining loop execution counts for: "; |
0 |
| 13564 |
F.printAsOperand(OS, /*PrintType=*/false); |
0 |
13564 |
F.printAsOperand(OS, /*PrintType=*/false); |
0 |
| 13565 |
OS << "\n"; |
0 |
13565 |
OS << "\n"; |
0 |
| 13566 |
for (Loop *I : LI) |
0 |
13566 |
for (Loop *I : LI) |
0 |
| 13567 |
PrintLoopInfo(OS, &SE, I); |
0 |
13567 |
PrintLoopInfo(OS, &SE, I); |
0 |
| 13568 |
} |
0 |
13568 |
} |
0 |
| 13569 |
|
--- |
13569 |
|
--- |
| 13570 |
ScalarEvolution::LoopDisposition |
--- |
13570 |
ScalarEvolution::LoopDisposition |
--- |
| 13571 |
ScalarEvolution::getLoopDisposition(const SCEV *S, const Loop *L) { |
0 |
13571 |
ScalarEvolution::getLoopDisposition(const SCEV *S, const Loop *L) { |
0 |
| 13572 |
auto &Values = LoopDispositions[S]; |
0 |
13572 |
auto &Values = LoopDispositions[S]; |
0 |
| 13573 |
for (auto &V : Values) { |
0 |
13573 |
for (auto &V : Values) { |
0 |
| 13574 |
if (V.getPointer() == L) |
0 |
13574 |
if (V.getPointer() == L) |
0 |
| 13575 |
return V.getInt(); |
0 |
13575 |
return V.getInt(); |
0 |
| 13576 |
} |
--- |
13576 |
} |
--- |
| 13577 |
Values.emplace_back(L, LoopVariant); |
0 |
13577 |
Values.emplace_back(L, LoopVariant); |
0 |
| 13578 |
LoopDisposition D = computeLoopDisposition(S, L); |
0 |
13578 |
LoopDisposition D = computeLoopDisposition(S, L); |
0 |
| 13579 |
auto &Values2 = LoopDispositions[S]; |
0 |
13579 |
auto &Values2 = LoopDispositions[S]; |
0 |
| 13580 |
for (auto &V : llvm::reverse(Values2)) { |
0 |
13580 |
for (auto &V : llvm::reverse(Values2)) { |
0 |
| 13581 |
if (V.getPointer() == L) { |
0 |
13581 |
if (V.getPointer() == L) { |
0 |
| 13582 |
V.setInt(D); |
0 |
13582 |
V.setInt(D); |
0 |
| 13583 |
break; |
0 |
13583 |
break; |
0 |
| 13584 |
} |
--- |
13584 |
} |
--- |
| 13585 |
} |
--- |
13585 |
} |
--- |
| 13586 |
return D; |
0 |
13586 |
return D; |
0 |
| 13587 |
} |
--- |
13587 |
} |
--- |
| 13588 |
|
--- |
13588 |
|
--- |
| 13589 |
ScalarEvolution::LoopDisposition |
--- |
13589 |
ScalarEvolution::LoopDisposition |
--- |
| 13590 |
ScalarEvolution::computeLoopDisposition(const SCEV *S, const Loop *L) { |
0 |
13590 |
ScalarEvolution::computeLoopDisposition(const SCEV *S, const Loop *L) { |
0 |
| 13591 |
switch (S->getSCEVType()) { |
0 |
13591 |
switch (S->getSCEVType()) { |
0 |
| 13592 |
case scConstant: |
0 |
13592 |
case scConstant: |
0 |
| 13593 |
case scVScale: |
--- |
13593 |
case scVScale: |
--- |
| 13594 |
return LoopInvariant; |
0 |
13594 |
return LoopInvariant; |
0 |
| 13595 |
case scAddRecExpr: { |
0 |
13595 |
case scAddRecExpr: { |
0 |
| 13596 |
const SCEVAddRecExpr *AR = cast(S); |
0 |
13596 |
const SCEVAddRecExpr *AR = cast(S); |
0 |
| 13597 |
|
--- |
13597 |
|
--- |
| 13598 |
// If L is the addrec's loop, it's computable. |
--- |
13598 |
// If L is the addrec's loop, it's computable. |
--- |
| 13599 |
if (AR->getLoop() == L) |
0 |
13599 |
if (AR->getLoop() == L) |
0 |
| 13600 |
return LoopComputable; |
0 |
13600 |
return LoopComputable; |
0 |
| 13601 |
|
--- |
13601 |
|
--- |
| 13602 |
// Add recurrences are never invariant in the function-body (null loop). |
--- |
13602 |
// Add recurrences are never invariant in the function-body (null loop). |
--- |
| 13603 |
if (!L) |
0 |
13603 |
if (!L) |
0 |
| 13604 |
return LoopVariant; |
0 |
13604 |
return LoopVariant; |
0 |
| 13605 |
|
--- |
13605 |
|
--- |
| 13606 |
// Everything that is not defined at loop entry is variant. |
--- |
13606 |
// Everything that is not defined at loop entry is variant. |
--- |
| 13607 |
if (DT.dominates(L->getHeader(), AR->getLoop()->getHeader())) |
0 |
13607 |
if (DT.dominates(L->getHeader(), AR->getLoop()->getHeader())) |
0 |
| 13608 |
return LoopVariant; |
0 |
13608 |
return LoopVariant; |
0 |
| 13609 |
assert(!L->contains(AR->getLoop()) && "Containing loop's header does not" |
0 |
13609 |
assert(!L->contains(AR->getLoop()) && "Containing loop's header does not" |
0 |
| 13610 |
" dominate the contained loop's header?"); |
--- |
13610 |
" dominate the contained loop's header?"); |
--- |
| 13611 |
|
--- |
13611 |
|
--- |
| 13612 |
// This recurrence is invariant w.r.t. L if AR's loop contains L. |
--- |
13612 |
// This recurrence is invariant w.r.t. L if AR's loop contains L. |
--- |
| 13613 |
if (AR->getLoop()->contains(L)) |
0 |
13613 |
if (AR->getLoop()->contains(L)) |
0 |
| 13614 |
return LoopInvariant; |
0 |
13614 |
return LoopInvariant; |
0 |
| 13615 |
|
--- |
13615 |
|
--- |
| 13616 |
// This recurrence is variant w.r.t. L if any of its operands |
--- |
13616 |
// This recurrence is variant w.r.t. L if any of its operands |
--- |
| 13617 |
// are variant. |
--- |
13617 |
// are variant. |
--- |
| 13618 |
for (const auto *Op : AR->operands()) |
0 |
13618 |
for (const auto *Op : AR->operands()) |
0 |
| 13619 |
if (!isLoopInvariant(Op, L)) |
0 |
13619 |
if (!isLoopInvariant(Op, L)) |
0 |
| 13620 |
return LoopVariant; |
0 |
13620 |
return LoopVariant; |
0 |
| 13621 |
|
--- |
13621 |
|
--- |
| 13622 |
// Otherwise it's loop-invariant. |
--- |
13622 |
// Otherwise it's loop-invariant. |
--- |
| 13623 |
return LoopInvariant; |
0 |
13623 |
return LoopInvariant; |
0 |
| 13624 |
} |
--- |
13624 |
} |
--- |
| 13625 |
case scTruncate: |
0 |
13625 |
case scTruncate: |
0 |
| 13626 |
case scZeroExtend: |
--- |
13626 |
case scZeroExtend: |
--- |
| 13627 |
case scSignExtend: |
--- |
13627 |
case scSignExtend: |
--- |
| 13628 |
case scPtrToInt: |
--- |
13628 |
case scPtrToInt: |
--- |
| 13629 |
case scAddExpr: |
--- |
13629 |
case scAddExpr: |
--- |
| 13630 |
case scMulExpr: |
--- |
13630 |
case scMulExpr: |
--- |
| 13631 |
case scUDivExpr: |
--- |
13631 |
case scUDivExpr: |
--- |
| 13632 |
case scUMaxExpr: |
--- |
13632 |
case scUMaxExpr: |
--- |
| 13633 |
case scSMaxExpr: |
--- |
13633 |
case scSMaxExpr: |
--- |
| 13634 |
case scUMinExpr: |
--- |
13634 |
case scUMinExpr: |
--- |
| 13635 |
case scSMinExpr: |
--- |
13635 |
case scSMinExpr: |
--- |
| 13636 |
case scSequentialUMinExpr: { |
--- |
13636 |
case scSequentialUMinExpr: { |
--- |
| 13637 |
bool HasVarying = false; |
0 |
13637 |
bool HasVarying = false; |
0 |
| 13638 |
for (const auto *Op : S->operands()) { |
0 |
13638 |
for (const auto *Op : S->operands()) { |
0 |
| 13639 |
LoopDisposition D = getLoopDisposition(Op, L); |
0 |
13639 |
LoopDisposition D = getLoopDisposition(Op, L); |
0 |
| 13640 |
if (D == LoopVariant) |
0 |
13640 |
if (D == LoopVariant) |
0 |
| 13641 |
return LoopVariant; |
0 |
13641 |
return LoopVariant; |
0 |
| 13642 |
if (D == LoopComputable) |
0 |
13642 |
if (D == LoopComputable) |
0 |
| 13643 |
HasVarying = true; |
0 |
13643 |
HasVarying = true; |
0 |
| 13644 |
} |
--- |
13644 |
} |
--- |
| 13645 |
return HasVarying ? LoopComputable : LoopInvariant; |
0 |
13645 |
return HasVarying ? LoopComputable : LoopInvariant; |
0 |
| 13646 |
} |
--- |
13646 |
} |
--- |
| 13647 |
case scUnknown: |
0 |
13647 |
case scUnknown: |
0 |
| 13648 |
// All non-instruction values are loop invariant. All instructions are loop |
--- |
13648 |
// All non-instruction values are loop invariant. All instructions are loop |
--- |
| 13649 |
// invariant if they are not contained in the specified loop. |
--- |
13649 |
// invariant if they are not contained in the specified loop. |
--- |
| 13650 |
// Instructions are never considered invariant in the function body |
--- |
13650 |
// Instructions are never considered invariant in the function body |
--- |
| 13651 |
// (null loop) because they are defined within the "loop". |
--- |
13651 |
// (null loop) because they are defined within the "loop". |
--- |
| 13652 |
if (auto *I = dyn_cast(cast(S)->getValue())) |
0 |
13652 |
if (auto *I = dyn_cast(cast(S)->getValue())) |
0 |
| 13653 |
return (L && !L->contains(I)) ? LoopInvariant : LoopVariant; |
0 |
13653 |
return (L && !L->contains(I)) ? LoopInvariant : LoopVariant; |
0 |
| 13654 |
return LoopInvariant; |
0 |
13654 |
return LoopInvariant; |
0 |
| 13655 |
case scCouldNotCompute: |
0 |
13655 |
case scCouldNotCompute: |
0 |
| 13656 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
13656 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
| 13657 |
} |
--- |
13657 |
} |
--- |
| 13658 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
13658 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
| 13659 |
} |
--- |
13659 |
} |
--- |
| 13660 |
|
--- |
13660 |
|
--- |
| 13661 |
bool ScalarEvolution::isLoopInvariant(const SCEV *S, const Loop *L) { |
0 |
13661 |
bool ScalarEvolution::isLoopInvariant(const SCEV *S, const Loop *L) { |
0 |
| 13662 |
return getLoopDisposition(S, L) == LoopInvariant; |
0 |
13662 |
return getLoopDisposition(S, L) == LoopInvariant; |
0 |
| 13663 |
} |
--- |
13663 |
} |
--- |
| 13664 |
|
--- |
13664 |
|
--- |
| 13665 |
bool ScalarEvolution::hasComputableLoopEvolution(const SCEV *S, const Loop *L) { |
0 |
13665 |
bool ScalarEvolution::hasComputableLoopEvolution(const SCEV *S, const Loop *L) { |
0 |
| 13666 |
return getLoopDisposition(S, L) == LoopComputable; |
0 |
13666 |
return getLoopDisposition(S, L) == LoopComputable; |
0 |
| 13667 |
} |
--- |
13667 |
} |
--- |
| 13668 |
|
--- |
13668 |
|
--- |
| 13669 |
ScalarEvolution::BlockDisposition |
--- |
13669 |
ScalarEvolution::BlockDisposition |
--- |
| 13670 |
ScalarEvolution::getBlockDisposition(const SCEV *S, const BasicBlock *BB) { |
0 |
13670 |
ScalarEvolution::getBlockDisposition(const SCEV *S, const BasicBlock *BB) { |
0 |
| 13671 |
auto &Values = BlockDispositions[S]; |
0 |
13671 |
auto &Values = BlockDispositions[S]; |
0 |
| 13672 |
for (auto &V : Values) { |
0 |
13672 |
for (auto &V : Values) { |
0 |
| 13673 |
if (V.getPointer() == BB) |
0 |
13673 |
if (V.getPointer() == BB) |
0 |
| 13674 |
return V.getInt(); |
0 |
13674 |
return V.getInt(); |
0 |
| 13675 |
} |
--- |
13675 |
} |
--- |
| 13676 |
Values.emplace_back(BB, DoesNotDominateBlock); |
0 |
13676 |
Values.emplace_back(BB, DoesNotDominateBlock); |
0 |
| 13677 |
BlockDisposition D = computeBlockDisposition(S, BB); |
0 |
13677 |
BlockDisposition D = computeBlockDisposition(S, BB); |
0 |
| 13678 |
auto &Values2 = BlockDispositions[S]; |
0 |
13678 |
auto &Values2 = BlockDispositions[S]; |
0 |
| 13679 |
for (auto &V : llvm::reverse(Values2)) { |
0 |
13679 |
for (auto &V : llvm::reverse(Values2)) { |
0 |
| 13680 |
if (V.getPointer() == BB) { |
0 |
13680 |
if (V.getPointer() == BB) { |
0 |
| 13681 |
V.setInt(D); |
0 |
13681 |
V.setInt(D); |
0 |
| 13682 |
break; |
0 |
13682 |
break; |
0 |
| 13683 |
} |
--- |
13683 |
} |
--- |
| 13684 |
} |
--- |
13684 |
} |
--- |
| 13685 |
return D; |
0 |
13685 |
return D; |
0 |
| 13686 |
} |
--- |
13686 |
} |
--- |
| 13687 |
|
--- |
13687 |
|
--- |
| 13688 |
ScalarEvolution::BlockDisposition |
--- |
13688 |
ScalarEvolution::BlockDisposition |
--- |
| 13689 |
ScalarEvolution::computeBlockDisposition(const SCEV *S, const BasicBlock *BB) { |
0 |
13689 |
ScalarEvolution::computeBlockDisposition(const SCEV *S, const BasicBlock *BB) { |
0 |
| 13690 |
switch (S->getSCEVType()) { |
0 |
13690 |
switch (S->getSCEVType()) { |
0 |
| 13691 |
case scConstant: |
0 |
13691 |
case scConstant: |
0 |
| 13692 |
case scVScale: |
--- |
13692 |
case scVScale: |
--- |
| 13693 |
return ProperlyDominatesBlock; |
0 |
13693 |
return ProperlyDominatesBlock; |
0 |
| 13694 |
case scAddRecExpr: { |
0 |
13694 |
case scAddRecExpr: { |
0 |
| 13695 |
// This uses a "dominates" query instead of "properly dominates" query |
--- |
13695 |
// This uses a "dominates" query instead of "properly dominates" query |
--- |
| 13696 |
// to test for proper dominance too, because the instruction which |
--- |
13696 |
// to test for proper dominance too, because the instruction which |
--- |
| 13697 |
// produces the addrec's value is a PHI, and a PHI effectively properly |
--- |
13697 |
// produces the addrec's value is a PHI, and a PHI effectively properly |
--- |
| 13698 |
// dominates its entire containing block. |
--- |
13698 |
// dominates its entire containing block. |
--- |
| 13699 |
const SCEVAddRecExpr *AR = cast(S); |
0 |
13699 |
const SCEVAddRecExpr *AR = cast(S); |
0 |
| 13700 |
if (!DT.dominates(AR->getLoop()->getHeader(), BB)) |
0 |
13700 |
if (!DT.dominates(AR->getLoop()->getHeader(), BB)) |
0 |
| 13701 |
return DoesNotDominateBlock; |
0 |
13701 |
return DoesNotDominateBlock; |
0 |
| 13702 |
|
--- |
13702 |
|
--- |
| 13703 |
// Fall through into SCEVNAryExpr handling. |
--- |
13703 |
// Fall through into SCEVNAryExpr handling. |
--- |
| 13704 |
[[fallthrough]]; |
--- |
13704 |
[[fallthrough]]; |
--- |
| 13705 |
} |
--- |
13705 |
} |
--- |
| 13706 |
case scTruncate: |
--- |
13706 |
case scTruncate: |
--- |
| 13707 |
case scZeroExtend: |
--- |
13707 |
case scZeroExtend: |
--- |
| 13708 |
case scSignExtend: |
--- |
13708 |
case scSignExtend: |
--- |
| 13709 |
case scPtrToInt: |
--- |
13709 |
case scPtrToInt: |
--- |
| 13710 |
case scAddExpr: |
--- |
13710 |
case scAddExpr: |
--- |
| 13711 |
case scMulExpr: |
--- |
13711 |
case scMulExpr: |
--- |
| 13712 |
case scUDivExpr: |
--- |
13712 |
case scUDivExpr: |
--- |
| 13713 |
case scUMaxExpr: |
--- |
13713 |
case scUMaxExpr: |
--- |
| 13714 |
case scSMaxExpr: |
--- |
13714 |
case scSMaxExpr: |
--- |
| 13715 |
case scUMinExpr: |
--- |
13715 |
case scUMinExpr: |
--- |
| 13716 |
case scSMinExpr: |
--- |
13716 |
case scSMinExpr: |
--- |
| 13717 |
case scSequentialUMinExpr: { |
--- |
13717 |
case scSequentialUMinExpr: { |
--- |
| 13718 |
bool Proper = true; |
0 |
13718 |
bool Proper = true; |
0 |
| 13719 |
for (const SCEV *NAryOp : S->operands()) { |
0 |
13719 |
for (const SCEV *NAryOp : S->operands()) { |
0 |
| 13720 |
BlockDisposition D = getBlockDisposition(NAryOp, BB); |
0 |
13720 |
BlockDisposition D = getBlockDisposition(NAryOp, BB); |
0 |
| 13721 |
if (D == DoesNotDominateBlock) |
0 |
13721 |
if (D == DoesNotDominateBlock) |
0 |
| 13722 |
return DoesNotDominateBlock; |
0 |
13722 |
return DoesNotDominateBlock; |
0 |
| 13723 |
if (D == DominatesBlock) |
0 |
13723 |
if (D == DominatesBlock) |
0 |
| 13724 |
Proper = false; |
0 |
13724 |
Proper = false; |
0 |
| 13725 |
} |
--- |
13725 |
} |
--- |
| 13726 |
return Proper ? ProperlyDominatesBlock : DominatesBlock; |
0 |
13726 |
return Proper ? ProperlyDominatesBlock : DominatesBlock; |
0 |
| 13727 |
} |
--- |
13727 |
} |
--- |
| 13728 |
case scUnknown: |
0 |
13728 |
case scUnknown: |
0 |
| 13729 |
if (Instruction *I = |
0 |
13729 |
if (Instruction *I = |
0 |
| 13730 |
dyn_cast(cast(S)->getValue())) { |
0 |
13730 |
dyn_cast(cast(S)->getValue())) { |
0 |
| 13731 |
if (I->getParent() == BB) |
0 |
13731 |
if (I->getParent() == BB) |
0 |
| 13732 |
return DominatesBlock; |
0 |
13732 |
return DominatesBlock; |
0 |
| 13733 |
if (DT.properlyDominates(I->getParent(), BB)) |
0 |
13733 |
if (DT.properlyDominates(I->getParent(), BB)) |
0 |
| 13734 |
return ProperlyDominatesBlock; |
0 |
13734 |
return ProperlyDominatesBlock; |
0 |
| 13735 |
return DoesNotDominateBlock; |
0 |
13735 |
return DoesNotDominateBlock; |
0 |
| 13736 |
} |
--- |
13736 |
} |
--- |
| 13737 |
return ProperlyDominatesBlock; |
0 |
13737 |
return ProperlyDominatesBlock; |
0 |
| 13738 |
case scCouldNotCompute: |
0 |
13738 |
case scCouldNotCompute: |
0 |
| 13739 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
13739 |
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
0 |
| 13740 |
} |
--- |
13740 |
} |
--- |
| 13741 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
13741 |
llvm_unreachable("Unknown SCEV kind!"); |
0 |
| 13742 |
} |
--- |
13742 |
} |
--- |
| 13743 |
|
--- |
13743 |
|
--- |
| 13744 |
bool ScalarEvolution::dominates(const SCEV *S, const BasicBlock *BB) { |
0 |
13744 |
bool ScalarEvolution::dominates(const SCEV *S, const BasicBlock *BB) { |
0 |
| 13745 |
return getBlockDisposition(S, BB) >= DominatesBlock; |
0 |
13745 |
return getBlockDisposition(S, BB) >= DominatesBlock; |
0 |
| 13746 |
} |
--- |
13746 |
} |
--- |
| 13747 |
|
--- |
13747 |
|
--- |
| 13748 |
bool ScalarEvolution::properlyDominates(const SCEV *S, const BasicBlock *BB) { |
0 |
13748 |
bool ScalarEvolution::properlyDominates(const SCEV *S, const BasicBlock *BB) { |
0 |
| 13749 |
return getBlockDisposition(S, BB) == ProperlyDominatesBlock; |
0 |
13749 |
return getBlockDisposition(S, BB) == ProperlyDominatesBlock; |
0 |
| 13750 |
} |
--- |
13750 |
} |
--- |
| 13751 |
|
--- |
13751 |
|
--- |
| 13752 |
bool ScalarEvolution::hasOperand(const SCEV *S, const SCEV *Op) const { |
0 |
13752 |
bool ScalarEvolution::hasOperand(const SCEV *S, const SCEV *Op) const { |
0 |
| 13753 |
return SCEVExprContains(S, [&](const SCEV *Expr) { return Expr == Op; }); |
0 |
13753 |
return SCEVExprContains(S, [&](const SCEV *Expr) { return Expr == Op; }); |
0 |
| 13754 |
} |
--- |
13754 |
} |
--- |
| 13755 |
|
--- |
13755 |
|
--- |
| 13756 |
void ScalarEvolution::forgetBackedgeTakenCounts(const Loop *L, |
0 |
13756 |
void ScalarEvolution::forgetBackedgeTakenCounts(const Loop *L, |
0 |
| 13757 |
bool Predicated) { |
--- |
13757 |
bool Predicated) { |
--- |
| 13758 |
auto &BECounts = |
0 |
13758 |
auto &BECounts = |
0 |
| 13759 |
Predicated ? PredicatedBackedgeTakenCounts : BackedgeTakenCounts; |
--- |
13759 |
Predicated ? PredicatedBackedgeTakenCounts : BackedgeTakenCounts; |
--- |
| 13760 |
auto It = BECounts.find(L); |
0 |
13760 |
auto It = BECounts.find(L); |
0 |
| 13761 |
if (It != BECounts.end()) { |
0 |
13761 |
if (It != BECounts.end()) { |
0 |
| 13762 |
for (const ExitNotTakenInfo &ENT : It->second.ExitNotTaken) { |
0 |
13762 |
for (const ExitNotTakenInfo &ENT : It->second.ExitNotTaken) { |
0 |
| 13763 |
for (const SCEV *S : {ENT.ExactNotTaken, ENT.SymbolicMaxNotTaken}) { |
0 |
13763 |
for (const SCEV *S : {ENT.ExactNotTaken, ENT.SymbolicMaxNotTaken}) { |
0 |
| 13764 |
if (!isa(S)) { |
0 |
13764 |
if (!isa(S)) { |
0 |
| 13765 |
auto UserIt = BECountUsers.find(S); |
0 |
13765 |
auto UserIt = BECountUsers.find(S); |
0 |
| 13766 |
assert(UserIt != BECountUsers.end()); |
0 |
13766 |
assert(UserIt != BECountUsers.end()); |
0 |
| 13767 |
UserIt->second.erase({L, Predicated}); |
0 |
13767 |
UserIt->second.erase({L, Predicated}); |
0 |
| 13768 |
} |
--- |
13768 |
} |
--- |
| 13769 |
} |
--- |
13769 |
} |
--- |
| 13770 |
} |
--- |
13770 |
} |
--- |
| 13771 |
BECounts.erase(It); |
0 |
13771 |
BECounts.erase(It); |
0 |
| 13772 |
} |
--- |
13772 |
} |
--- |
| 13773 |
} |
0 |
13773 |
} |
0 |
| 13774 |
|
--- |
13774 |
|
--- |
| 13775 |
void ScalarEvolution::forgetMemoizedResults(ArrayRef SCEVs) { |
0 |
13775 |
void ScalarEvolution::forgetMemoizedResults(ArrayRef SCEVs) { |
0 |
| 13776 |
SmallPtrSet ToForget(SCEVs.begin(), SCEVs.end()); |
0 |
13776 |
SmallPtrSet ToForget(SCEVs.begin(), SCEVs.end()); |
0 |
| 13777 |
SmallVector Worklist(ToForget.begin(), ToForget.end()); |
0 |
13777 |
SmallVector Worklist(ToForget.begin(), ToForget.end()); |
0 |
| 13778 |
|
--- |
13778 |
|
--- |
| 13779 |
while (!Worklist.empty()) { |
0 |
13779 |
while (!Worklist.empty()) { |
0 |
| 13780 |
const SCEV *Curr = Worklist.pop_back_val(); |
0 |
13780 |
const SCEV *Curr = Worklist.pop_back_val(); |
0 |
| 13781 |
auto Users = SCEVUsers.find(Curr); |
0 |
13781 |
auto Users = SCEVUsers.find(Curr); |
0 |
| 13782 |
if (Users != SCEVUsers.end()) |
0 |
13782 |
if (Users != SCEVUsers.end()) |
0 |
| 13783 |
for (const auto *User : Users->second) |
0 |
13783 |
for (const auto *User : Users->second) |
0 |
| 13784 |
if (ToForget.insert(User).second) |
0 |
13784 |
if (ToForget.insert(User).second) |
0 |
| 13785 |
Worklist.push_back(User); |
0 |
13785 |
Worklist.push_back(User); |
0 |
| 13786 |
} |
--- |
13786 |
} |
--- |
| 13787 |
|
--- |
13787 |
|
--- |
| 13788 |
for (const auto *S : ToForget) |
0 |
13788 |
for (const auto *S : ToForget) |
0 |
| 13789 |
forgetMemoizedResultsImpl(S); |
0 |
13789 |
forgetMemoizedResultsImpl(S); |
0 |
| 13790 |
|
--- |
13790 |
|
--- |
| 13791 |
for (auto I = PredicatedSCEVRewrites.begin(); |
0 |
13791 |
for (auto I = PredicatedSCEVRewrites.begin(); |
0 |
| 13792 |
I != PredicatedSCEVRewrites.end();) { |
0 |
13792 |
I != PredicatedSCEVRewrites.end();) { |
0 |
| 13793 |
std::pair Entry = I->first; |
0 |
13793 |
std::pair Entry = I->first; |
0 |
| 13794 |
if (ToForget.count(Entry.first)) |
0 |
13794 |
if (ToForget.count(Entry.first)) |
0 |
| 13795 |
PredicatedSCEVRewrites.erase(I++); |
0 |
13795 |
PredicatedSCEVRewrites.erase(I++); |
0 |
| 13796 |
else |
--- |
13796 |
else |
--- |
| 13797 |
++I; |
0 |
13797 |
++I; |
0 |
| 13798 |
} |
--- |
13798 |
} |
--- |
| 13799 |
} |
0 |
13799 |
} |
0 |
| 13800 |
|
--- |
13800 |
|
--- |
| 13801 |
void ScalarEvolution::forgetMemoizedResultsImpl(const SCEV *S) { |
0 |
13801 |
void ScalarEvolution::forgetMemoizedResultsImpl(const SCEV *S) { |
0 |
| 13802 |
LoopDispositions.erase(S); |
0 |
13802 |
LoopDispositions.erase(S); |
0 |
| 13803 |
BlockDispositions.erase(S); |
0 |
13803 |
BlockDispositions.erase(S); |
0 |
| 13804 |
UnsignedRanges.erase(S); |
0 |
13804 |
UnsignedRanges.erase(S); |
0 |
| 13805 |
SignedRanges.erase(S); |
0 |
13805 |
SignedRanges.erase(S); |
0 |
| 13806 |
HasRecMap.erase(S); |
0 |
13806 |
HasRecMap.erase(S); |
0 |
| 13807 |
ConstantMultipleCache.erase(S); |
0 |
13807 |
ConstantMultipleCache.erase(S); |
0 |
| 13808 |
|
--- |
13808 |
|
--- |
| 13809 |
if (auto *AR = dyn_cast(S)) { |
0 |
13809 |
if (auto *AR = dyn_cast(S)) { |
0 |
| 13810 |
UnsignedWrapViaInductionTried.erase(AR); |
0 |
13810 |
UnsignedWrapViaInductionTried.erase(AR); |
0 |
| 13811 |
SignedWrapViaInductionTried.erase(AR); |
0 |
13811 |
SignedWrapViaInductionTried.erase(AR); |
0 |
| 13812 |
} |
--- |
13812 |
} |
--- |
| 13813 |
|
--- |
13813 |
|
--- |
| 13814 |
auto ExprIt = ExprValueMap.find(S); |
0 |
13814 |
auto ExprIt = ExprValueMap.find(S); |
0 |
| 13815 |
if (ExprIt != ExprValueMap.end()) { |
0 |
13815 |
if (ExprIt != ExprValueMap.end()) { |
0 |
| 13816 |
for (Value *V : ExprIt->second) { |
0 |
13816 |
for (Value *V : ExprIt->second) { |
0 |
| 13817 |
auto ValueIt = ValueExprMap.find_as(V); |
0 |
13817 |
auto ValueIt = ValueExprMap.find_as(V); |
0 |
| 13818 |
if (ValueIt != ValueExprMap.end()) |
0 |
13818 |
if (ValueIt != ValueExprMap.end()) |
0 |
| 13819 |
ValueExprMap.erase(ValueIt); |
0 |
13819 |
ValueExprMap.erase(ValueIt); |
0 |
| 13820 |
} |
--- |
13820 |
} |
--- |
| 13821 |
ExprValueMap.erase(ExprIt); |
0 |
13821 |
ExprValueMap.erase(ExprIt); |
0 |
| 13822 |
} |
--- |
13822 |
} |
--- |
| 13823 |
|
--- |
13823 |
|
--- |
| 13824 |
auto ScopeIt = ValuesAtScopes.find(S); |
0 |
13824 |
auto ScopeIt = ValuesAtScopes.find(S); |
0 |
| 13825 |
if (ScopeIt != ValuesAtScopes.end()) { |
0 |
13825 |
if (ScopeIt != ValuesAtScopes.end()) { |
0 |
| 13826 |
for (const auto &Pair : ScopeIt->second) |
0 |
13826 |
for (const auto &Pair : ScopeIt->second) |
0 |
| 13827 |
if (!isa_and_nonnull(Pair.second)) |
0 |
13827 |
if (!isa_and_nonnull(Pair.second)) |
0 |
| 13828 |
erase_value(ValuesAtScopesUsers[Pair.second], |
0 |
13828 |
erase_value(ValuesAtScopesUsers[Pair.second], |
0 |
| 13829 |
std::make_pair(Pair.first, S)); |
0 |
13829 |
std::make_pair(Pair.first, S)); |
0 |
| 13830 |
ValuesAtScopes.erase(ScopeIt); |
0 |
13830 |
ValuesAtScopes.erase(ScopeIt); |
0 |
| 13831 |
} |
--- |
13831 |
} |
--- |
| 13832 |
|
--- |
13832 |
|
--- |
| 13833 |
auto ScopeUserIt = ValuesAtScopesUsers.find(S); |
0 |
13833 |
auto ScopeUserIt = ValuesAtScopesUsers.find(S); |
0 |
| 13834 |
if (ScopeUserIt != ValuesAtScopesUsers.end()) { |
0 |
13834 |
if (ScopeUserIt != ValuesAtScopesUsers.end()) { |
0 |
| 13835 |
for (const auto &Pair : ScopeUserIt->second) |
0 |
13835 |
for (const auto &Pair : ScopeUserIt->second) |
0 |
| 13836 |
erase_value(ValuesAtScopes[Pair.second], std::make_pair(Pair.first, S)); |
0 |
13836 |
erase_value(ValuesAtScopes[Pair.second], std::make_pair(Pair.first, S)); |
0 |
| 13837 |
ValuesAtScopesUsers.erase(ScopeUserIt); |
0 |
13837 |
ValuesAtScopesUsers.erase(ScopeUserIt); |
0 |
| 13838 |
} |
--- |
13838 |
} |
--- |
| 13839 |
|
--- |
13839 |
|
--- |
| 13840 |
auto BEUsersIt = BECountUsers.find(S); |
0 |
13840 |
auto BEUsersIt = BECountUsers.find(S); |
0 |
| 13841 |
if (BEUsersIt != BECountUsers.end()) { |
0 |
13841 |
if (BEUsersIt != BECountUsers.end()) { |
0 |
| 13842 |
// Work on a copy, as forgetBackedgeTakenCounts() will modify the original. |
--- |
13842 |
// Work on a copy, as forgetBackedgeTakenCounts() will modify the original. |
--- |
| 13843 |
auto Copy = BEUsersIt->second; |
0 |
13843 |
auto Copy = BEUsersIt->second; |
0 |
| 13844 |
for (const auto &Pair : Copy) |
0 |
13844 |
for (const auto &Pair : Copy) |
0 |
| 13845 |
forgetBackedgeTakenCounts(Pair.getPointer(), Pair.getInt()); |
0 |
13845 |
forgetBackedgeTakenCounts(Pair.getPointer(), Pair.getInt()); |
0 |
| 13846 |
BECountUsers.erase(BEUsersIt); |
0 |
13846 |
BECountUsers.erase(BEUsersIt); |
0 |
| 13847 |
} |
0 |
13847 |
} |
0 |
| 13848 |
|
--- |
13848 |
|
--- |
| 13849 |
auto FoldUser = FoldCacheUser.find(S); |
0 |
13849 |
auto FoldUser = FoldCacheUser.find(S); |
0 |
| 13850 |
if (FoldUser != FoldCacheUser.end()) |
0 |
13850 |
if (FoldUser != FoldCacheUser.end()) |
0 |
| 13851 |
for (auto &KV : FoldUser->second) |
0 |
13851 |
for (auto &KV : FoldUser->second) |
0 |
| 13852 |
FoldCache.erase(KV); |
0 |
13852 |
FoldCache.erase(KV); |
0 |
| 13853 |
FoldCacheUser.erase(S); |
0 |
13853 |
FoldCacheUser.erase(S); |
0 |
| 13854 |
} |
0 |
13854 |
} |
0 |
| 13855 |
|
--- |
13855 |
|
--- |
| 13856 |
void |
--- |
13856 |
void |
--- |
| 13857 |
ScalarEvolution::getUsedLoops(const SCEV *S, |
0 |
13857 |
ScalarEvolution::getUsedLoops(const SCEV *S, |
0 |
| 13858 |
SmallPtrSetImpl &LoopsUsed) { |
--- |
13858 |
SmallPtrSetImpl &LoopsUsed) { |
--- |
| 13859 |
struct FindUsedLoops { |
--- |
13859 |
struct FindUsedLoops { |
--- |
| 13860 |
FindUsedLoops(SmallPtrSetImpl &LoopsUsed) |
0 |
13860 |
FindUsedLoops(SmallPtrSetImpl &LoopsUsed) |
0 |
| 13861 |
: LoopsUsed(LoopsUsed) {} |
0 |
13861 |
: LoopsUsed(LoopsUsed) {} |
0 |
| 13862 |
SmallPtrSetImpl &LoopsUsed; |
--- |
13862 |
SmallPtrSetImpl &LoopsUsed; |
--- |
| 13863 |
bool follow(const SCEV *S) { |
0 |
13863 |
bool follow(const SCEV *S) { |
0 |
| 13864 |
if (auto *AR = dyn_cast(S)) |
0 |
13864 |
if (auto *AR = dyn_cast(S)) |
0 |
| 13865 |
LoopsUsed.insert(AR->getLoop()); |
0 |
13865 |
LoopsUsed.insert(AR->getLoop()); |
0 |
| 13866 |
return true; |
0 |
13866 |
return true; |
0 |
| 13867 |
} |
--- |
13867 |
} |
--- |
| 13868 |
|
--- |
13868 |
|
--- |
| 13869 |
bool isDone() const { return false; } |
0 |
13869 |
bool isDone() const { return false; } |
0 |
| 13870 |
}; |
--- |
13870 |
}; |
--- |
| 13871 |
|
--- |
13871 |
|
--- |
| 13872 |
FindUsedLoops F(LoopsUsed); |
0 |
13872 |
FindUsedLoops F(LoopsUsed); |
0 |
| 13873 |
SCEVTraversal(F).visitAll(S); |
0 |
13873 |
SCEVTraversal(F).visitAll(S); |
0 |
| 13874 |
} |
0 |
13874 |
} |
0 |
| 13875 |
|
--- |
13875 |
|
--- |
| 13876 |
void ScalarEvolution::getReachableBlocks( |
0 |
13876 |
void ScalarEvolution::getReachableBlocks( |
0 |
| 13877 |
SmallPtrSetImpl &Reachable, Function &F) { |
--- |
13877 |
SmallPtrSetImpl &Reachable, Function &F) { |
--- |
| 13878 |
SmallVector Worklist; |
0 |
13878 |
SmallVector Worklist; |
0 |
| 13879 |
Worklist.push_back(&F.getEntryBlock()); |
0 |
13879 |
Worklist.push_back(&F.getEntryBlock()); |
0 |
| 13880 |
while (!Worklist.empty()) { |
0 |
13880 |
while (!Worklist.empty()) { |
0 |
| 13881 |
BasicBlock *BB = Worklist.pop_back_val(); |
0 |
13881 |
BasicBlock *BB = Worklist.pop_back_val(); |
0 |
| 13882 |
if (!Reachable.insert(BB).second) |
0 |
13882 |
if (!Reachable.insert(BB).second) |
0 |
| 13883 |
continue; |
0 |
13883 |
continue; |
0 |
| 13884 |
|
--- |
13884 |
|
--- |
| 13885 |
Value *Cond; |
--- |
13885 |
Value *Cond; |
--- |
| 13886 |
BasicBlock *TrueBB, *FalseBB; |
--- |
13886 |
BasicBlock *TrueBB, *FalseBB; |
--- |
| 13887 |
if (match(BB->getTerminator(), m_Br(m_Value(Cond), m_BasicBlock(TrueBB), |
0 |
13887 |
if (match(BB->getTerminator(), m_Br(m_Value(Cond), m_BasicBlock(TrueBB), |
0 |
| 13888 |
m_BasicBlock(FalseBB)))) { |
0 |
13888 |
m_BasicBlock(FalseBB)))) { |
0 |
| 13889 |
if (auto *C = dyn_cast(Cond)) { |
0 |
13889 |
if (auto *C = dyn_cast(Cond)) { |
0 |
| 13890 |
Worklist.push_back(C->isOne() ? TrueBB : FalseBB); |
0 |
13890 |
Worklist.push_back(C->isOne() ? TrueBB : FalseBB); |
0 |
| 13891 |
continue; |
0 |
13891 |
continue; |
0 |
| 13892 |
} |
--- |
13892 |
} |
--- |
| 13893 |
|
--- |
13893 |
|
--- |
| 13894 |
if (auto *Cmp = dyn_cast(Cond)) { |
0 |
13894 |
if (auto *Cmp = dyn_cast(Cond)) { |
0 |
| 13895 |
const SCEV *L = getSCEV(Cmp->getOperand(0)); |
0 |
13895 |
const SCEV *L = getSCEV(Cmp->getOperand(0)); |
0 |
| 13896 |
const SCEV *R = getSCEV(Cmp->getOperand(1)); |
0 |
13896 |
const SCEV *R = getSCEV(Cmp->getOperand(1)); |
0 |
| 13897 |
if (isKnownPredicateViaConstantRanges(Cmp->getPredicate(), L, R)) { |
0 |
13897 |
if (isKnownPredicateViaConstantRanges(Cmp->getPredicate(), L, R)) { |
0 |
| 13898 |
Worklist.push_back(TrueBB); |
0 |
13898 |
Worklist.push_back(TrueBB); |
0 |
| 13899 |
continue; |
0 |
13899 |
continue; |
0 |
| 13900 |
} |
--- |
13900 |
} |
--- |
| 13901 |
if (isKnownPredicateViaConstantRanges(Cmp->getInversePredicate(), L, |
0 |
13901 |
if (isKnownPredicateViaConstantRanges(Cmp->getInversePredicate(), L, |
0 |
| 13902 |
R)) { |
--- |
13902 |
R)) { |
--- |
| 13903 |
Worklist.push_back(FalseBB); |
0 |
13903 |
Worklist.push_back(FalseBB); |
0 |
| 13904 |
continue; |
0 |
13904 |
continue; |
0 |
| 13905 |
} |
--- |
13905 |
} |
--- |
| 13906 |
} |
--- |
13906 |
} |
--- |
| 13907 |
} |
--- |
13907 |
} |
--- |
| 13908 |
|
--- |
13908 |
|
--- |
| 13909 |
append_range(Worklist, successors(BB)); |
0 |
13909 |
append_range(Worklist, successors(BB)); |
0 |
| 13910 |
} |
--- |
13910 |
} |
--- |
| 13911 |
} |
0 |
13911 |
} |
0 |
| 13912 |
|
--- |
13912 |
|
--- |
| 13913 |
void ScalarEvolution::verify() const { |
0 |
13913 |
void ScalarEvolution::verify() const { |
0 |
| 13914 |
ScalarEvolution &SE = *const_cast(this); |
0 |
13914 |
ScalarEvolution &SE = *const_cast(this); |
0 |
| 13915 |
ScalarEvolution SE2(F, TLI, AC, DT, LI); |
0 |
13915 |
ScalarEvolution SE2(F, TLI, AC, DT, LI); |
0 |
| 13916 |
|
--- |
13916 |
|
--- |
| 13917 |
SmallVector LoopStack(LI.begin(), LI.end()); |
0 |
13917 |
SmallVector LoopStack(LI.begin(), LI.end()); |
0 |
| 13918 |
|
--- |
13918 |
|
--- |
| 13919 |
// Map's SCEV expressions from one ScalarEvolution "universe" to another. |
--- |
13919 |
// Map's SCEV expressions from one ScalarEvolution "universe" to another. |
--- |
| 13920 |
struct SCEVMapper : public SCEVRewriteVisitor { |
--- |
13920 |
struct SCEVMapper : public SCEVRewriteVisitor { |
--- |
| 13921 |
SCEVMapper(ScalarEvolution &SE) : SCEVRewriteVisitor(SE) {} |
0 |
13921 |
SCEVMapper(ScalarEvolution &SE) : SCEVRewriteVisitor(SE) {} |
0 |
| 13922 |
|
--- |
13922 |
|
--- |
| 13923 |
const SCEV *visitConstant(const SCEVConstant *Constant) { |
0 |
13923 |
const SCEV *visitConstant(const SCEVConstant *Constant) { |
0 |
| 13924 |
return SE.getConstant(Constant->getAPInt()); |
0 |
13924 |
return SE.getConstant(Constant->getAPInt()); |
0 |
| 13925 |
} |
--- |
13925 |
} |
--- |
| 13926 |
|
--- |
13926 |
|
--- |
| 13927 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
13927 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
| 13928 |
return SE.getUnknown(Expr->getValue()); |
0 |
13928 |
return SE.getUnknown(Expr->getValue()); |
0 |
| 13929 |
} |
--- |
13929 |
} |
--- |
| 13930 |
|
--- |
13930 |
|
--- |
| 13931 |
const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) { |
0 |
13931 |
const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) { |
0 |
| 13932 |
return SE.getCouldNotCompute(); |
0 |
13932 |
return SE.getCouldNotCompute(); |
0 |
| 13933 |
} |
--- |
13933 |
} |
--- |
| 13934 |
}; |
--- |
13934 |
}; |
--- |
| 13935 |
|
--- |
13935 |
|
--- |
| 13936 |
SCEVMapper SCM(SE2); |
0 |
13936 |
SCEVMapper SCM(SE2); |
0 |
| 13937 |
SmallPtrSet ReachableBlocks; |
0 |
13937 |
SmallPtrSet ReachableBlocks; |
0 |
| 13938 |
SE2.getReachableBlocks(ReachableBlocks, F); |
0 |
13938 |
SE2.getReachableBlocks(ReachableBlocks, F); |
0 |
| 13939 |
|
--- |
13939 |
|
--- |
| 13940 |
auto GetDelta = [&](const SCEV *Old, const SCEV *New) -> const SCEV * { |
0 |
13940 |
auto GetDelta = [&](const SCEV *Old, const SCEV *New) -> const SCEV * { |
0 |
| 13941 |
if (containsUndefs(Old) || containsUndefs(New)) { |
0 |
13941 |
if (containsUndefs(Old) || containsUndefs(New)) { |
0 |
| 13942 |
// SCEV treats "undef" as an unknown but consistent value (i.e. it does |
--- |
13942 |
// SCEV treats "undef" as an unknown but consistent value (i.e. it does |
--- |
| 13943 |
// not propagate undef aggressively). This means we can (and do) fail |
--- |
13943 |
// not propagate undef aggressively). This means we can (and do) fail |
--- |
| 13944 |
// verification in cases where a transform makes a value go from "undef" |
--- |
13944 |
// verification in cases where a transform makes a value go from "undef" |
--- |
| 13945 |
// to "undef+1" (say). The transform is fine, since in both cases the |
--- |
13945 |
// to "undef+1" (say). The transform is fine, since in both cases the |
--- |
| 13946 |
// result is "undef", but SCEV thinks the value increased by 1. |
--- |
13946 |
// result is "undef", but SCEV thinks the value increased by 1. |
--- |
| 13947 |
return nullptr; |
0 |
13947 |
return nullptr; |
0 |
| 13948 |
} |
--- |
13948 |
} |
--- |
| 13949 |
|
--- |
13949 |
|
--- |
| 13950 |
// Unless VerifySCEVStrict is set, we only compare constant deltas. |
--- |
13950 |
// Unless VerifySCEVStrict is set, we only compare constant deltas. |
--- |
| 13951 |
const SCEV *Delta = SE2.getMinusSCEV(Old, New); |
0 |
13951 |
const SCEV *Delta = SE2.getMinusSCEV(Old, New); |
0 |
| 13952 |
if (!VerifySCEVStrict && !isa(Delta)) |
0 |
13952 |
if (!VerifySCEVStrict && !isa(Delta)) |
0 |
| 13953 |
return nullptr; |
0 |
13953 |
return nullptr; |
0 |
| 13954 |
|
--- |
13954 |
|
--- |
| 13955 |
return Delta; |
0 |
13955 |
return Delta; |
0 |
| 13956 |
}; |
0 |
13956 |
}; |
0 |
| 13957 |
|
--- |
13957 |
|
--- |
| 13958 |
while (!LoopStack.empty()) { |
0 |
13958 |
while (!LoopStack.empty()) { |
0 |
| 13959 |
auto *L = LoopStack.pop_back_val(); |
0 |
13959 |
auto *L = LoopStack.pop_back_val(); |
0 |
| 13960 |
llvm::append_range(LoopStack, *L); |
0 |
13960 |
llvm::append_range(LoopStack, *L); |
0 |
| 13961 |
|
--- |
13961 |
|
--- |
| 13962 |
// Only verify BECounts in reachable loops. For an unreachable loop, |
--- |
13962 |
// Only verify BECounts in reachable loops. For an unreachable loop, |
--- |
| 13963 |
// any BECount is legal. |
--- |
13963 |
// any BECount is legal. |
--- |
| 13964 |
if (!ReachableBlocks.contains(L->getHeader())) |
0 |
13964 |
if (!ReachableBlocks.contains(L->getHeader())) |
0 |
| 13965 |
continue; |
0 |
13965 |
continue; |
0 |
| 13966 |
|
--- |
13966 |
|
--- |
| 13967 |
// Only verify cached BECounts. Computing new BECounts may change the |
--- |
13967 |
// Only verify cached BECounts. Computing new BECounts may change the |
--- |
| 13968 |
// results of subsequent SCEV uses. |
--- |
13968 |
// results of subsequent SCEV uses. |
--- |
| 13969 |
auto It = BackedgeTakenCounts.find(L); |
0 |
13969 |
auto It = BackedgeTakenCounts.find(L); |
0 |
| 13970 |
if (It == BackedgeTakenCounts.end()) |
0 |
13970 |
if (It == BackedgeTakenCounts.end()) |
0 |
| 13971 |
continue; |
0 |
13971 |
continue; |
0 |
| 13972 |
|
--- |
13972 |
|
--- |
| 13973 |
auto *CurBECount = |
--- |
13973 |
auto *CurBECount = |
--- |
| 13974 |
SCM.visit(It->second.getExact(L, const_cast(this))); |
0 |
13974 |
SCM.visit(It->second.getExact(L, const_cast(this))); |
0 |
| 13975 |
auto *NewBECount = SE2.getBackedgeTakenCount(L); |
0 |
13975 |
auto *NewBECount = SE2.getBackedgeTakenCount(L); |
0 |
| 13976 |
|
--- |
13976 |
|
--- |
| 13977 |
if (CurBECount == SE2.getCouldNotCompute() || |
0 |
13977 |
if (CurBECount == SE2.getCouldNotCompute() || |
0 |
| 13978 |
NewBECount == SE2.getCouldNotCompute()) { |
0 |
13978 |
NewBECount == SE2.getCouldNotCompute()) { |
0 |
| 13979 |
// NB! This situation is legal, but is very suspicious -- whatever pass |
--- |
13979 |
// NB! This situation is legal, but is very suspicious -- whatever pass |
--- |
| 13980 |
// change the loop to make a trip count go from could not compute to |
--- |
13980 |
// change the loop to make a trip count go from could not compute to |
--- |
| 13981 |
// computable or vice-versa *should have* invalidated SCEV. However, we |
--- |
13981 |
// computable or vice-versa *should have* invalidated SCEV. However, we |
--- |
| 13982 |
// choose not to assert here (for now) since we don't want false |
--- |
13982 |
// choose not to assert here (for now) since we don't want false |
--- |
| 13983 |
// positives. |
--- |
13983 |
// positives. |
--- |
| 13984 |
continue; |
0 |
13984 |
continue; |
0 |
| 13985 |
} |
--- |
13985 |
} |
--- |
| 13986 |
|
--- |
13986 |
|
--- |
| 13987 |
if (SE.getTypeSizeInBits(CurBECount->getType()) > |
0 |
13987 |
if (SE.getTypeSizeInBits(CurBECount->getType()) > |
0 |
| 13988 |
SE.getTypeSizeInBits(NewBECount->getType())) |
0 |
13988 |
SE.getTypeSizeInBits(NewBECount->getType())) |
0 |
| 13989 |
NewBECount = SE2.getZeroExtendExpr(NewBECount, CurBECount->getType()); |
0 |
13989 |
NewBECount = SE2.getZeroExtendExpr(NewBECount, CurBECount->getType()); |
0 |
| 13990 |
else if (SE.getTypeSizeInBits(CurBECount->getType()) < |
0 |
13990 |
else if (SE.getTypeSizeInBits(CurBECount->getType()) < |
0 |
| 13991 |
SE.getTypeSizeInBits(NewBECount->getType())) |
0 |
13991 |
SE.getTypeSizeInBits(NewBECount->getType())) |
0 |
| 13992 |
CurBECount = SE2.getZeroExtendExpr(CurBECount, NewBECount->getType()); |
0 |
13992 |
CurBECount = SE2.getZeroExtendExpr(CurBECount, NewBECount->getType()); |
0 |
| 13993 |
|
--- |
13993 |
|
--- |
| 13994 |
const SCEV *Delta = GetDelta(CurBECount, NewBECount); |
0 |
13994 |
const SCEV *Delta = GetDelta(CurBECount, NewBECount); |
0 |
| 13995 |
if (Delta && !Delta->isZero()) { |
0 |
13995 |
if (Delta && !Delta->isZero()) { |
0 |
| 13996 |
dbgs() << "Trip Count for " << *L << " Changed!\n"; |
0 |
13996 |
dbgs() << "Trip Count for " << *L << " Changed!\n"; |
0 |
| 13997 |
dbgs() << "Old: " << *CurBECount << "\n"; |
0 |
13997 |
dbgs() << "Old: " << *CurBECount << "\n"; |
0 |
| 13998 |
dbgs() << "New: " << *NewBECount << "\n"; |
0 |
13998 |
dbgs() << "New: " << *NewBECount << "\n"; |
0 |
| 13999 |
dbgs() << "Delta: " << *Delta << "\n"; |
0 |
13999 |
dbgs() << "Delta: " << *Delta << "\n"; |
0 |
| 14000 |
std::abort(); |
0 |
14000 |
std::abort(); |
0 |
| 14001 |
} |
--- |
14001 |
} |
--- |
| 14002 |
} |
--- |
14002 |
} |
--- |
| 14003 |
|
--- |
14003 |
|
--- |
| 14004 |
// Collect all valid loops currently in LoopInfo. |
--- |
14004 |
// Collect all valid loops currently in LoopInfo. |
--- |
| 14005 |
SmallPtrSet ValidLoops; |
0 |
14005 |
SmallPtrSet ValidLoops; |
0 |
| 14006 |
SmallVector Worklist(LI.begin(), LI.end()); |
0 |
14006 |
SmallVector Worklist(LI.begin(), LI.end()); |
0 |
| 14007 |
while (!Worklist.empty()) { |
0 |
14007 |
while (!Worklist.empty()) { |
0 |
| 14008 |
Loop *L = Worklist.pop_back_val(); |
0 |
14008 |
Loop *L = Worklist.pop_back_val(); |
0 |
| 14009 |
if (ValidLoops.insert(L).second) |
0 |
14009 |
if (ValidLoops.insert(L).second) |
0 |
| 14010 |
Worklist.append(L->begin(), L->end()); |
0 |
14010 |
Worklist.append(L->begin(), L->end()); |
0 |
| 14011 |
} |
--- |
14011 |
} |
--- |
| 14012 |
for (const auto &KV : ValueExprMap) { |
0 |
14012 |
for (const auto &KV : ValueExprMap) { |
0 |
| 14013 |
#ifndef NDEBUG |
--- |
14013 |
#ifndef NDEBUG |
--- |
| 14014 |
// Check for SCEV expressions referencing invalid/deleted loops. |
--- |
14014 |
// Check for SCEV expressions referencing invalid/deleted loops. |
--- |
| 14015 |
if (auto *AR = dyn_cast(KV.second)) { |
0 |
14015 |
if (auto *AR = dyn_cast(KV.second)) { |
0 |
| 14016 |
assert(ValidLoops.contains(AR->getLoop()) && |
0 |
14016 |
assert(ValidLoops.contains(AR->getLoop()) && |
0 |
| 14017 |
"AddRec references invalid loop"); |
--- |
14017 |
"AddRec references invalid loop"); |
--- |
| 14018 |
} |
--- |
14018 |
} |
--- |
| 14019 |
#endif |
--- |
14019 |
#endif |
--- |
| 14020 |
|
--- |
14020 |
|
--- |
| 14021 |
// Check that the value is also part of the reverse map. |
--- |
14021 |
// Check that the value is also part of the reverse map. |
--- |
| 14022 |
auto It = ExprValueMap.find(KV.second); |
0 |
14022 |
auto It = ExprValueMap.find(KV.second); |
0 |
| 14023 |
if (It == ExprValueMap.end() || !It->second.contains(KV.first)) { |
0 |
14023 |
if (It == ExprValueMap.end() || !It->second.contains(KV.first)) { |
0 |
| 14024 |
dbgs() << "Value " << *KV.first |
0 |
14024 |
dbgs() << "Value " << *KV.first |
0 |
| 14025 |
<< " is in ValueExprMap but not in ExprValueMap\n"; |
0 |
14025 |
<< " is in ValueExprMap but not in ExprValueMap\n"; |
0 |
| 14026 |
std::abort(); |
0 |
14026 |
std::abort(); |
0 |
| 14027 |
} |
--- |
14027 |
} |
--- |
| 14028 |
|
--- |
14028 |
|
--- |
| 14029 |
if (auto *I = dyn_cast(&*KV.first)) { |
0 |
14029 |
if (auto *I = dyn_cast(&*KV.first)) { |
0 |
| 14030 |
if (!ReachableBlocks.contains(I->getParent())) |
0 |
14030 |
if (!ReachableBlocks.contains(I->getParent())) |
0 |
| 14031 |
continue; |
0 |
14031 |
continue; |
0 |
| 14032 |
const SCEV *OldSCEV = SCM.visit(KV.second); |
0 |
14032 |
const SCEV *OldSCEV = SCM.visit(KV.second); |
0 |
| 14033 |
const SCEV *NewSCEV = SE2.getSCEV(I); |
0 |
14033 |
const SCEV *NewSCEV = SE2.getSCEV(I); |
0 |
| 14034 |
const SCEV *Delta = GetDelta(OldSCEV, NewSCEV); |
0 |
14034 |
const SCEV *Delta = GetDelta(OldSCEV, NewSCEV); |
0 |
| 14035 |
if (Delta && !Delta->isZero()) { |
0 |
14035 |
if (Delta && !Delta->isZero()) { |
0 |
| 14036 |
dbgs() << "SCEV for value " << *I << " changed!\n" |
0 |
14036 |
dbgs() << "SCEV for value " << *I << " changed!\n" |
0 |
| 14037 |
<< "Old: " << *OldSCEV << "\n" |
0 |
14037 |
<< "Old: " << *OldSCEV << "\n" |
0 |
| 14038 |
<< "New: " << *NewSCEV << "\n" |
0 |
14038 |
<< "New: " << *NewSCEV << "\n" |
0 |
| 14039 |
<< "Delta: " << *Delta << "\n"; |
0 |
14039 |
<< "Delta: " << *Delta << "\n"; |
0 |
| 14040 |
std::abort(); |
0 |
14040 |
std::abort(); |
0 |
| 14041 |
} |
--- |
14041 |
} |
--- |
| 14042 |
} |
--- |
14042 |
} |
--- |
| 14043 |
} |
--- |
14043 |
} |
--- |
| 14044 |
|
--- |
14044 |
|
--- |
| 14045 |
for (const auto &KV : ExprValueMap) { |
0 |
14045 |
for (const auto &KV : ExprValueMap) { |
0 |
| 14046 |
for (Value *V : KV.second) { |
0 |
14046 |
for (Value *V : KV.second) { |
0 |
| 14047 |
auto It = ValueExprMap.find_as(V); |
0 |
14047 |
auto It = ValueExprMap.find_as(V); |
0 |
| 14048 |
if (It == ValueExprMap.end()) { |
0 |
14048 |
if (It == ValueExprMap.end()) { |
0 |
| 14049 |
dbgs() << "Value " << *V |
0 |
14049 |
dbgs() << "Value " << *V |
0 |
| 14050 |
<< " is in ExprValueMap but not in ValueExprMap\n"; |
0 |
14050 |
<< " is in ExprValueMap but not in ValueExprMap\n"; |
0 |
| 14051 |
std::abort(); |
0 |
14051 |
std::abort(); |
0 |
| 14052 |
} |
--- |
14052 |
} |
--- |
| 14053 |
if (It->second != KV.first) { |
0 |
14053 |
if (It->second != KV.first) { |
0 |
| 14054 |
dbgs() << "Value " << *V << " mapped to " << *It->second |
0 |
14054 |
dbgs() << "Value " << *V << " mapped to " << *It->second |
0 |
| 14055 |
<< " rather than " << *KV.first << "\n"; |
0 |
14055 |
<< " rather than " << *KV.first << "\n"; |
0 |
| 14056 |
std::abort(); |
0 |
14056 |
std::abort(); |
0 |
| 14057 |
} |
--- |
14057 |
} |
--- |
| 14058 |
} |
--- |
14058 |
} |
--- |
| 14059 |
} |
--- |
14059 |
} |
--- |
| 14060 |
|
--- |
14060 |
|
--- |
| 14061 |
// Verify integrity of SCEV users. |
--- |
14061 |
// Verify integrity of SCEV users. |
--- |
| 14062 |
for (const auto &S : UniqueSCEVs) { |
0 |
14062 |
for (const auto &S : UniqueSCEVs) { |
0 |
| 14063 |
for (const auto *Op : S.operands()) { |
0 |
14063 |
for (const auto *Op : S.operands()) { |
0 |
| 14064 |
// We do not store dependencies of constants. |
--- |
14064 |
// We do not store dependencies of constants. |
--- |
| 14065 |
if (isa(Op)) |
0 |
14065 |
if (isa(Op)) |
0 |
| 14066 |
continue; |
0 |
14066 |
continue; |
0 |
| 14067 |
auto It = SCEVUsers.find(Op); |
0 |
14067 |
auto It = SCEVUsers.find(Op); |
0 |
| 14068 |
if (It != SCEVUsers.end() && It->second.count(&S)) |
0 |
14068 |
if (It != SCEVUsers.end() && It->second.count(&S)) |
0 |
| 14069 |
continue; |
0 |
14069 |
continue; |
0 |
| 14070 |
dbgs() << "Use of operand " << *Op << " by user " << S |
0 |
14070 |
dbgs() << "Use of operand " << *Op << " by user " << S |
0 |
| 14071 |
<< " is not being tracked!\n"; |
0 |
14071 |
<< " is not being tracked!\n"; |
0 |
| 14072 |
std::abort(); |
0 |
14072 |
std::abort(); |
0 |
| 14073 |
} |
--- |
14073 |
} |
--- |
| 14074 |
} |
--- |
14074 |
} |
--- |
| 14075 |
|
--- |
14075 |
|
--- |
| 14076 |
// Verify integrity of ValuesAtScopes users. |
--- |
14076 |
// Verify integrity of ValuesAtScopes users. |
--- |
| 14077 |
for (const auto &ValueAndVec : ValuesAtScopes) { |
0 |
14077 |
for (const auto &ValueAndVec : ValuesAtScopes) { |
0 |
| 14078 |
const SCEV *Value = ValueAndVec.first; |
0 |
14078 |
const SCEV *Value = ValueAndVec.first; |
0 |
| 14079 |
for (const auto &LoopAndValueAtScope : ValueAndVec.second) { |
0 |
14079 |
for (const auto &LoopAndValueAtScope : ValueAndVec.second) { |
0 |
| 14080 |
const Loop *L = LoopAndValueAtScope.first; |
0 |
14080 |
const Loop *L = LoopAndValueAtScope.first; |
0 |
| 14081 |
const SCEV *ValueAtScope = LoopAndValueAtScope.second; |
0 |
14081 |
const SCEV *ValueAtScope = LoopAndValueAtScope.second; |
0 |
| 14082 |
if (!isa(ValueAtScope)) { |
0 |
14082 |
if (!isa(ValueAtScope)) { |
0 |
| 14083 |
auto It = ValuesAtScopesUsers.find(ValueAtScope); |
0 |
14083 |
auto It = ValuesAtScopesUsers.find(ValueAtScope); |
0 |
| 14084 |
if (It != ValuesAtScopesUsers.end() && |
0 |
14084 |
if (It != ValuesAtScopesUsers.end() && |
0 |
| 14085 |
is_contained(It->second, std::make_pair(L, Value))) |
0 |
14085 |
is_contained(It->second, std::make_pair(L, Value))) |
0 |
| 14086 |
continue; |
0 |
14086 |
continue; |
0 |
| 14087 |
dbgs() << "Value: " << *Value << ", Loop: " << *L << ", ValueAtScope: " |
0 |
14087 |
dbgs() << "Value: " << *Value << ", Loop: " << *L << ", ValueAtScope: " |
0 |
| 14088 |
<< *ValueAtScope << " missing in ValuesAtScopesUsers\n"; |
0 |
14088 |
<< *ValueAtScope << " missing in ValuesAtScopesUsers\n"; |
0 |
| 14089 |
std::abort(); |
0 |
14089 |
std::abort(); |
0 |
| 14090 |
} |
--- |
14090 |
} |
--- |
| 14091 |
} |
--- |
14091 |
} |
--- |
| 14092 |
} |
--- |
14092 |
} |
--- |
| 14093 |
|
--- |
14093 |
|
--- |
| 14094 |
for (const auto &ValueAtScopeAndVec : ValuesAtScopesUsers) { |
0 |
14094 |
for (const auto &ValueAtScopeAndVec : ValuesAtScopesUsers) { |
0 |
| 14095 |
const SCEV *ValueAtScope = ValueAtScopeAndVec.first; |
0 |
14095 |
const SCEV *ValueAtScope = ValueAtScopeAndVec.first; |
0 |
| 14096 |
for (const auto &LoopAndValue : ValueAtScopeAndVec.second) { |
0 |
14096 |
for (const auto &LoopAndValue : ValueAtScopeAndVec.second) { |
0 |
| 14097 |
const Loop *L = LoopAndValue.first; |
0 |
14097 |
const Loop *L = LoopAndValue.first; |
0 |
| 14098 |
const SCEV *Value = LoopAndValue.second; |
0 |
14098 |
const SCEV *Value = LoopAndValue.second; |
0 |
| 14099 |
assert(!isa(Value)); |
0 |
14099 |
assert(!isa(Value)); |
0 |
| 14100 |
auto It = ValuesAtScopes.find(Value); |
0 |
14100 |
auto It = ValuesAtScopes.find(Value); |
0 |
| 14101 |
if (It != ValuesAtScopes.end() && |
0 |
14101 |
if (It != ValuesAtScopes.end() && |
0 |
| 14102 |
is_contained(It->second, std::make_pair(L, ValueAtScope))) |
0 |
14102 |
is_contained(It->second, std::make_pair(L, ValueAtScope))) |
0 |
| 14103 |
continue; |
0 |
14103 |
continue; |
0 |
| 14104 |
dbgs() << "Value: " << *Value << ", Loop: " << *L << ", ValueAtScope: " |
0 |
14104 |
dbgs() << "Value: " << *Value << ", Loop: " << *L << ", ValueAtScope: " |
0 |
| 14105 |
<< *ValueAtScope << " missing in ValuesAtScopes\n"; |
0 |
14105 |
<< *ValueAtScope << " missing in ValuesAtScopes\n"; |
0 |
| 14106 |
std::abort(); |
0 |
14106 |
std::abort(); |
0 |
| 14107 |
} |
--- |
14107 |
} |
--- |
| 14108 |
} |
--- |
14108 |
} |
--- |
| 14109 |
|
--- |
14109 |
|
--- |
| 14110 |
// Verify integrity of BECountUsers. |
--- |
14110 |
// Verify integrity of BECountUsers. |
--- |
| 14111 |
auto VerifyBECountUsers = [&](bool Predicated) { |
0 |
14111 |
auto VerifyBECountUsers = [&](bool Predicated) { |
0 |
| 14112 |
auto &BECounts = |
0 |
14112 |
auto &BECounts = |
0 |
| 14113 |
Predicated ? PredicatedBackedgeTakenCounts : BackedgeTakenCounts; |
0 |
14113 |
Predicated ? PredicatedBackedgeTakenCounts : BackedgeTakenCounts; |
0 |
| 14114 |
for (const auto &LoopAndBEInfo : BECounts) { |
0 |
14114 |
for (const auto &LoopAndBEInfo : BECounts) { |
0 |
| 14115 |
for (const ExitNotTakenInfo &ENT : LoopAndBEInfo.second.ExitNotTaken) { |
0 |
14115 |
for (const ExitNotTakenInfo &ENT : LoopAndBEInfo.second.ExitNotTaken) { |
0 |
| 14116 |
for (const SCEV *S : {ENT.ExactNotTaken, ENT.SymbolicMaxNotTaken}) { |
0 |
14116 |
for (const SCEV *S : {ENT.ExactNotTaken, ENT.SymbolicMaxNotTaken}) { |
0 |
| 14117 |
if (!isa(S)) { |
0 |
14117 |
if (!isa(S)) { |
0 |
| 14118 |
auto UserIt = BECountUsers.find(S); |
0 |
14118 |
auto UserIt = BECountUsers.find(S); |
0 |
| 14119 |
if (UserIt != BECountUsers.end() && |
0 |
14119 |
if (UserIt != BECountUsers.end() && |
0 |
| 14120 |
UserIt->second.contains({ LoopAndBEInfo.first, Predicated })) |
0 |
14120 |
UserIt->second.contains({ LoopAndBEInfo.first, Predicated })) |
0 |
| 14121 |
continue; |
0 |
14121 |
continue; |
0 |
| 14122 |
dbgs() << "Value " << *S << " for loop " << *LoopAndBEInfo.first |
0 |
14122 |
dbgs() << "Value " << *S << " for loop " << *LoopAndBEInfo.first |
0 |
| 14123 |
<< " missing from BECountUsers\n"; |
0 |
14123 |
<< " missing from BECountUsers\n"; |
0 |
| 14124 |
std::abort(); |
0 |
14124 |
std::abort(); |
0 |
| 14125 |
} |
--- |
14125 |
} |
--- |
| 14126 |
} |
--- |
14126 |
} |
--- |
| 14127 |
} |
--- |
14127 |
} |
--- |
| 14128 |
} |
--- |
14128 |
} |
--- |
| 14129 |
}; |
0 |
14129 |
}; |
0 |
| 14130 |
VerifyBECountUsers(/* Predicated */ false); |
0 |
14130 |
VerifyBECountUsers(/* Predicated */ false); |
0 |
| 14131 |
VerifyBECountUsers(/* Predicated */ true); |
0 |
14131 |
VerifyBECountUsers(/* Predicated */ true); |
0 |
| 14132 |
|
--- |
14132 |
|
--- |
| 14133 |
// Verify intergity of loop disposition cache. |
--- |
14133 |
// Verify intergity of loop disposition cache. |
--- |
| 14134 |
for (auto &[S, Values] : LoopDispositions) { |
0 |
14134 |
for (auto &[S, Values] : LoopDispositions) { |
0 |
| 14135 |
for (auto [Loop, CachedDisposition] : Values) { |
0 |
14135 |
for (auto [Loop, CachedDisposition] : Values) { |
0 |
| 14136 |
const auto RecomputedDisposition = SE2.getLoopDisposition(S, Loop); |
0 |
14136 |
const auto RecomputedDisposition = SE2.getLoopDisposition(S, Loop); |
0 |
| 14137 |
if (CachedDisposition != RecomputedDisposition) { |
0 |
14137 |
if (CachedDisposition != RecomputedDisposition) { |
0 |
| 14138 |
dbgs() << "Cached disposition of " << *S << " for loop " << *Loop |
0 |
14138 |
dbgs() << "Cached disposition of " << *S << " for loop " << *Loop |
0 |
| 14139 |
<< " is incorrect: cached " << CachedDisposition << ", actual " |
0 |
14139 |
<< " is incorrect: cached " << CachedDisposition << ", actual " |
0 |
| 14140 |
<< RecomputedDisposition << "\n"; |
0 |
14140 |
<< RecomputedDisposition << "\n"; |
0 |
| 14141 |
std::abort(); |
0 |
14141 |
std::abort(); |
0 |
| 14142 |
} |
--- |
14142 |
} |
--- |
| 14143 |
} |
--- |
14143 |
} |
--- |
| 14144 |
} |
--- |
14144 |
} |
--- |
| 14145 |
|
--- |
14145 |
|
--- |
| 14146 |
// Verify integrity of the block disposition cache. |
--- |
14146 |
// Verify integrity of the block disposition cache. |
--- |
| 14147 |
for (auto &[S, Values] : BlockDispositions) { |
0 |
14147 |
for (auto &[S, Values] : BlockDispositions) { |
0 |
| 14148 |
for (auto [BB, CachedDisposition] : Values) { |
0 |
14148 |
for (auto [BB, CachedDisposition] : Values) { |
0 |
| 14149 |
const auto RecomputedDisposition = SE2.getBlockDisposition(S, BB); |
0 |
14149 |
const auto RecomputedDisposition = SE2.getBlockDisposition(S, BB); |
0 |
| 14150 |
if (CachedDisposition != RecomputedDisposition) { |
0 |
14150 |
if (CachedDisposition != RecomputedDisposition) { |
0 |
| 14151 |
dbgs() << "Cached disposition of " << *S << " for block %" |
0 |
14151 |
dbgs() << "Cached disposition of " << *S << " for block %" |
0 |
| 14152 |
<< BB->getName() << " is incorrect: cached " << CachedDisposition |
0 |
14152 |
<< BB->getName() << " is incorrect: cached " << CachedDisposition |
0 |
| 14153 |
<< ", actual " << RecomputedDisposition << "\n"; |
0 |
14153 |
<< ", actual " << RecomputedDisposition << "\n"; |
0 |
| 14154 |
std::abort(); |
0 |
14154 |
std::abort(); |
0 |
| 14155 |
} |
--- |
14155 |
} |
--- |
| 14156 |
} |
--- |
14156 |
} |
--- |
| 14157 |
} |
--- |
14157 |
} |
--- |
| 14158 |
|
--- |
14158 |
|
--- |
| 14159 |
// Verify FoldCache/FoldCacheUser caches. |
--- |
14159 |
// Verify FoldCache/FoldCacheUser caches. |
--- |
| 14160 |
for (auto [FoldID, Expr] : FoldCache) { |
0 |
14160 |
for (auto [FoldID, Expr] : FoldCache) { |
0 |
| 14161 |
auto I = FoldCacheUser.find(Expr); |
0 |
14161 |
auto I = FoldCacheUser.find(Expr); |
0 |
| 14162 |
if (I == FoldCacheUser.end()) { |
0 |
14162 |
if (I == FoldCacheUser.end()) { |
0 |
| 14163 |
dbgs() << "Missing entry in FoldCacheUser for cached expression " << *Expr |
0 |
14163 |
dbgs() << "Missing entry in FoldCacheUser for cached expression " << *Expr |
0 |
| 14164 |
<< "!\n"; |
0 |
14164 |
<< "!\n"; |
0 |
| 14165 |
std::abort(); |
0 |
14165 |
std::abort(); |
0 |
| 14166 |
} |
--- |
14166 |
} |
--- |
| 14167 |
if (!is_contained(I->second, FoldID)) { |
0 |
14167 |
if (!is_contained(I->second, FoldID)) { |
0 |
| 14168 |
dbgs() << "Missing FoldID in cached users of " << *Expr << "!\n"; |
0 |
14168 |
dbgs() << "Missing FoldID in cached users of " << *Expr << "!\n"; |
0 |
| 14169 |
std::abort(); |
0 |
14169 |
std::abort(); |
0 |
| 14170 |
} |
--- |
14170 |
} |
--- |
| 14171 |
} |
--- |
14171 |
} |
--- |
| 14172 |
for (auto [Expr, IDs] : FoldCacheUser) { |
0 |
14172 |
for (auto [Expr, IDs] : FoldCacheUser) { |
0 |
| 14173 |
for (auto &FoldID : IDs) { |
0 |
14173 |
for (auto &FoldID : IDs) { |
0 |
| 14174 |
auto I = FoldCache.find(FoldID); |
0 |
14174 |
auto I = FoldCache.find(FoldID); |
0 |
| 14175 |
if (I == FoldCache.end()) { |
0 |
14175 |
if (I == FoldCache.end()) { |
0 |
| 14176 |
dbgs() << "Missing entry in FoldCache for expression " << *Expr |
0 |
14176 |
dbgs() << "Missing entry in FoldCache for expression " << *Expr |
0 |
| 14177 |
<< "!\n"; |
0 |
14177 |
<< "!\n"; |
0 |
| 14178 |
std::abort(); |
0 |
14178 |
std::abort(); |
0 |
| 14179 |
} |
--- |
14179 |
} |
--- |
| 14180 |
if (I->second != Expr) { |
0 |
14180 |
if (I->second != Expr) { |
0 |
| 14181 |
dbgs() << "Entry in FoldCache doesn't match FoldCacheUser: " |
0 |
14181 |
dbgs() << "Entry in FoldCache doesn't match FoldCacheUser: " |
0 |
| 14182 |
<< *I->second << " != " << *Expr << "!\n"; |
0 |
14182 |
<< *I->second << " != " << *Expr << "!\n"; |
0 |
| 14183 |
std::abort(); |
0 |
14183 |
std::abort(); |
0 |
| 14184 |
} |
--- |
14184 |
} |
--- |
| 14185 |
} |
--- |
14185 |
} |
--- |
| 14186 |
} |
0 |
14186 |
} |
0 |
| 14187 |
|
--- |
14187 |
|
--- |
| 14188 |
// Verify that ConstantMultipleCache computations are correct. We check that |
--- |
14188 |
// Verify that ConstantMultipleCache computations are correct. We check that |
--- |
| 14189 |
// cached multiples and recomputed multiples are multiples of each other to |
--- |
14189 |
// cached multiples and recomputed multiples are multiples of each other to |
--- |
| 14190 |
// verify correctness. It is possible that a recomputed multiple is different |
--- |
14190 |
// verify correctness. It is possible that a recomputed multiple is different |
--- |
| 14191 |
// from the cached multiple due to strengthened no wrap flags or changes in |
--- |
14191 |
// from the cached multiple due to strengthened no wrap flags or changes in |
--- |
| 14192 |
// KnownBits computations. |
--- |
14192 |
// KnownBits computations. |
--- |
| 14193 |
for (auto [S, Multiple] : ConstantMultipleCache) { |
0 |
14193 |
for (auto [S, Multiple] : ConstantMultipleCache) { |
0 |
| 14194 |
APInt RecomputedMultiple = SE2.getConstantMultiple(S); |
0 |
14194 |
APInt RecomputedMultiple = SE2.getConstantMultiple(S); |
0 |
| 14195 |
if ((Multiple != 0 && RecomputedMultiple != 0 && |
0 |
14195 |
if ((Multiple != 0 && RecomputedMultiple != 0 && |
0 |
| 14196 |
Multiple.urem(RecomputedMultiple) != 0 && |
0 |
14196 |
Multiple.urem(RecomputedMultiple) != 0 && |
0 |
| 14197 |
RecomputedMultiple.urem(Multiple) != 0)) { |
0 |
14197 |
RecomputedMultiple.urem(Multiple) != 0)) { |
0 |
| 14198 |
dbgs() << "Incorrect cached computation in ConstantMultipleCache for " |
0 |
14198 |
dbgs() << "Incorrect cached computation in ConstantMultipleCache for " |
0 |
| 14199 |
<< *S << " : Computed " << RecomputedMultiple |
0 |
14199 |
<< *S << " : Computed " << RecomputedMultiple |
0 |
| 14200 |
<< " but cache contains " << Multiple << "!\n"; |
0 |
14200 |
<< " but cache contains " << Multiple << "!\n"; |
0 |
| 14201 |
std::abort(); |
0 |
14201 |
std::abort(); |
0 |
| 14202 |
} |
--- |
14202 |
} |
--- |
| 14203 |
} |
0 |
14203 |
} |
0 |
| 14204 |
} |
0 |
14204 |
} |
0 |
| 14205 |
|
--- |
14205 |
|
--- |
| 14206 |
bool ScalarEvolution::invalidate( |
0 |
14206 |
bool ScalarEvolution::invalidate( |
0 |
| 14207 |
Function &F, const PreservedAnalyses &PA, |
--- |
14207 |
Function &F, const PreservedAnalyses &PA, |
--- |
| 14208 |
FunctionAnalysisManager::Invalidator &Inv) { |
--- |
14208 |
FunctionAnalysisManager::Invalidator &Inv) { |
--- |
| 14209 |
// Invalidate the ScalarEvolution object whenever it isn't preserved or one |
--- |
14209 |
// Invalidate the ScalarEvolution object whenever it isn't preserved or one |
--- |
| 14210 |
// of its dependencies is invalidated. |
--- |
14210 |
// of its dependencies is invalidated. |
--- |
| 14211 |
auto PAC = PA.getChecker(); |
0 |
14211 |
auto PAC = PA.getChecker(); |
0 |
| 14212 |
return !(PAC.preserved() || PAC.preservedSet>()) || |
0 |
14212 |
return !(PAC.preserved() || PAC.preservedSet>()) || |
0 |
| 14213 |
Inv.invalidate(F, PA) || |
0 |
14213 |
Inv.invalidate(F, PA) || |
0 |
| 14214 |
Inv.invalidate(F, PA) || |
0 |
14214 |
Inv.invalidate(F, PA) || |
0 |
| 14215 |
Inv.invalidate(F, PA); |
0 |
14215 |
Inv.invalidate(F, PA); |
0 |
| 14216 |
} |
--- |
14216 |
} |
--- |
| 14217 |
|
--- |
14217 |
|
--- |
| 14218 |
AnalysisKey ScalarEvolutionAnalysis::Key; |
--- |
14218 |
AnalysisKey ScalarEvolutionAnalysis::Key; |
--- |
| 14219 |
|
--- |
14219 |
|
--- |
| 14220 |
ScalarEvolution ScalarEvolutionAnalysis::run(Function &F, |
0 |
14220 |
ScalarEvolution ScalarEvolutionAnalysis::run(Function &F, |
0 |
| 14221 |
FunctionAnalysisManager &AM) { |
--- |
14221 |
FunctionAnalysisManager &AM) { |
--- |
| 14222 |
auto &TLI = AM.getResult(F); |
0 |
14222 |
auto &TLI = AM.getResult(F); |
0 |
| 14223 |
auto &AC = AM.getResult(F); |
0 |
14223 |
auto &AC = AM.getResult(F); |
0 |
| 14224 |
auto &DT = AM.getResult(F); |
0 |
14224 |
auto &DT = AM.getResult(F); |
0 |
| 14225 |
auto &LI = AM.getResult(F); |
0 |
14225 |
auto &LI = AM.getResult(F); |
0 |
| 14226 |
return ScalarEvolution(F, TLI, AC, DT, LI); |
0 |
14226 |
return ScalarEvolution(F, TLI, AC, DT, LI); |
0 |
| 14227 |
} |
--- |
14227 |
} |
--- |
| 14228 |
|
--- |
14228 |
|
--- |
| 14229 |
PreservedAnalyses |
--- |
14229 |
PreservedAnalyses |
--- |
| 14230 |
ScalarEvolutionVerifierPass::run(Function &F, FunctionAnalysisManager &AM) { |
0 |
14230 |
ScalarEvolutionVerifierPass::run(Function &F, FunctionAnalysisManager &AM) { |
0 |
| 14231 |
AM.getResult(F).verify(); |
0 |
14231 |
AM.getResult(F).verify(); |
0 |
| 14232 |
return PreservedAnalyses::all(); |
0 |
14232 |
return PreservedAnalyses::all(); |
0 |
| 14233 |
} |
--- |
14233 |
} |
--- |
| 14234 |
|
--- |
14234 |
|
--- |
| 14235 |
PreservedAnalyses |
--- |
14235 |
PreservedAnalyses |
--- |
| 14236 |
ScalarEvolutionPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { |
0 |
14236 |
ScalarEvolutionPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { |
0 |
| 14237 |
// For compatibility with opt's -analyze feature under legacy pass manager |
--- |
14237 |
// For compatibility with opt's -analyze feature under legacy pass manager |
--- |
| 14238 |
// which was not ported to NPM. This keeps tests using |
--- |
14238 |
// which was not ported to NPM. This keeps tests using |
--- |
| 14239 |
// update_analyze_test_checks.py working. |
--- |
14239 |
// update_analyze_test_checks.py working. |
--- |
| 14240 |
OS << "Printing analysis 'Scalar Evolution Analysis' for function '" |
0 |
14240 |
OS << "Printing analysis 'Scalar Evolution Analysis' for function '" |
0 |
| 14241 |
<< F.getName() << "':\n"; |
0 |
14241 |
<< F.getName() << "':\n"; |
0 |
| 14242 |
AM.getResult(F).print(OS); |
0 |
14242 |
AM.getResult(F).print(OS); |
0 |
| 14243 |
return PreservedAnalyses::all(); |
0 |
14243 |
return PreservedAnalyses::all(); |
0 |
| 14244 |
} |
--- |
14244 |
} |
--- |
| 14245 |
|
--- |
14245 |
|
--- |
| 14246 |
INITIALIZE_PASS_BEGIN(ScalarEvolutionWrapperPass, "scalar-evolution", |
1 |
14246 |
INITIALIZE_PASS_BEGIN(ScalarEvolutionWrapperPass, "scalar-evolution", |
1 |
| 14247 |
"Scalar Evolution Analysis", false, true) |
--- |
14247 |
"Scalar Evolution Analysis", false, true) |
--- |
| 14248 |
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
1 |
14248 |
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
1 |
| 14249 |
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
1 |
14249 |
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
1 |
| 14250 |
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
1 |
14250 |
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
1 |
| 14251 |
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
1 |
14251 |
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
1 |
| 14252 |
INITIALIZE_PASS_END(ScalarEvolutionWrapperPass, "scalar-evolution", |
25 |
14252 |
INITIALIZE_PASS_END(ScalarEvolutionWrapperPass, "scalar-evolution", |
25 |
| 14253 |
"Scalar Evolution Analysis", false, true) |
--- |
14253 |
"Scalar Evolution Analysis", false, true) |
--- |
| 14254 |
|
--- |
14254 |
|
--- |
| 14255 |
char ScalarEvolutionWrapperPass::ID = 0; |
--- |
14255 |
char ScalarEvolutionWrapperPass::ID = 0; |
--- |
| 14256 |
|
--- |
14256 |
|
--- |
| 14257 |
ScalarEvolutionWrapperPass::ScalarEvolutionWrapperPass() : FunctionPass(ID) { |
1 |
14257 |
ScalarEvolutionWrapperPass::ScalarEvolutionWrapperPass() : FunctionPass(ID) { |
1 |
| 14258 |
initializeScalarEvolutionWrapperPassPass(*PassRegistry::getPassRegistry()); |
1 |
14258 |
initializeScalarEvolutionWrapperPassPass(*PassRegistry::getPassRegistry()); |
1 |
| 14259 |
} |
1 |
14259 |
} |
1 |
| 14260 |
|
--- |
14260 |
|
--- |
| 14261 |
bool ScalarEvolutionWrapperPass::runOnFunction(Function &F) { |
1 |
14261 |
bool ScalarEvolutionWrapperPass::runOnFunction(Function &F) { |
1 |
| 14262 |
SE.reset(new ScalarEvolution( |
1 |
14262 |
SE.reset(new ScalarEvolution( |
1 |
| 14263 |
F, getAnalysis().getTLI(F), |
1 |
14263 |
F, getAnalysis().getTLI(F), |
1 |
| 14264 |
getAnalysis().getAssumptionCache(F), |
1 |
14264 |
getAnalysis().getAssumptionCache(F), |
1 |
| 14265 |
getAnalysis().getDomTree(), |
1 |
14265 |
getAnalysis().getDomTree(), |
1 |
| 14266 |
getAnalysis().getLoopInfo())); |
1 |
14266 |
getAnalysis().getLoopInfo())); |
1 |
| 14267 |
return false; |
1 |
14267 |
return false; |
1 |
| 14268 |
} |
--- |
14268 |
} |
--- |
| 14269 |
|
--- |
14269 |
|
--- |
| 14270 |
void ScalarEvolutionWrapperPass::releaseMemory() { SE.reset(); } |
1 |
14270 |
void ScalarEvolutionWrapperPass::releaseMemory() { SE.reset(); } |
1 |
| 14271 |
|
--- |
14271 |
|
--- |
| 14272 |
void ScalarEvolutionWrapperPass::print(raw_ostream &OS, const Module *) const { |
0 |
14272 |
void ScalarEvolutionWrapperPass::print(raw_ostream &OS, const Module *) const { |
0 |
| 14273 |
SE->print(OS); |
0 |
14273 |
SE->print(OS); |
0 |
| 14274 |
} |
0 |
14274 |
} |
0 |
| 14275 |
|
--- |
14275 |
|
--- |
| 14276 |
void ScalarEvolutionWrapperPass::verifyAnalysis() const { |
0 |
14276 |
void ScalarEvolutionWrapperPass::verifyAnalysis() const { |
0 |
| 14277 |
if (!VerifySCEV) |
0 |
14277 |
if (!VerifySCEV) |
0 |
| 14278 |
return; |
0 |
14278 |
return; |
0 |
| 14279 |
|
--- |
14279 |
|
--- |
| 14280 |
SE->verify(); |
0 |
14280 |
SE->verify(); |
0 |
| 14281 |
} |
--- |
14281 |
} |
--- |
| 14282 |
|
--- |
14282 |
|
--- |
| 14283 |
void ScalarEvolutionWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
1 |
14283 |
void ScalarEvolutionWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
1 |
| 14284 |
AU.setPreservesAll(); |
1 |
14284 |
AU.setPreservesAll(); |
1 |
| 14285 |
AU.addRequiredTransitive(); |
1 |
14285 |
AU.addRequiredTransitive(); |
1 |
| 14286 |
AU.addRequiredTransitive(); |
1 |
14286 |
AU.addRequiredTransitive(); |
1 |
| 14287 |
AU.addRequiredTransitive(); |
1 |
14287 |
AU.addRequiredTransitive(); |
1 |
| 14288 |
AU.addRequiredTransitive(); |
1 |
14288 |
AU.addRequiredTransitive(); |
1 |
| 14289 |
} |
1 |
14289 |
} |
1 |
| 14290 |
|
--- |
14290 |
|
--- |
| 14291 |
const SCEVPredicate *ScalarEvolution::getEqualPredicate(const SCEV *LHS, |
0 |
14291 |
const SCEVPredicate *ScalarEvolution::getEqualPredicate(const SCEV *LHS, |
0 |
| 14292 |
const SCEV *RHS) { |
--- |
14292 |
const SCEV *RHS) { |
--- |
| 14293 |
return getComparePredicate(ICmpInst::ICMP_EQ, LHS, RHS); |
0 |
14293 |
return getComparePredicate(ICmpInst::ICMP_EQ, LHS, RHS); |
0 |
| 14294 |
} |
--- |
14294 |
} |
--- |
| 14295 |
|
--- |
14295 |
|
--- |
| 14296 |
const SCEVPredicate * |
--- |
14296 |
const SCEVPredicate * |
--- |
| 14297 |
ScalarEvolution::getComparePredicate(const ICmpInst::Predicate Pred, |
0 |
14297 |
ScalarEvolution::getComparePredicate(const ICmpInst::Predicate Pred, |
0 |
| 14298 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
14298 |
const SCEV *LHS, const SCEV *RHS) { |
--- |
| 14299 |
FoldingSetNodeID ID; |
0 |
14299 |
FoldingSetNodeID ID; |
0 |
| 14300 |
assert(LHS->getType() == RHS->getType() && |
0 |
14300 |
assert(LHS->getType() == RHS->getType() && |
0 |
| 14301 |
"Type mismatch between LHS and RHS"); |
--- |
14301 |
"Type mismatch between LHS and RHS"); |
--- |
| 14302 |
// Unique this node based on the arguments |
--- |
14302 |
// Unique this node based on the arguments |
--- |
| 14303 |
ID.AddInteger(SCEVPredicate::P_Compare); |
0 |
14303 |
ID.AddInteger(SCEVPredicate::P_Compare); |
0 |
| 14304 |
ID.AddInteger(Pred); |
0 |
14304 |
ID.AddInteger(Pred); |
0 |
| 14305 |
ID.AddPointer(LHS); |
0 |
14305 |
ID.AddPointer(LHS); |
0 |
| 14306 |
ID.AddPointer(RHS); |
0 |
14306 |
ID.AddPointer(RHS); |
0 |
| 14307 |
void *IP = nullptr; |
0 |
14307 |
void *IP = nullptr; |
0 |
| 14308 |
if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, IP)) |
0 |
14308 |
if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, IP)) |
0 |
| 14309 |
return S; |
0 |
14309 |
return S; |
0 |
| 14310 |
SCEVComparePredicate *Eq = new (SCEVAllocator) |
0 |
14310 |
SCEVComparePredicate *Eq = new (SCEVAllocator) |
0 |
| 14311 |
SCEVComparePredicate(ID.Intern(SCEVAllocator), Pred, LHS, RHS); |
0 |
14311 |
SCEVComparePredicate(ID.Intern(SCEVAllocator), Pred, LHS, RHS); |
0 |
| 14312 |
UniquePreds.InsertNode(Eq, IP); |
0 |
14312 |
UniquePreds.InsertNode(Eq, IP); |
0 |
| 14313 |
return Eq; |
0 |
14313 |
return Eq; |
0 |
| 14314 |
} |
0 |
14314 |
} |
0 |
| 14315 |
|
--- |
14315 |
|
--- |
| 14316 |
const SCEVPredicate *ScalarEvolution::getWrapPredicate( |
0 |
14316 |
const SCEVPredicate *ScalarEvolution::getWrapPredicate( |
0 |
| 14317 |
const SCEVAddRecExpr *AR, |
--- |
14317 |
const SCEVAddRecExpr *AR, |
--- |
| 14318 |
SCEVWrapPredicate::IncrementWrapFlags AddedFlags) { |
--- |
14318 |
SCEVWrapPredicate::IncrementWrapFlags AddedFlags) { |
--- |
| 14319 |
FoldingSetNodeID ID; |
0 |
14319 |
FoldingSetNodeID ID; |
0 |
| 14320 |
// Unique this node based on the arguments |
--- |
14320 |
// Unique this node based on the arguments |
--- |
| 14321 |
ID.AddInteger(SCEVPredicate::P_Wrap); |
0 |
14321 |
ID.AddInteger(SCEVPredicate::P_Wrap); |
0 |
| 14322 |
ID.AddPointer(AR); |
0 |
14322 |
ID.AddPointer(AR); |
0 |
| 14323 |
ID.AddInteger(AddedFlags); |
0 |
14323 |
ID.AddInteger(AddedFlags); |
0 |
| 14324 |
void *IP = nullptr; |
0 |
14324 |
void *IP = nullptr; |
0 |
| 14325 |
if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, IP)) |
0 |
14325 |
if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, IP)) |
0 |
| 14326 |
return S; |
0 |
14326 |
return S; |
0 |
| 14327 |
auto *OF = new (SCEVAllocator) |
0 |
14327 |
auto *OF = new (SCEVAllocator) |
0 |
| 14328 |
SCEVWrapPredicate(ID.Intern(SCEVAllocator), AR, AddedFlags); |
0 |
14328 |
SCEVWrapPredicate(ID.Intern(SCEVAllocator), AR, AddedFlags); |
0 |
| 14329 |
UniquePreds.InsertNode(OF, IP); |
0 |
14329 |
UniquePreds.InsertNode(OF, IP); |
0 |
| 14330 |
return OF; |
0 |
14330 |
return OF; |
0 |
| 14331 |
} |
0 |
14331 |
} |
0 |
| 14332 |
|
--- |
14332 |
|
--- |
| 14333 |
namespace { |
--- |
14333 |
namespace { |
--- |
| 14334 |
|
--- |
14334 |
|
--- |
| 14335 |
class SCEVPredicateRewriter : public SCEVRewriteVisitor { |
--- |
14335 |
class SCEVPredicateRewriter : public SCEVRewriteVisitor { |
--- |
| 14336 |
public: |
--- |
14336 |
public: |
--- |
| 14337 |
|
--- |
14337 |
|
--- |
| 14338 |
/// Rewrites \p S in the context of a loop L and the SCEV predication |
--- |
14338 |
/// Rewrites \p S in the context of a loop L and the SCEV predication |
--- |
| 14339 |
/// infrastructure. |
--- |
14339 |
/// infrastructure. |
--- |
| 14340 |
/// |
--- |
14340 |
/// |
--- |
| 14341 |
/// If \p Pred is non-null, the SCEV expression is rewritten to respect the |
--- |
14341 |
/// If \p Pred is non-null, the SCEV expression is rewritten to respect the |
--- |
| 14342 |
/// equivalences present in \p Pred. |
--- |
14342 |
/// equivalences present in \p Pred. |
--- |
| 14343 |
/// |
--- |
14343 |
/// |
--- |
| 14344 |
/// If \p NewPreds is non-null, rewrite is free to add further predicates to |
--- |
14344 |
/// If \p NewPreds is non-null, rewrite is free to add further predicates to |
--- |
| 14345 |
/// \p NewPreds such that the result will be an AddRecExpr. |
--- |
14345 |
/// \p NewPreds such that the result will be an AddRecExpr. |
--- |
| 14346 |
static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE, |
0 |
14346 |
static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE, |
0 |
| 14347 |
SmallPtrSetImpl *NewPreds, |
--- |
14347 |
SmallPtrSetImpl *NewPreds, |
--- |
| 14348 |
const SCEVPredicate *Pred) { |
--- |
14348 |
const SCEVPredicate *Pred) { |
--- |
| 14349 |
SCEVPredicateRewriter Rewriter(L, SE, NewPreds, Pred); |
0 |
14349 |
SCEVPredicateRewriter Rewriter(L, SE, NewPreds, Pred); |
0 |
| 14350 |
return Rewriter.visit(S); |
0 |
14350 |
return Rewriter.visit(S); |
0 |
| 14351 |
} |
0 |
14351 |
} |
0 |
| 14352 |
|
--- |
14352 |
|
--- |
| 14353 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
14353 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
| 14354 |
if (Pred) { |
0 |
14354 |
if (Pred) { |
0 |
| 14355 |
if (auto *U = dyn_cast(Pred)) { |
0 |
14355 |
if (auto *U = dyn_cast(Pred)) { |
0 |
| 14356 |
for (const auto *Pred : U->getPredicates()) |
0 |
14356 |
for (const auto *Pred : U->getPredicates()) |
0 |
| 14357 |
if (const auto *IPred = dyn_cast(Pred)) |
0 |
14357 |
if (const auto *IPred = dyn_cast(Pred)) |
0 |
| 14358 |
if (IPred->getLHS() == Expr && |
0 |
14358 |
if (IPred->getLHS() == Expr && |
0 |
| 14359 |
IPred->getPredicate() == ICmpInst::ICMP_EQ) |
0 |
14359 |
IPred->getPredicate() == ICmpInst::ICMP_EQ) |
0 |
| 14360 |
return IPred->getRHS(); |
0 |
14360 |
return IPred->getRHS(); |
0 |
| 14361 |
} else if (const auto *IPred = dyn_cast(Pred)) { |
0 |
14361 |
} else if (const auto *IPred = dyn_cast(Pred)) { |
0 |
| 14362 |
if (IPred->getLHS() == Expr && |
0 |
14362 |
if (IPred->getLHS() == Expr && |
0 |
| 14363 |
IPred->getPredicate() == ICmpInst::ICMP_EQ) |
0 |
14363 |
IPred->getPredicate() == ICmpInst::ICMP_EQ) |
0 |
| 14364 |
return IPred->getRHS(); |
0 |
14364 |
return IPred->getRHS(); |
0 |
| 14365 |
} |
--- |
14365 |
} |
--- |
| 14366 |
} |
--- |
14366 |
} |
--- |
| 14367 |
return convertToAddRecWithPreds(Expr); |
0 |
14367 |
return convertToAddRecWithPreds(Expr); |
0 |
| 14368 |
} |
--- |
14368 |
} |
--- |
| 14369 |
|
--- |
14369 |
|
--- |
| 14370 |
const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { |
0 |
14370 |
const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { |
0 |
| 14371 |
const SCEV *Operand = visit(Expr->getOperand()); |
0 |
14371 |
const SCEV *Operand = visit(Expr->getOperand()); |
0 |
| 14372 |
const SCEVAddRecExpr *AR = dyn_cast(Operand); |
0 |
14372 |
const SCEVAddRecExpr *AR = dyn_cast(Operand); |
0 |
| 14373 |
if (AR && AR->getLoop() == L && AR->isAffine()) { |
0 |
14373 |
if (AR && AR->getLoop() == L && AR->isAffine()) { |
0 |
| 14374 |
// This couldn't be folded because the operand didn't have the nuw |
--- |
14374 |
// This couldn't be folded because the operand didn't have the nuw |
--- |
| 14375 |
// flag. Add the nusw flag as an assumption that we could make. |
--- |
14375 |
// flag. Add the nusw flag as an assumption that we could make. |
--- |
| 14376 |
const SCEV *Step = AR->getStepRecurrence(SE); |
0 |
14376 |
const SCEV *Step = AR->getStepRecurrence(SE); |
0 |
| 14377 |
Type *Ty = Expr->getType(); |
0 |
14377 |
Type *Ty = Expr->getType(); |
0 |
| 14378 |
if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNUSW)) |
0 |
14378 |
if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNUSW)) |
0 |
| 14379 |
return SE.getAddRecExpr(SE.getZeroExtendExpr(AR->getStart(), Ty), |
0 |
14379 |
return SE.getAddRecExpr(SE.getZeroExtendExpr(AR->getStart(), Ty), |
0 |
| 14380 |
SE.getSignExtendExpr(Step, Ty), L, |
0 |
14380 |
SE.getSignExtendExpr(Step, Ty), L, |
0 |
| 14381 |
AR->getNoWrapFlags()); |
0 |
14381 |
AR->getNoWrapFlags()); |
0 |
| 14382 |
} |
--- |
14382 |
} |
--- |
| 14383 |
return SE.getZeroExtendExpr(Operand, Expr->getType()); |
0 |
14383 |
return SE.getZeroExtendExpr(Operand, Expr->getType()); |
0 |
| 14384 |
} |
--- |
14384 |
} |
--- |
| 14385 |
|
--- |
14385 |
|
--- |
| 14386 |
const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { |
0 |
14386 |
const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { |
0 |
| 14387 |
const SCEV *Operand = visit(Expr->getOperand()); |
0 |
14387 |
const SCEV *Operand = visit(Expr->getOperand()); |
0 |
| 14388 |
const SCEVAddRecExpr *AR = dyn_cast(Operand); |
0 |
14388 |
const SCEVAddRecExpr *AR = dyn_cast(Operand); |
0 |
| 14389 |
if (AR && AR->getLoop() == L && AR->isAffine()) { |
0 |
14389 |
if (AR && AR->getLoop() == L && AR->isAffine()) { |
0 |
| 14390 |
// This couldn't be folded because the operand didn't have the nsw |
--- |
14390 |
// This couldn't be folded because the operand didn't have the nsw |
--- |
| 14391 |
// flag. Add the nssw flag as an assumption that we could make. |
--- |
14391 |
// flag. Add the nssw flag as an assumption that we could make. |
--- |
| 14392 |
const SCEV *Step = AR->getStepRecurrence(SE); |
0 |
14392 |
const SCEV *Step = AR->getStepRecurrence(SE); |
0 |
| 14393 |
Type *Ty = Expr->getType(); |
0 |
14393 |
Type *Ty = Expr->getType(); |
0 |
| 14394 |
if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNSSW)) |
0 |
14394 |
if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNSSW)) |
0 |
| 14395 |
return SE.getAddRecExpr(SE.getSignExtendExpr(AR->getStart(), Ty), |
0 |
14395 |
return SE.getAddRecExpr(SE.getSignExtendExpr(AR->getStart(), Ty), |
0 |
| 14396 |
SE.getSignExtendExpr(Step, Ty), L, |
0 |
14396 |
SE.getSignExtendExpr(Step, Ty), L, |
0 |
| 14397 |
AR->getNoWrapFlags()); |
0 |
14397 |
AR->getNoWrapFlags()); |
0 |
| 14398 |
} |
--- |
14398 |
} |
--- |
| 14399 |
return SE.getSignExtendExpr(Operand, Expr->getType()); |
0 |
14399 |
return SE.getSignExtendExpr(Operand, Expr->getType()); |
0 |
| 14400 |
} |
--- |
14400 |
} |
--- |
| 14401 |
|
--- |
14401 |
|
--- |
| 14402 |
private: |
--- |
14402 |
private: |
--- |
| 14403 |
explicit SCEVPredicateRewriter(const Loop *L, ScalarEvolution &SE, |
0 |
14403 |
explicit SCEVPredicateRewriter(const Loop *L, ScalarEvolution &SE, |
0 |
| 14404 |
SmallPtrSetImpl *NewPreds, |
--- |
14404 |
SmallPtrSetImpl *NewPreds, |
--- |
| 14405 |
const SCEVPredicate *Pred) |
--- |
14405 |
const SCEVPredicate *Pred) |
--- |
| 14406 |
: SCEVRewriteVisitor(SE), NewPreds(NewPreds), Pred(Pred), L(L) {} |
0 |
14406 |
: SCEVRewriteVisitor(SE), NewPreds(NewPreds), Pred(Pred), L(L) {} |
0 |
| 14407 |
|
--- |
14407 |
|
--- |
| 14408 |
bool addOverflowAssumption(const SCEVPredicate *P) { |
0 |
14408 |
bool addOverflowAssumption(const SCEVPredicate *P) { |
0 |
| 14409 |
if (!NewPreds) { |
0 |
14409 |
if (!NewPreds) { |
0 |
| 14410 |
// Check if we've already made this assumption. |
--- |
14410 |
// Check if we've already made this assumption. |
--- |
| 14411 |
return Pred && Pred->implies(P); |
0 |
14411 |
return Pred && Pred->implies(P); |
0 |
| 14412 |
} |
--- |
14412 |
} |
--- |
| 14413 |
NewPreds->insert(P); |
0 |
14413 |
NewPreds->insert(P); |
0 |
| 14414 |
return true; |
0 |
14414 |
return true; |
0 |
| 14415 |
} |
--- |
14415 |
} |
--- |
| 14416 |
|
--- |
14416 |
|
--- |
| 14417 |
bool addOverflowAssumption(const SCEVAddRecExpr *AR, |
0 |
14417 |
bool addOverflowAssumption(const SCEVAddRecExpr *AR, |
0 |
| 14418 |
SCEVWrapPredicate::IncrementWrapFlags AddedFlags) { |
--- |
14418 |
SCEVWrapPredicate::IncrementWrapFlags AddedFlags) { |
--- |
| 14419 |
auto *A = SE.getWrapPredicate(AR, AddedFlags); |
0 |
14419 |
auto *A = SE.getWrapPredicate(AR, AddedFlags); |
0 |
| 14420 |
return addOverflowAssumption(A); |
0 |
14420 |
return addOverflowAssumption(A); |
0 |
| 14421 |
} |
--- |
14421 |
} |
--- |
| 14422 |
|
--- |
14422 |
|
--- |
| 14423 |
// If \p Expr represents a PHINode, we try to see if it can be represented |
--- |
14423 |
// If \p Expr represents a PHINode, we try to see if it can be represented |
--- |
| 14424 |
// as an AddRec, possibly under a predicate (PHISCEVPred). If it is possible |
--- |
14424 |
// as an AddRec, possibly under a predicate (PHISCEVPred). If it is possible |
--- |
| 14425 |
// to add this predicate as a runtime overflow check, we return the AddRec. |
--- |
14425 |
// to add this predicate as a runtime overflow check, we return the AddRec. |
--- |
| 14426 |
// If \p Expr does not meet these conditions (is not a PHI node, or we |
--- |
14426 |
// If \p Expr does not meet these conditions (is not a PHI node, or we |
--- |
| 14427 |
// couldn't create an AddRec for it, or couldn't add the predicate), we just |
--- |
14427 |
// couldn't create an AddRec for it, or couldn't add the predicate), we just |
--- |
| 14428 |
// return \p Expr. |
--- |
14428 |
// return \p Expr. |
--- |
| 14429 |
const SCEV *convertToAddRecWithPreds(const SCEVUnknown *Expr) { |
0 |
14429 |
const SCEV *convertToAddRecWithPreds(const SCEVUnknown *Expr) { |
0 |
| 14430 |
if (!isa(Expr->getValue())) |
0 |
14430 |
if (!isa(Expr->getValue())) |
0 |
| 14431 |
return Expr; |
0 |
14431 |
return Expr; |
0 |
| 14432 |
std::optional< |
--- |
14432 |
std::optional< |
--- |
| 14433 |
std::pair>> |
--- |
14433 |
std::pair>> |
--- |
| 14434 |
PredicatedRewrite = SE.createAddRecFromPHIWithCasts(Expr); |
0 |
14434 |
PredicatedRewrite = SE.createAddRecFromPHIWithCasts(Expr); |
0 |
| 14435 |
if (!PredicatedRewrite) |
0 |
14435 |
if (!PredicatedRewrite) |
0 |
| 14436 |
return Expr; |
0 |
14436 |
return Expr; |
0 |
| 14437 |
for (const auto *P : PredicatedRewrite->second){ |
0 |
14437 |
for (const auto *P : PredicatedRewrite->second){ |
0 |
| 14438 |
// Wrap predicates from outer loops are not supported. |
--- |
14438 |
// Wrap predicates from outer loops are not supported. |
--- |
| 14439 |
if (auto *WP = dyn_cast(P)) { |
0 |
14439 |
if (auto *WP = dyn_cast(P)) { |
0 |
| 14440 |
if (L != WP->getExpr()->getLoop()) |
0 |
14440 |
if (L != WP->getExpr()->getLoop()) |
0 |
| 14441 |
return Expr; |
0 |
14441 |
return Expr; |
0 |
| 14442 |
} |
--- |
14442 |
} |
--- |
| 14443 |
if (!addOverflowAssumption(P)) |
0 |
14443 |
if (!addOverflowAssumption(P)) |
0 |
| 14444 |
return Expr; |
0 |
14444 |
return Expr; |
0 |
| 14445 |
} |
--- |
14445 |
} |
--- |
| 14446 |
return PredicatedRewrite->first; |
0 |
14446 |
return PredicatedRewrite->first; |
0 |
| 14447 |
} |
0 |
14447 |
} |
0 |
| 14448 |
|
--- |
14448 |
|
--- |
| 14449 |
SmallPtrSetImpl *NewPreds; |
--- |
14449 |
SmallPtrSetImpl *NewPreds; |
--- |
| 14450 |
const SCEVPredicate *Pred; |
--- |
14450 |
const SCEVPredicate *Pred; |
--- |
| 14451 |
const Loop *L; |
--- |
14451 |
const Loop *L; |
--- |
| 14452 |
}; |
--- |
14452 |
}; |
--- |
| 14453 |
|
--- |
14453 |
|
--- |
| 14454 |
} // end anonymous namespace |
--- |
14454 |
} // end anonymous namespace |
--- |
| 14455 |
|
--- |
14455 |
|
--- |
| 14456 |
const SCEV * |
--- |
14456 |
const SCEV * |
--- |
| 14457 |
ScalarEvolution::rewriteUsingPredicate(const SCEV *S, const Loop *L, |
0 |
14457 |
ScalarEvolution::rewriteUsingPredicate(const SCEV *S, const Loop *L, |
0 |
| 14458 |
const SCEVPredicate &Preds) { |
--- |
14458 |
const SCEVPredicate &Preds) { |
--- |
| 14459 |
return SCEVPredicateRewriter::rewrite(S, L, *this, nullptr, &Preds); |
0 |
14459 |
return SCEVPredicateRewriter::rewrite(S, L, *this, nullptr, &Preds); |
0 |
| 14460 |
} |
--- |
14460 |
} |
--- |
| 14461 |
|
--- |
14461 |
|
--- |
| 14462 |
const SCEVAddRecExpr *ScalarEvolution::convertSCEVToAddRecWithPredicates( |
0 |
14462 |
const SCEVAddRecExpr *ScalarEvolution::convertSCEVToAddRecWithPredicates( |
0 |
| 14463 |
const SCEV *S, const Loop *L, |
--- |
14463 |
const SCEV *S, const Loop *L, |
--- |
| 14464 |
SmallPtrSetImpl &Preds) { |
--- |
14464 |
SmallPtrSetImpl &Preds) { |
--- |
| 14465 |
SmallPtrSet TransformPreds; |
0 |
14465 |
SmallPtrSet TransformPreds; |
0 |
| 14466 |
S = SCEVPredicateRewriter::rewrite(S, L, *this, &TransformPreds, nullptr); |
0 |
14466 |
S = SCEVPredicateRewriter::rewrite(S, L, *this, &TransformPreds, nullptr); |
0 |
| 14467 |
auto *AddRec = dyn_cast(S); |
0 |
14467 |
auto *AddRec = dyn_cast(S); |
0 |
| 14468 |
|
--- |
14468 |
|
--- |
| 14469 |
if (!AddRec) |
0 |
14469 |
if (!AddRec) |
0 |
| 14470 |
return nullptr; |
0 |
14470 |
return nullptr; |
0 |
| 14471 |
|
--- |
14471 |
|
--- |
| 14472 |
// Since the transformation was successful, we can now transfer the SCEV |
--- |
14472 |
// Since the transformation was successful, we can now transfer the SCEV |
--- |
| 14473 |
// predicates. |
--- |
14473 |
// predicates. |
--- |
| 14474 |
for (const auto *P : TransformPreds) |
0 |
14474 |
for (const auto *P : TransformPreds) |
0 |
| 14475 |
Preds.insert(P); |
0 |
14475 |
Preds.insert(P); |
0 |
| 14476 |
|
--- |
14476 |
|
--- |
| 14477 |
return AddRec; |
0 |
14477 |
return AddRec; |
0 |
| 14478 |
} |
0 |
14478 |
} |
0 |
| 14479 |
|
--- |
14479 |
|
--- |
| 14480 |
/// SCEV predicates |
--- |
14480 |
/// SCEV predicates |
--- |
| 14481 |
SCEVPredicate::SCEVPredicate(const FoldingSetNodeIDRef ID, |
0 |
14481 |
SCEVPredicate::SCEVPredicate(const FoldingSetNodeIDRef ID, |
0 |
| 14482 |
SCEVPredicateKind Kind) |
--- |
14482 |
SCEVPredicateKind Kind) |
--- |
| 14483 |
: FastID(ID), Kind(Kind) {} |
0 |
14483 |
: FastID(ID), Kind(Kind) {} |
0 |
| 14484 |
|
--- |
14484 |
|
--- |
| 14485 |
SCEVComparePredicate::SCEVComparePredicate(const FoldingSetNodeIDRef ID, |
0 |
14485 |
SCEVComparePredicate::SCEVComparePredicate(const FoldingSetNodeIDRef ID, |
0 |
| 14486 |
const ICmpInst::Predicate Pred, |
--- |
14486 |
const ICmpInst::Predicate Pred, |
--- |
| 14487 |
const SCEV *LHS, const SCEV *RHS) |
--- |
14487 |
const SCEV *LHS, const SCEV *RHS) |
--- |
| 14488 |
: SCEVPredicate(ID, P_Compare), Pred(Pred), LHS(LHS), RHS(RHS) { |
0 |
14488 |
: SCEVPredicate(ID, P_Compare), Pred(Pred), LHS(LHS), RHS(RHS) { |
0 |
| 14489 |
assert(LHS->getType() == RHS->getType() && "LHS and RHS types don't match"); |
0 |
14489 |
assert(LHS->getType() == RHS->getType() && "LHS and RHS types don't match"); |
0 |
| 14490 |
assert(LHS != RHS && "LHS and RHS are the same SCEV"); |
0 |
14490 |
assert(LHS != RHS && "LHS and RHS are the same SCEV"); |
0 |
| 14491 |
} |
0 |
14491 |
} |
0 |
| 14492 |
|
--- |
14492 |
|
--- |
| 14493 |
bool SCEVComparePredicate::implies(const SCEVPredicate *N) const { |
0 |
14493 |
bool SCEVComparePredicate::implies(const SCEVPredicate *N) const { |
0 |
| 14494 |
const auto *Op = dyn_cast(N); |
0 |
14494 |
const auto *Op = dyn_cast(N); |
0 |
| 14495 |
|
--- |
14495 |
|
--- |
| 14496 |
if (!Op) |
0 |
14496 |
if (!Op) |
0 |
| 14497 |
return false; |
0 |
14497 |
return false; |
0 |
| 14498 |
|
--- |
14498 |
|
--- |
| 14499 |
if (Pred != ICmpInst::ICMP_EQ) |
0 |
14499 |
if (Pred != ICmpInst::ICMP_EQ) |
0 |
| 14500 |
return false; |
0 |
14500 |
return false; |
0 |
| 14501 |
|
--- |
14501 |
|
--- |
| 14502 |
return Op->LHS == LHS && Op->RHS == RHS; |
0 |
14502 |
return Op->LHS == LHS && Op->RHS == RHS; |
0 |
| 14503 |
} |
--- |
14503 |
} |
--- |
| 14504 |
|
--- |
14504 |
|
--- |
| 14505 |
bool SCEVComparePredicate::isAlwaysTrue() const { return false; } |
0 |
14505 |
bool SCEVComparePredicate::isAlwaysTrue() const { return false; } |
0 |
| 14506 |
|
--- |
14506 |
|
--- |
| 14507 |
void SCEVComparePredicate::print(raw_ostream &OS, unsigned Depth) const { |
0 |
14507 |
void SCEVComparePredicate::print(raw_ostream &OS, unsigned Depth) const { |
0 |
| 14508 |
if (Pred == ICmpInst::ICMP_EQ) |
0 |
14508 |
if (Pred == ICmpInst::ICMP_EQ) |
0 |
| 14509 |
OS.indent(Depth) << "Equal predicate: " << *LHS << " == " << *RHS << "\n"; |
0 |
14509 |
OS.indent(Depth) << "Equal predicate: " << *LHS << " == " << *RHS << "\n"; |
0 |
| 14510 |
else |
--- |
14510 |
else |
--- |
| 14511 |
OS.indent(Depth) << "Compare predicate: " << *LHS << " " << Pred << ") " |
0 |
14511 |
OS.indent(Depth) << "Compare predicate: " << *LHS << " " << Pred << ") " |
0 |
| 14512 |
<< *RHS << "\n"; |
0 |
14512 |
<< *RHS << "\n"; |
0 |
| 14513 |
|
--- |
14513 |
|
--- |
| 14514 |
} |
0 |
14514 |
} |
0 |
| 14515 |
|
--- |
14515 |
|
--- |
| 14516 |
SCEVWrapPredicate::SCEVWrapPredicate(const FoldingSetNodeIDRef ID, |
0 |
14516 |
SCEVWrapPredicate::SCEVWrapPredicate(const FoldingSetNodeIDRef ID, |
0 |
| 14517 |
const SCEVAddRecExpr *AR, |
--- |
14517 |
const SCEVAddRecExpr *AR, |
--- |
| 14518 |
IncrementWrapFlags Flags) |
--- |
14518 |
IncrementWrapFlags Flags) |
--- |
| 14519 |
: SCEVPredicate(ID, P_Wrap), AR(AR), Flags(Flags) {} |
0 |
14519 |
: SCEVPredicate(ID, P_Wrap), AR(AR), Flags(Flags) {} |
0 |
| 14520 |
|
--- |
14520 |
|
--- |
| 14521 |
const SCEVAddRecExpr *SCEVWrapPredicate::getExpr() const { return AR; } |
0 |
14521 |
const SCEVAddRecExpr *SCEVWrapPredicate::getExpr() const { return AR; } |
0 |
| 14522 |
|
--- |
14522 |
|
--- |
| 14523 |
bool SCEVWrapPredicate::implies(const SCEVPredicate *N) const { |
0 |
14523 |
bool SCEVWrapPredicate::implies(const SCEVPredicate *N) const { |
0 |
| 14524 |
const auto *Op = dyn_cast(N); |
0 |
14524 |
const auto *Op = dyn_cast(N); |
0 |
| 14525 |
|
--- |
14525 |
|
--- |
| 14526 |
return Op && Op->AR == AR && setFlags(Flags, Op->Flags) == Flags; |
0 |
14526 |
return Op && Op->AR == AR && setFlags(Flags, Op->Flags) == Flags; |
0 |
| 14527 |
} |
--- |
14527 |
} |
--- |
| 14528 |
|
--- |
14528 |
|
--- |
| 14529 |
bool SCEVWrapPredicate::isAlwaysTrue() const { |
0 |
14529 |
bool SCEVWrapPredicate::isAlwaysTrue() const { |
0 |
| 14530 |
SCEV::NoWrapFlags ScevFlags = AR->getNoWrapFlags(); |
0 |
14530 |
SCEV::NoWrapFlags ScevFlags = AR->getNoWrapFlags(); |
0 |
| 14531 |
IncrementWrapFlags IFlags = Flags; |
0 |
14531 |
IncrementWrapFlags IFlags = Flags; |
0 |
| 14532 |
|
--- |
14532 |
|
--- |
| 14533 |
if (ScalarEvolution::setFlags(ScevFlags, SCEV::FlagNSW) == ScevFlags) |
0 |
14533 |
if (ScalarEvolution::setFlags(ScevFlags, SCEV::FlagNSW) == ScevFlags) |
0 |
| 14534 |
IFlags = clearFlags(IFlags, IncrementNSSW); |
0 |
14534 |
IFlags = clearFlags(IFlags, IncrementNSSW); |
0 |
| 14535 |
|
--- |
14535 |
|
--- |
| 14536 |
return IFlags == IncrementAnyWrap; |
0 |
14536 |
return IFlags == IncrementAnyWrap; |
0 |
| 14537 |
} |
--- |
14537 |
} |
--- |
| 14538 |
|
--- |
14538 |
|
--- |
| 14539 |
void SCEVWrapPredicate::print(raw_ostream &OS, unsigned Depth) const { |
0 |
14539 |
void SCEVWrapPredicate::print(raw_ostream &OS, unsigned Depth) const { |
0 |
| 14540 |
OS.indent(Depth) << *getExpr() << " Added Flags: "; |
0 |
14540 |
OS.indent(Depth) << *getExpr() << " Added Flags: "; |
0 |
| 14541 |
if (SCEVWrapPredicate::IncrementNUSW & getFlags()) |
0 |
14541 |
if (SCEVWrapPredicate::IncrementNUSW & getFlags()) |
0 |
| 14542 |
OS << ""; |
0 |
14542 |
OS << ""; |
0 |
| 14543 |
if (SCEVWrapPredicate::IncrementNSSW & getFlags()) |
0 |
14543 |
if (SCEVWrapPredicate::IncrementNSSW & getFlags()) |
0 |
| 14544 |
OS << ""; |
0 |
14544 |
OS << ""; |
0 |
| 14545 |
OS << "\n"; |
0 |
14545 |
OS << "\n"; |
0 |
| 14546 |
} |
0 |
14546 |
} |
0 |
| 14547 |
|
--- |
14547 |
|
--- |
| 14548 |
SCEVWrapPredicate::IncrementWrapFlags |
--- |
14548 |
SCEVWrapPredicate::IncrementWrapFlags |
--- |
| 14549 |
SCEVWrapPredicate::getImpliedFlags(const SCEVAddRecExpr *AR, |
0 |
14549 |
SCEVWrapPredicate::getImpliedFlags(const SCEVAddRecExpr *AR, |
0 |
| 14550 |
ScalarEvolution &SE) { |
--- |
14550 |
ScalarEvolution &SE) { |
--- |
| 14551 |
IncrementWrapFlags ImpliedFlags = IncrementAnyWrap; |
0 |
14551 |
IncrementWrapFlags ImpliedFlags = IncrementAnyWrap; |
0 |
| 14552 |
SCEV::NoWrapFlags StaticFlags = AR->getNoWrapFlags(); |
0 |
14552 |
SCEV::NoWrapFlags StaticFlags = AR->getNoWrapFlags(); |
0 |
| 14553 |
|
--- |
14553 |
|
--- |
| 14554 |
// We can safely transfer the NSW flag as NSSW. |
--- |
14554 |
// We can safely transfer the NSW flag as NSSW. |
--- |
| 14555 |
if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNSW) == StaticFlags) |
0 |
14555 |
if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNSW) == StaticFlags) |
0 |
| 14556 |
ImpliedFlags = IncrementNSSW; |
0 |
14556 |
ImpliedFlags = IncrementNSSW; |
0 |
| 14557 |
|
--- |
14557 |
|
--- |
| 14558 |
if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNUW) == StaticFlags) { |
0 |
14558 |
if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNUW) == StaticFlags) { |
0 |
| 14559 |
// If the increment is positive, the SCEV NUW flag will also imply the |
--- |
14559 |
// If the increment is positive, the SCEV NUW flag will also imply the |
--- |
| 14560 |
// WrapPredicate NUSW flag. |
--- |
14560 |
// WrapPredicate NUSW flag. |
--- |
| 14561 |
if (const auto *Step = dyn_cast(AR->getStepRecurrence(SE))) |
0 |
14561 |
if (const auto *Step = dyn_cast(AR->getStepRecurrence(SE))) |
0 |
| 14562 |
if (Step->getValue()->getValue().isNonNegative()) |
0 |
14562 |
if (Step->getValue()->getValue().isNonNegative()) |
0 |
| 14563 |
ImpliedFlags = setFlags(ImpliedFlags, IncrementNUSW); |
0 |
14563 |
ImpliedFlags = setFlags(ImpliedFlags, IncrementNUSW); |
0 |
| 14564 |
} |
--- |
14564 |
} |
--- |
| 14565 |
|
--- |
14565 |
|
--- |
| 14566 |
return ImpliedFlags; |
0 |
14566 |
return ImpliedFlags; |
0 |
| 14567 |
} |
--- |
14567 |
} |
--- |
| 14568 |
|
--- |
14568 |
|
--- |
| 14569 |
/// Union predicates don't get cached so create a dummy set ID for it. |
--- |
14569 |
/// Union predicates don't get cached so create a dummy set ID for it. |
--- |
| 14570 |
SCEVUnionPredicate::SCEVUnionPredicate(ArrayRef Preds) |
0 |
14570 |
SCEVUnionPredicate::SCEVUnionPredicate(ArrayRef Preds) |
0 |
| 14571 |
: SCEVPredicate(FoldingSetNodeIDRef(nullptr, 0), P_Union) { |
0 |
14571 |
: SCEVPredicate(FoldingSetNodeIDRef(nullptr, 0), P_Union) { |
0 |
| 14572 |
for (const auto *P : Preds) |
0 |
14572 |
for (const auto *P : Preds) |
0 |
| 14573 |
add(P); |
0 |
14573 |
add(P); |
0 |
| 14574 |
} |
0 |
14574 |
} |
0 |
| 14575 |
|
--- |
14575 |
|
--- |
| 14576 |
bool SCEVUnionPredicate::isAlwaysTrue() const { |
0 |
14576 |
bool SCEVUnionPredicate::isAlwaysTrue() const { |
0 |
| 14577 |
return all_of(Preds, |
0 |
14577 |
return all_of(Preds, |
0 |
| 14578 |
[](const SCEVPredicate *I) { return I->isAlwaysTrue(); }); |
0 |
14578 |
[](const SCEVPredicate *I) { return I->isAlwaysTrue(); }); |
0 |
| 14579 |
} |
--- |
14579 |
} |
--- |
| 14580 |
|
--- |
14580 |
|
--- |
| 14581 |
bool SCEVUnionPredicate::implies(const SCEVPredicate *N) const { |
0 |
14581 |
bool SCEVUnionPredicate::implies(const SCEVPredicate *N) const { |
0 |
| 14582 |
if (const auto *Set = dyn_cast(N)) |
0 |
14582 |
if (const auto *Set = dyn_cast(N)) |
0 |
| 14583 |
return all_of(Set->Preds, |
0 |
14583 |
return all_of(Set->Preds, |
0 |
| 14584 |
[this](const SCEVPredicate *I) { return this->implies(I); }); |
0 |
14584 |
[this](const SCEVPredicate *I) { return this->implies(I); }); |
0 |
| 14585 |
|
--- |
14585 |
|
--- |
| 14586 |
return any_of(Preds, |
0 |
14586 |
return any_of(Preds, |
0 |
| 14587 |
[N](const SCEVPredicate *I) { return I->implies(N); }); |
0 |
14587 |
[N](const SCEVPredicate *I) { return I->implies(N); }); |
0 |
| 14588 |
} |
--- |
14588 |
} |
--- |
| 14589 |
|
--- |
14589 |
|
--- |
| 14590 |
void SCEVUnionPredicate::print(raw_ostream &OS, unsigned Depth) const { |
0 |
14590 |
void SCEVUnionPredicate::print(raw_ostream &OS, unsigned Depth) const { |
0 |
| 14591 |
for (const auto *Pred : Preds) |
0 |
14591 |
for (const auto *Pred : Preds) |
0 |
| 14592 |
Pred->print(OS, Depth); |
0 |
14592 |
Pred->print(OS, Depth); |
0 |
| 14593 |
} |
0 |
14593 |
} |
0 |
| 14594 |
|
--- |
14594 |
|
--- |
| 14595 |
void SCEVUnionPredicate::add(const SCEVPredicate *N) { |
0 |
14595 |
void SCEVUnionPredicate::add(const SCEVPredicate *N) { |
0 |
| 14596 |
if (const auto *Set = dyn_cast(N)) { |
0 |
14596 |
if (const auto *Set = dyn_cast(N)) { |
0 |
| 14597 |
for (const auto *Pred : Set->Preds) |
0 |
14597 |
for (const auto *Pred : Set->Preds) |
0 |
| 14598 |
add(Pred); |
0 |
14598 |
add(Pred); |
0 |
| 14599 |
return; |
0 |
14599 |
return; |
0 |
| 14600 |
} |
--- |
14600 |
} |
--- |
| 14601 |
|
--- |
14601 |
|
--- |
| 14602 |
Preds.push_back(N); |
0 |
14602 |
Preds.push_back(N); |
0 |
| 14603 |
} |
--- |
14603 |
} |
--- |
| 14604 |
|
--- |
14604 |
|
--- |
| 14605 |
PredicatedScalarEvolution::PredicatedScalarEvolution(ScalarEvolution &SE, |
0 |
14605 |
PredicatedScalarEvolution::PredicatedScalarEvolution(ScalarEvolution &SE, |
0 |
| 14606 |
Loop &L) |
--- |
14606 |
Loop &L) |
--- |
| 14607 |
: SE(SE), L(L) { |
0 |
14607 |
: SE(SE), L(L) { |
0 |
| 14608 |
SmallVector Empty; |
0 |
14608 |
SmallVector Empty; |
0 |
| 14609 |
Preds = std::make_unique(Empty); |
0 |
14609 |
Preds = std::make_unique(Empty); |
0 |
| 14610 |
} |
0 |
14610 |
} |
0 |
| 14611 |
|
--- |
14611 |
|
--- |
| 14612 |
void ScalarEvolution::registerUser(const SCEV *User, |
0 |
14612 |
void ScalarEvolution::registerUser(const SCEV *User, |
0 |
| 14613 |
ArrayRef Ops) { |
--- |
14613 |
ArrayRef Ops) { |
--- |
| 14614 |
for (const auto *Op : Ops) |
0 |
14614 |
for (const auto *Op : Ops) |
0 |
| 14615 |
// We do not expect that forgetting cached data for SCEVConstants will ever |
--- |
14615 |
// We do not expect that forgetting cached data for SCEVConstants will ever |
--- |
| 14616 |
// open any prospects for sharpening or introduce any correctness issues, |
--- |
14616 |
// open any prospects for sharpening or introduce any correctness issues, |
--- |
| 14617 |
// so we don't bother storing their dependencies. |
--- |
14617 |
// so we don't bother storing their dependencies. |
--- |
| 14618 |
if (!isa(Op)) |
0 |
14618 |
if (!isa(Op)) |
0 |
| 14619 |
SCEVUsers[Op].insert(User); |
0 |
14619 |
SCEVUsers[Op].insert(User); |
0 |
| 14620 |
} |
0 |
14620 |
} |
0 |
| 14621 |
|
--- |
14621 |
|
--- |
| 14622 |
const SCEV *PredicatedScalarEvolution::getSCEV(Value *V) { |
0 |
14622 |
const SCEV *PredicatedScalarEvolution::getSCEV(Value *V) { |
0 |
| 14623 |
const SCEV *Expr = SE.getSCEV(V); |
0 |
14623 |
const SCEV *Expr = SE.getSCEV(V); |
0 |
| 14624 |
RewriteEntry &Entry = RewriteMap[Expr]; |
0 |
14624 |
RewriteEntry &Entry = RewriteMap[Expr]; |
0 |
| 14625 |
|
--- |
14625 |
|
--- |
| 14626 |
// If we already have an entry and the version matches, return it. |
--- |
14626 |
// If we already have an entry and the version matches, return it. |
--- |
| 14627 |
if (Entry.second && Generation == Entry.first) |
0 |
14627 |
if (Entry.second && Generation == Entry.first) |
0 |
| 14628 |
return Entry.second; |
0 |
14628 |
return Entry.second; |
0 |
| 14629 |
|
--- |
14629 |
|
--- |
| 14630 |
// We found an entry but it's stale. Rewrite the stale entry |
--- |
14630 |
// We found an entry but it's stale. Rewrite the stale entry |
--- |
| 14631 |
// according to the current predicate. |
--- |
14631 |
// according to the current predicate. |
--- |
| 14632 |
if (Entry.second) |
0 |
14632 |
if (Entry.second) |
0 |
| 14633 |
Expr = Entry.second; |
0 |
14633 |
Expr = Entry.second; |
0 |
| 14634 |
|
--- |
14634 |
|
--- |
| 14635 |
const SCEV *NewSCEV = SE.rewriteUsingPredicate(Expr, &L, *Preds); |
0 |
14635 |
const SCEV *NewSCEV = SE.rewriteUsingPredicate(Expr, &L, *Preds); |
0 |
| 14636 |
Entry = {Generation, NewSCEV}; |
0 |
14636 |
Entry = {Generation, NewSCEV}; |
0 |
| 14637 |
|
--- |
14637 |
|
--- |
| 14638 |
return NewSCEV; |
0 |
14638 |
return NewSCEV; |
0 |
| 14639 |
} |
--- |
14639 |
} |
--- |
| 14640 |
|
--- |
14640 |
|
--- |
| 14641 |
const SCEV *PredicatedScalarEvolution::getBackedgeTakenCount() { |
0 |
14641 |
const SCEV *PredicatedScalarEvolution::getBackedgeTakenCount() { |
0 |
| 14642 |
if (!BackedgeCount) { |
0 |
14642 |
if (!BackedgeCount) { |
0 |
| 14643 |
SmallVector Preds; |
0 |
14643 |
SmallVector Preds; |
0 |
| 14644 |
BackedgeCount = SE.getPredicatedBackedgeTakenCount(&L, Preds); |
0 |
14644 |
BackedgeCount = SE.getPredicatedBackedgeTakenCount(&L, Preds); |
0 |
| 14645 |
for (const auto *P : Preds) |
0 |
14645 |
for (const auto *P : Preds) |
0 |
| 14646 |
addPredicate(*P); |
0 |
14646 |
addPredicate(*P); |
0 |
| 14647 |
} |
0 |
14647 |
} |
0 |
| 14648 |
return BackedgeCount; |
0 |
14648 |
return BackedgeCount; |
0 |
| 14649 |
} |
--- |
14649 |
} |
--- |
| 14650 |
|
--- |
14650 |
|
--- |
| 14651 |
void PredicatedScalarEvolution::addPredicate(const SCEVPredicate &Pred) { |
0 |
14651 |
void PredicatedScalarEvolution::addPredicate(const SCEVPredicate &Pred) { |
0 |
| 14652 |
if (Preds->implies(&Pred)) |
0 |
14652 |
if (Preds->implies(&Pred)) |
0 |
| 14653 |
return; |
0 |
14653 |
return; |
0 |
| 14654 |
|
--- |
14654 |
|
--- |
| 14655 |
auto &OldPreds = Preds->getPredicates(); |
0 |
14655 |
auto &OldPreds = Preds->getPredicates(); |
0 |
| 14656 |
SmallVector NewPreds(OldPreds.begin(), OldPreds.end()); |
0 |
14656 |
SmallVector NewPreds(OldPreds.begin(), OldPreds.end()); |
0 |
| 14657 |
NewPreds.push_back(&Pred); |
0 |
14657 |
NewPreds.push_back(&Pred); |
0 |
| 14658 |
Preds = std::make_unique(NewPreds); |
0 |
14658 |
Preds = std::make_unique(NewPreds); |
0 |
| 14659 |
updateGeneration(); |
0 |
14659 |
updateGeneration(); |
0 |
| 14660 |
} |
0 |
14660 |
} |
0 |
| 14661 |
|
--- |
14661 |
|
--- |
| 14662 |
const SCEVPredicate &PredicatedScalarEvolution::getPredicate() const { |
0 |
14662 |
const SCEVPredicate &PredicatedScalarEvolution::getPredicate() const { |
0 |
| 14663 |
return *Preds; |
0 |
14663 |
return *Preds; |
0 |
| 14664 |
} |
--- |
14664 |
} |
--- |
| 14665 |
|
--- |
14665 |
|
--- |
| 14666 |
void PredicatedScalarEvolution::updateGeneration() { |
0 |
14666 |
void PredicatedScalarEvolution::updateGeneration() { |
0 |
| 14667 |
// If the generation number wrapped recompute everything. |
--- |
14667 |
// If the generation number wrapped recompute everything. |
--- |
| 14668 |
if (++Generation == 0) { |
0 |
14668 |
if (++Generation == 0) { |
0 |
| 14669 |
for (auto &II : RewriteMap) { |
0 |
14669 |
for (auto &II : RewriteMap) { |
0 |
| 14670 |
const SCEV *Rewritten = II.second.second; |
0 |
14670 |
const SCEV *Rewritten = II.second.second; |
0 |
| 14671 |
II.second = {Generation, SE.rewriteUsingPredicate(Rewritten, &L, *Preds)}; |
0 |
14671 |
II.second = {Generation, SE.rewriteUsingPredicate(Rewritten, &L, *Preds)}; |
0 |
| 14672 |
} |
--- |
14672 |
} |
--- |
| 14673 |
} |
--- |
14673 |
} |
--- |
| 14674 |
} |
0 |
14674 |
} |
0 |
| 14675 |
|
--- |
14675 |
|
--- |
| 14676 |
void PredicatedScalarEvolution::setNoOverflow( |
0 |
14676 |
void PredicatedScalarEvolution::setNoOverflow( |
0 |
| 14677 |
Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) { |
--- |
14677 |
Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) { |
--- |
| 14678 |
const SCEV *Expr = getSCEV(V); |
0 |
14678 |
const SCEV *Expr = getSCEV(V); |
0 |
| 14679 |
const auto *AR = cast(Expr); |
0 |
14679 |
const auto *AR = cast(Expr); |
0 |
| 14680 |
|
--- |
14680 |
|
--- |
| 14681 |
auto ImpliedFlags = SCEVWrapPredicate::getImpliedFlags(AR, SE); |
0 |
14681 |
auto ImpliedFlags = SCEVWrapPredicate::getImpliedFlags(AR, SE); |
0 |
| 14682 |
|
--- |
14682 |
|
--- |
| 14683 |
// Clear the statically implied flags. |
--- |
14683 |
// Clear the statically implied flags. |
--- |
| 14684 |
Flags = SCEVWrapPredicate::clearFlags(Flags, ImpliedFlags); |
0 |
14684 |
Flags = SCEVWrapPredicate::clearFlags(Flags, ImpliedFlags); |
0 |
| 14685 |
addPredicate(*SE.getWrapPredicate(AR, Flags)); |
0 |
14685 |
addPredicate(*SE.getWrapPredicate(AR, Flags)); |
0 |
| 14686 |
|
--- |
14686 |
|
--- |
| 14687 |
auto II = FlagsMap.insert({V, Flags}); |
0 |
14687 |
auto II = FlagsMap.insert({V, Flags}); |
0 |
| 14688 |
if (!II.second) |
0 |
14688 |
if (!II.second) |
0 |
| 14689 |
II.first->second = SCEVWrapPredicate::setFlags(Flags, II.first->second); |
0 |
14689 |
II.first->second = SCEVWrapPredicate::setFlags(Flags, II.first->second); |
0 |
| 14690 |
} |
0 |
14690 |
} |
0 |
| 14691 |
|
--- |
14691 |
|
--- |
| 14692 |
bool PredicatedScalarEvolution::hasNoOverflow( |
0 |
14692 |
bool PredicatedScalarEvolution::hasNoOverflow( |
0 |
| 14693 |
Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) { |
--- |
14693 |
Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) { |
--- |
| 14694 |
const SCEV *Expr = getSCEV(V); |
0 |
14694 |
const SCEV *Expr = getSCEV(V); |
0 |
| 14695 |
const auto *AR = cast(Expr); |
0 |
14695 |
const auto *AR = cast(Expr); |
0 |
| 14696 |
|
--- |
14696 |
|
--- |
| 14697 |
Flags = SCEVWrapPredicate::clearFlags( |
0 |
14697 |
Flags = SCEVWrapPredicate::clearFlags( |
0 |
| 14698 |
Flags, SCEVWrapPredicate::getImpliedFlags(AR, SE)); |
--- |
14698 |
Flags, SCEVWrapPredicate::getImpliedFlags(AR, SE)); |
--- |
| 14699 |
|
--- |
14699 |
|
--- |
| 14700 |
auto II = FlagsMap.find(V); |
0 |
14700 |
auto II = FlagsMap.find(V); |
0 |
| 14701 |
|
--- |
14701 |
|
--- |
| 14702 |
if (II != FlagsMap.end()) |
0 |
14702 |
if (II != FlagsMap.end()) |
0 |
| 14703 |
Flags = SCEVWrapPredicate::clearFlags(Flags, II->second); |
0 |
14703 |
Flags = SCEVWrapPredicate::clearFlags(Flags, II->second); |
0 |
| 14704 |
|
--- |
14704 |
|
--- |
| 14705 |
return Flags == SCEVWrapPredicate::IncrementAnyWrap; |
0 |
14705 |
return Flags == SCEVWrapPredicate::IncrementAnyWrap; |
0 |
| 14706 |
} |
--- |
14706 |
} |
--- |
| 14707 |
|
--- |
14707 |
|
--- |
| 14708 |
const SCEVAddRecExpr *PredicatedScalarEvolution::getAsAddRec(Value *V) { |
0 |
14708 |
const SCEVAddRecExpr *PredicatedScalarEvolution::getAsAddRec(Value *V) { |
0 |
| 14709 |
const SCEV *Expr = this->getSCEV(V); |
0 |
14709 |
const SCEV *Expr = this->getSCEV(V); |
0 |
| 14710 |
SmallPtrSet NewPreds; |
0 |
14710 |
SmallPtrSet NewPreds; |
0 |
| 14711 |
auto *New = SE.convertSCEVToAddRecWithPredicates(Expr, &L, NewPreds); |
0 |
14711 |
auto *New = SE.convertSCEVToAddRecWithPredicates(Expr, &L, NewPreds); |
0 |
| 14712 |
|
--- |
14712 |
|
--- |
| 14713 |
if (!New) |
0 |
14713 |
if (!New) |
0 |
| 14714 |
return nullptr; |
0 |
14714 |
return nullptr; |
0 |
| 14715 |
|
--- |
14715 |
|
--- |
| 14716 |
for (const auto *P : NewPreds) |
0 |
14716 |
for (const auto *P : NewPreds) |
0 |
| 14717 |
addPredicate(*P); |
0 |
14717 |
addPredicate(*P); |
0 |
| 14718 |
|
--- |
14718 |
|
--- |
| 14719 |
RewriteMap[SE.getSCEV(V)] = {Generation, New}; |
0 |
14719 |
RewriteMap[SE.getSCEV(V)] = {Generation, New}; |
0 |
| 14720 |
return New; |
0 |
14720 |
return New; |
0 |
| 14721 |
} |
0 |
14721 |
} |
0 |
| 14722 |
|
--- |
14722 |
|
--- |
| 14723 |
PredicatedScalarEvolution::PredicatedScalarEvolution( |
0 |
14723 |
PredicatedScalarEvolution::PredicatedScalarEvolution( |
0 |
| 14724 |
const PredicatedScalarEvolution &Init) |
--- |
14724 |
const PredicatedScalarEvolution &Init) |
--- |
| 14725 |
: RewriteMap(Init.RewriteMap), SE(Init.SE), L(Init.L), |
0 |
14725 |
: RewriteMap(Init.RewriteMap), SE(Init.SE), L(Init.L), |
0 |
| 14726 |
Preds(std::make_unique(Init.Preds->getPredicates())), |
0 |
14726 |
Preds(std::make_unique(Init.Preds->getPredicates())), |
0 |
| 14727 |
Generation(Init.Generation), BackedgeCount(Init.BackedgeCount) { |
0 |
14727 |
Generation(Init.Generation), BackedgeCount(Init.BackedgeCount) { |
0 |
| 14728 |
for (auto I : Init.FlagsMap) |
0 |
14728 |
for (auto I : Init.FlagsMap) |
0 |
| 14729 |
FlagsMap.insert(I); |
0 |
14729 |
FlagsMap.insert(I); |
0 |
| 14730 |
} |
0 |
14730 |
} |
0 |
| 14731 |
|
--- |
14731 |
|
--- |
| 14732 |
void PredicatedScalarEvolution::print(raw_ostream &OS, unsigned Depth) const { |
0 |
14732 |
void PredicatedScalarEvolution::print(raw_ostream &OS, unsigned Depth) const { |
0 |
| 14733 |
// For each block. |
--- |
14733 |
// For each block. |
--- |
| 14734 |
for (auto *BB : L.getBlocks()) |
0 |
14734 |
for (auto *BB : L.getBlocks()) |
0 |
| 14735 |
for (auto &I : *BB) { |
0 |
14735 |
for (auto &I : *BB) { |
0 |
| 14736 |
if (!SE.isSCEVable(I.getType())) |
0 |
14736 |
if (!SE.isSCEVable(I.getType())) |
0 |
| 14737 |
continue; |
0 |
14737 |
continue; |
0 |
| 14738 |
|
--- |
14738 |
|
--- |
| 14739 |
auto *Expr = SE.getSCEV(&I); |
0 |
14739 |
auto *Expr = SE.getSCEV(&I); |
0 |
| 14740 |
auto II = RewriteMap.find(Expr); |
0 |
14740 |
auto II = RewriteMap.find(Expr); |
0 |
| 14741 |
|
--- |
14741 |
|
--- |
| 14742 |
if (II == RewriteMap.end()) |
0 |
14742 |
if (II == RewriteMap.end()) |
0 |
| 14743 |
continue; |
0 |
14743 |
continue; |
0 |
| 14744 |
|
--- |
14744 |
|
--- |
| 14745 |
// Don't print things that are not interesting. |
--- |
14745 |
// Don't print things that are not interesting. |
--- |
| 14746 |
if (II->second.second == Expr) |
0 |
14746 |
if (II->second.second == Expr) |
0 |
| 14747 |
continue; |
0 |
14747 |
continue; |
0 |
| 14748 |
|
--- |
14748 |
|
--- |
| 14749 |
OS.indent(Depth) << "[PSE]" << I << ":\n"; |
0 |
14749 |
OS.indent(Depth) << "[PSE]" << I << ":\n"; |
0 |
| 14750 |
OS.indent(Depth + 2) << *Expr << "\n"; |
0 |
14750 |
OS.indent(Depth + 2) << *Expr << "\n"; |
0 |
| 14751 |
OS.indent(Depth + 2) << "--> " << *II->second.second << "\n"; |
0 |
14751 |
OS.indent(Depth + 2) << "--> " << *II->second.second << "\n"; |
0 |
| 14752 |
} |
--- |
14752 |
} |
--- |
| 14753 |
} |
0 |
14753 |
} |
0 |
| 14754 |
|
--- |
14754 |
|
--- |
| 14755 |
// Match the mathematical pattern A - (A / B) * B, where A and B can be |
--- |
14755 |
// Match the mathematical pattern A - (A / B) * B, where A and B can be |
--- |
| 14756 |
// arbitrary expressions. Also match zext (trunc A to iB) to iY, which is used |
--- |
14756 |
// arbitrary expressions. Also match zext (trunc A to iB) to iY, which is used |
--- |
| 14757 |
// for URem with constant power-of-2 second operands. |
--- |
14757 |
// for URem with constant power-of-2 second operands. |
--- |
| 14758 |
// It's not always easy, as A and B can be folded (imagine A is X / 2, and B is |
--- |
14758 |
// It's not always easy, as A and B can be folded (imagine A is X / 2, and B is |
--- |
| 14759 |
// 4, A / B becomes X / 8). |
--- |
14759 |
// 4, A / B becomes X / 8). |
--- |
| 14760 |
bool ScalarEvolution::matchURem(const SCEV *Expr, const SCEV *&LHS, |
0 |
14760 |
bool ScalarEvolution::matchURem(const SCEV *Expr, const SCEV *&LHS, |
0 |
| 14761 |
const SCEV *&RHS) { |
--- |
14761 |
const SCEV *&RHS) { |
--- |
| 14762 |
// Try to match 'zext (trunc A to iB) to iY', which is used |
--- |
14762 |
// Try to match 'zext (trunc A to iB) to iY', which is used |
--- |
| 14763 |
// for URem with constant power-of-2 second operands. Make sure the size of |
--- |
14763 |
// for URem with constant power-of-2 second operands. Make sure the size of |
--- |
| 14764 |
// the operand A matches the size of the whole expressions. |
--- |
14764 |
// the operand A matches the size of the whole expressions. |
--- |
| 14765 |
if (const auto *ZExt = dyn_cast(Expr)) |
0 |
14765 |
if (const auto *ZExt = dyn_cast(Expr)) |
0 |
| 14766 |
if (const auto *Trunc = dyn_cast(ZExt->getOperand(0))) { |
0 |
14766 |
if (const auto *Trunc = dyn_cast(ZExt->getOperand(0))) { |
0 |
| 14767 |
LHS = Trunc->getOperand(); |
0 |
14767 |
LHS = Trunc->getOperand(); |
0 |
| 14768 |
// Bail out if the type of the LHS is larger than the type of the |
--- |
14768 |
// Bail out if the type of the LHS is larger than the type of the |
--- |
| 14769 |
// expression for now. |
--- |
14769 |
// expression for now. |
--- |
| 14770 |
if (getTypeSizeInBits(LHS->getType()) > |
0 |
14770 |
if (getTypeSizeInBits(LHS->getType()) > |
0 |
| 14771 |
getTypeSizeInBits(Expr->getType())) |
0 |
14771 |
getTypeSizeInBits(Expr->getType())) |
0 |
| 14772 |
return false; |
0 |
14772 |
return false; |
0 |
| 14773 |
if (LHS->getType() != Expr->getType()) |
0 |
14773 |
if (LHS->getType() != Expr->getType()) |
0 |
| 14774 |
LHS = getZeroExtendExpr(LHS, Expr->getType()); |
0 |
14774 |
LHS = getZeroExtendExpr(LHS, Expr->getType()); |
0 |
| 14775 |
RHS = getConstant(APInt(getTypeSizeInBits(Expr->getType()), 1) |
0 |
14775 |
RHS = getConstant(APInt(getTypeSizeInBits(Expr->getType()), 1) |
0 |
| 14776 |
<< getTypeSizeInBits(Trunc->getType())); |
0 |
14776 |
<< getTypeSizeInBits(Trunc->getType())); |
0 |
| 14777 |
return true; |
0 |
14777 |
return true; |
0 |
| 14778 |
} |
--- |
14778 |
} |
--- |
| 14779 |
const auto *Add = dyn_cast(Expr); |
0 |
14779 |
const auto *Add = dyn_cast(Expr); |
0 |
| 14780 |
if (Add == nullptr || Add->getNumOperands() != 2) |
0 |
14780 |
if (Add == nullptr || Add->getNumOperands() != 2) |
0 |
| 14781 |
return false; |
0 |
14781 |
return false; |
0 |
| 14782 |
|
--- |
14782 |
|
--- |
| 14783 |
const SCEV *A = Add->getOperand(1); |
0 |
14783 |
const SCEV *A = Add->getOperand(1); |
0 |
| 14784 |
const auto *Mul = dyn_cast(Add->getOperand(0)); |
0 |
14784 |
const auto *Mul = dyn_cast(Add->getOperand(0)); |
0 |
| 14785 |
|
--- |
14785 |
|
--- |
| 14786 |
if (Mul == nullptr) |
0 |
14786 |
if (Mul == nullptr) |
0 |
| 14787 |
return false; |
0 |
14787 |
return false; |
0 |
| 14788 |
|
--- |
14788 |
|
--- |
| 14789 |
const auto MatchURemWithDivisor = [&](const SCEV *B) { |
0 |
14789 |
const auto MatchURemWithDivisor = [&](const SCEV *B) { |
0 |
| 14790 |
// (SomeExpr + (-(SomeExpr / B) * B)). |
--- |
14790 |
// (SomeExpr + (-(SomeExpr / B) * B)). |
--- |
| 14791 |
if (Expr == getURemExpr(A, B)) { |
0 |
14791 |
if (Expr == getURemExpr(A, B)) { |
0 |
| 14792 |
LHS = A; |
0 |
14792 |
LHS = A; |
0 |
| 14793 |
RHS = B; |
0 |
14793 |
RHS = B; |
0 |
| 14794 |
return true; |
0 |
14794 |
return true; |
0 |
| 14795 |
} |
--- |
14795 |
} |
--- |
| 14796 |
return false; |
0 |
14796 |
return false; |
0 |
| 14797 |
}; |
0 |
14797 |
}; |
0 |
| 14798 |
|
--- |
14798 |
|
--- |
| 14799 |
// (SomeExpr + (-1 * (SomeExpr / B) * B)). |
--- |
14799 |
// (SomeExpr + (-1 * (SomeExpr / B) * B)). |
--- |
| 14800 |
if (Mul->getNumOperands() == 3 && isa(Mul->getOperand(0))) |
0 |
14800 |
if (Mul->getNumOperands() == 3 && isa(Mul->getOperand(0))) |
0 |
| 14801 |
return MatchURemWithDivisor(Mul->getOperand(1)) || |
0 |
14801 |
return MatchURemWithDivisor(Mul->getOperand(1)) || |
0 |
| 14802 |
MatchURemWithDivisor(Mul->getOperand(2)); |
0 |
14802 |
MatchURemWithDivisor(Mul->getOperand(2)); |
0 |
| 14803 |
|
--- |
14803 |
|
--- |
| 14804 |
// (SomeExpr + ((-SomeExpr / B) * B)) or (SomeExpr + ((SomeExpr / B) * -B)). |
--- |
14804 |
// (SomeExpr + ((-SomeExpr / B) * B)) or (SomeExpr + ((SomeExpr / B) * -B)). |
--- |
| 14805 |
if (Mul->getNumOperands() == 2) |
0 |
14805 |
if (Mul->getNumOperands() == 2) |
0 |
| 14806 |
return MatchURemWithDivisor(Mul->getOperand(1)) || |
0 |
14806 |
return MatchURemWithDivisor(Mul->getOperand(1)) || |
0 |
| 14807 |
MatchURemWithDivisor(Mul->getOperand(0)) || |
0 |
14807 |
MatchURemWithDivisor(Mul->getOperand(0)) || |
0 |
| 14808 |
MatchURemWithDivisor(getNegativeSCEV(Mul->getOperand(1))) || |
0 |
14808 |
MatchURemWithDivisor(getNegativeSCEV(Mul->getOperand(1))) || |
0 |
| 14809 |
MatchURemWithDivisor(getNegativeSCEV(Mul->getOperand(0))); |
0 |
14809 |
MatchURemWithDivisor(getNegativeSCEV(Mul->getOperand(0))); |
0 |
| 14810 |
return false; |
0 |
14810 |
return false; |
0 |
| 14811 |
} |
--- |
14811 |
} |
--- |
| 14812 |
|
--- |
14812 |
|
--- |
| 14813 |
const SCEV * |
--- |
14813 |
const SCEV * |
--- |
| 14814 |
ScalarEvolution::computeSymbolicMaxBackedgeTakenCount(const Loop *L) { |
0 |
14814 |
ScalarEvolution::computeSymbolicMaxBackedgeTakenCount(const Loop *L) { |
0 |
| 14815 |
SmallVector ExitingBlocks; |
0 |
14815 |
SmallVector ExitingBlocks; |
0 |
| 14816 |
L->getExitingBlocks(ExitingBlocks); |
0 |
14816 |
L->getExitingBlocks(ExitingBlocks); |
0 |
| 14817 |
|
--- |
14817 |
|
--- |
| 14818 |
// Form an expression for the maximum exit count possible for this loop. We |
--- |
14818 |
// Form an expression for the maximum exit count possible for this loop. We |
--- |
| 14819 |
// merge the max and exact information to approximate a version of |
--- |
14819 |
// merge the max and exact information to approximate a version of |
--- |
| 14820 |
// getConstantMaxBackedgeTakenCount which isn't restricted to just constants. |
--- |
14820 |
// getConstantMaxBackedgeTakenCount which isn't restricted to just constants. |
--- |
| 14821 |
SmallVector ExitCounts; |
0 |
14821 |
SmallVector ExitCounts; |
0 |
| 14822 |
for (BasicBlock *ExitingBB : ExitingBlocks) { |
0 |
14822 |
for (BasicBlock *ExitingBB : ExitingBlocks) { |
0 |
| 14823 |
const SCEV *ExitCount = |
--- |
14823 |
const SCEV *ExitCount = |
--- |
| 14824 |
getExitCount(L, ExitingBB, ScalarEvolution::SymbolicMaximum); |
0 |
14824 |
getExitCount(L, ExitingBB, ScalarEvolution::SymbolicMaximum); |
0 |
| 14825 |
if (!isa(ExitCount)) { |
0 |
14825 |
if (!isa(ExitCount)) { |
0 |
| 14826 |
assert(DT.dominates(ExitingBB, L->getLoopLatch()) && |
0 |
14826 |
assert(DT.dominates(ExitingBB, L->getLoopLatch()) && |
0 |
| 14827 |
"We should only have known counts for exiting blocks that " |
--- |
14827 |
"We should only have known counts for exiting blocks that " |
--- |
| 14828 |
"dominate latch!"); |
--- |
14828 |
"dominate latch!"); |
--- |
| 14829 |
ExitCounts.push_back(ExitCount); |
0 |
14829 |
ExitCounts.push_back(ExitCount); |
0 |
| 14830 |
} |
--- |
14830 |
} |
--- |
| 14831 |
} |
--- |
14831 |
} |
--- |
| 14832 |
if (ExitCounts.empty()) |
0 |
14832 |
if (ExitCounts.empty()) |
0 |
| 14833 |
return getCouldNotCompute(); |
0 |
14833 |
return getCouldNotCompute(); |
0 |
| 14834 |
return getUMinFromMismatchedTypes(ExitCounts, /*Sequential*/ true); |
0 |
14834 |
return getUMinFromMismatchedTypes(ExitCounts, /*Sequential*/ true); |
0 |
| 14835 |
} |
0 |
14835 |
} |
0 |
| 14836 |
|
--- |
14836 |
|
--- |
| 14837 |
/// A rewriter to replace SCEV expressions in Map with the corresponding entry |
--- |
14837 |
/// A rewriter to replace SCEV expressions in Map with the corresponding entry |
--- |
| 14838 |
/// in the map. It skips AddRecExpr because we cannot guarantee that the |
--- |
14838 |
/// in the map. It skips AddRecExpr because we cannot guarantee that the |
--- |
| 14839 |
/// replacement is loop invariant in the loop of the AddRec. |
--- |
14839 |
/// replacement is loop invariant in the loop of the AddRec. |
--- |
| 14840 |
class SCEVLoopGuardRewriter : public SCEVRewriteVisitor { |
--- |
14840 |
class SCEVLoopGuardRewriter : public SCEVRewriteVisitor { |
--- |
| 14841 |
const DenseMap ⤅ |
--- |
14841 |
const DenseMap ⤅ |
--- |
| 14842 |
|
--- |
14842 |
|
--- |
| 14843 |
public: |
--- |
14843 |
public: |
--- |
| 14844 |
SCEVLoopGuardRewriter(ScalarEvolution &SE, |
0 |
14844 |
SCEVLoopGuardRewriter(ScalarEvolution &SE, |
0 |
| 14845 |
DenseMap &M) |
--- |
14845 |
DenseMap &M) |
--- |
| 14846 |
: SCEVRewriteVisitor(SE), Map(M) {} |
0 |
14846 |
: SCEVRewriteVisitor(SE), Map(M) {} |
0 |
| 14847 |
|
--- |
14847 |
|
--- |
| 14848 |
const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { return Expr; } |
0 |
14848 |
const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { return Expr; } |
0 |
| 14849 |
|
--- |
14849 |
|
--- |
| 14850 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
14850 |
const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
0 |
| 14851 |
auto I = Map.find(Expr); |
0 |
14851 |
auto I = Map.find(Expr); |
0 |
| 14852 |
if (I == Map.end()) |
0 |
14852 |
if (I == Map.end()) |
0 |
| 14853 |
return Expr; |
0 |
14853 |
return Expr; |
0 |
| 14854 |
return I->second; |
0 |
14854 |
return I->second; |
0 |
| 14855 |
} |
--- |
14855 |
} |
--- |
| 14856 |
|
--- |
14856 |
|
--- |
| 14857 |
const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { |
0 |
14857 |
const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { |
0 |
| 14858 |
auto I = Map.find(Expr); |
0 |
14858 |
auto I = Map.find(Expr); |
0 |
| 14859 |
if (I == Map.end()) { |
0 |
14859 |
if (I == Map.end()) { |
0 |
| 14860 |
// If we didn't find the extact ZExt expr in the map, check if there's an |
--- |
14860 |
// If we didn't find the extact ZExt expr in the map, check if there's an |
--- |
| 14861 |
// entry for a smaller ZExt we can use instead. |
--- |
14861 |
// entry for a smaller ZExt we can use instead. |
--- |
| 14862 |
Type *Ty = Expr->getType(); |
0 |
14862 |
Type *Ty = Expr->getType(); |
0 |
| 14863 |
const SCEV *Op = Expr->getOperand(0); |
0 |
14863 |
const SCEV *Op = Expr->getOperand(0); |
0 |
| 14864 |
unsigned Bitwidth = Ty->getScalarSizeInBits() / 2; |
0 |
14864 |
unsigned Bitwidth = Ty->getScalarSizeInBits() / 2; |
0 |
| 14865 |
while (Bitwidth % 8 == 0 && Bitwidth >= 8 && |
0 |
14865 |
while (Bitwidth % 8 == 0 && Bitwidth >= 8 && |
0 |
| 14866 |
Bitwidth > Op->getType()->getScalarSizeInBits()) { |
0 |
14866 |
Bitwidth > Op->getType()->getScalarSizeInBits()) { |
0 |
| 14867 |
Type *NarrowTy = IntegerType::get(SE.getContext(), Bitwidth); |
0 |
14867 |
Type *NarrowTy = IntegerType::get(SE.getContext(), Bitwidth); |
0 |
| 14868 |
auto *NarrowExt = SE.getZeroExtendExpr(Op, NarrowTy); |
0 |
14868 |
auto *NarrowExt = SE.getZeroExtendExpr(Op, NarrowTy); |
0 |
| 14869 |
auto I = Map.find(NarrowExt); |
0 |
14869 |
auto I = Map.find(NarrowExt); |
0 |
| 14870 |
if (I != Map.end()) |
0 |
14870 |
if (I != Map.end()) |
0 |
| 14871 |
return SE.getZeroExtendExpr(I->second, Ty); |
0 |
14871 |
return SE.getZeroExtendExpr(I->second, Ty); |
0 |
| 14872 |
Bitwidth = Bitwidth / 2; |
0 |
14872 |
Bitwidth = Bitwidth / 2; |
0 |
| 14873 |
} |
--- |
14873 |
} |
--- |
| 14874 |
|
--- |
14874 |
|
--- |
| 14875 |
return SCEVRewriteVisitor::visitZeroExtendExpr( |
0 |
14875 |
return SCEVRewriteVisitor::visitZeroExtendExpr( |
0 |
| 14876 |
Expr); |
0 |
14876 |
Expr); |
0 |
| 14877 |
} |
--- |
14877 |
} |
--- |
| 14878 |
return I->second; |
0 |
14878 |
return I->second; |
0 |
| 14879 |
} |
--- |
14879 |
} |
--- |
| 14880 |
|
--- |
14880 |
|
--- |
| 14881 |
const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { |
0 |
14881 |
const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { |
0 |
| 14882 |
auto I = Map.find(Expr); |
0 |
14882 |
auto I = Map.find(Expr); |
0 |
| 14883 |
if (I == Map.end()) |
0 |
14883 |
if (I == Map.end()) |
0 |
| 14884 |
return SCEVRewriteVisitor::visitSignExtendExpr( |
0 |
14884 |
return SCEVRewriteVisitor::visitSignExtendExpr( |
0 |
| 14885 |
Expr); |
0 |
14885 |
Expr); |
0 |
| 14886 |
return I->second; |
0 |
14886 |
return I->second; |
0 |
| 14887 |
} |
--- |
14887 |
} |
--- |
| 14888 |
|
--- |
14888 |
|
--- |
| 14889 |
const SCEV *visitUMinExpr(const SCEVUMinExpr *Expr) { |
0 |
14889 |
const SCEV *visitUMinExpr(const SCEVUMinExpr *Expr) { |
0 |
| 14890 |
auto I = Map.find(Expr); |
0 |
14890 |
auto I = Map.find(Expr); |
0 |
| 14891 |
if (I == Map.end()) |
0 |
14891 |
if (I == Map.end()) |
0 |
| 14892 |
return SCEVRewriteVisitor::visitUMinExpr(Expr); |
0 |
14892 |
return SCEVRewriteVisitor::visitUMinExpr(Expr); |
0 |
| 14893 |
return I->second; |
0 |
14893 |
return I->second; |
0 |
| 14894 |
} |
--- |
14894 |
} |
--- |
| 14895 |
|
--- |
14895 |
|
--- |
| 14896 |
const SCEV *visitSMinExpr(const SCEVSMinExpr *Expr) { |
0 |
14896 |
const SCEV *visitSMinExpr(const SCEVSMinExpr *Expr) { |
0 |
| 14897 |
auto I = Map.find(Expr); |
0 |
14897 |
auto I = Map.find(Expr); |
0 |
| 14898 |
if (I == Map.end()) |
0 |
14898 |
if (I == Map.end()) |
0 |
| 14899 |
return SCEVRewriteVisitor::visitSMinExpr(Expr); |
0 |
14899 |
return SCEVRewriteVisitor::visitSMinExpr(Expr); |
0 |
| 14900 |
return I->second; |
0 |
14900 |
return I->second; |
0 |
| 14901 |
} |
--- |
14901 |
} |
--- |
| 14902 |
}; |
--- |
14902 |
}; |
--- |
| 14903 |
|
--- |
14903 |
|
--- |
| 14904 |
const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) { |
0 |
14904 |
const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) { |
0 |
| 14905 |
SmallVector ExprsToRewrite; |
0 |
14905 |
SmallVector ExprsToRewrite; |
0 |
| 14906 |
auto CollectCondition = [&](ICmpInst::Predicate Predicate, const SCEV *LHS, |
0 |
14906 |
auto CollectCondition = [&](ICmpInst::Predicate Predicate, const SCEV *LHS, |
0 |
| 14907 |
const SCEV *RHS, |
--- |
14907 |
const SCEV *RHS, |
--- |
| 14908 |
DenseMap |
--- |
14908 |
DenseMap |
--- |
| 14909 |
&RewriteMap) { |
--- |
14909 |
&RewriteMap) { |
--- |
| 14910 |
// WARNING: It is generally unsound to apply any wrap flags to the proposed |
--- |
14910 |
// WARNING: It is generally unsound to apply any wrap flags to the proposed |
--- |
| 14911 |
// replacement SCEV which isn't directly implied by the structure of that |
--- |
14911 |
// replacement SCEV which isn't directly implied by the structure of that |
--- |
| 14912 |
// SCEV. In particular, using contextual facts to imply flags is *NOT* |
--- |
14912 |
// SCEV. In particular, using contextual facts to imply flags is *NOT* |
--- |
| 14913 |
// legal. See the scoping rules for flags in the header to understand why. |
--- |
14913 |
// legal. See the scoping rules for flags in the header to understand why. |
--- |
| 14914 |
|
--- |
14914 |
|
--- |
| 14915 |
// If LHS is a constant, apply information to the other expression. |
--- |
14915 |
// If LHS is a constant, apply information to the other expression. |
--- |
| 14916 |
if (isa(LHS)) { |
0 |
14916 |
if (isa(LHS)) { |
0 |
| 14917 |
std::swap(LHS, RHS); |
0 |
14917 |
std::swap(LHS, RHS); |
0 |
| 14918 |
Predicate = CmpInst::getSwappedPredicate(Predicate); |
0 |
14918 |
Predicate = CmpInst::getSwappedPredicate(Predicate); |
0 |
| 14919 |
} |
--- |
14919 |
} |
--- |
| 14920 |
|
--- |
14920 |
|
--- |
| 14921 |
// Check for a condition of the form (-C1 + X < C2). InstCombine will |
--- |
14921 |
// Check for a condition of the form (-C1 + X < C2). InstCombine will |
--- |
| 14922 |
// create this form when combining two checks of the form (X u< C2 + C1) and |
--- |
14922 |
// create this form when combining two checks of the form (X u< C2 + C1) and |
--- |
| 14923 |
// (X >=u C1). |
--- |
14923 |
// (X >=u C1). |
--- |
| 14924 |
auto MatchRangeCheckIdiom = [this, Predicate, LHS, RHS, &RewriteMap, |
0 |
14924 |
auto MatchRangeCheckIdiom = [this, Predicate, LHS, RHS, &RewriteMap, |
0 |
| 14925 |
&ExprsToRewrite]() { |
0 |
14925 |
&ExprsToRewrite]() { |
0 |
| 14926 |
auto *AddExpr = dyn_cast(LHS); |
0 |
14926 |
auto *AddExpr = dyn_cast(LHS); |
0 |
| 14927 |
if (!AddExpr || AddExpr->getNumOperands() != 2) |
0 |
14927 |
if (!AddExpr || AddExpr->getNumOperands() != 2) |
0 |
| 14928 |
return false; |
0 |
14928 |
return false; |
0 |
| 14929 |
|
--- |
14929 |
|
--- |
| 14930 |
auto *C1 = dyn_cast(AddExpr->getOperand(0)); |
0 |
14930 |
auto *C1 = dyn_cast(AddExpr->getOperand(0)); |
0 |
| 14931 |
auto *LHSUnknown = dyn_cast(AddExpr->getOperand(1)); |
0 |
14931 |
auto *LHSUnknown = dyn_cast(AddExpr->getOperand(1)); |
0 |
| 14932 |
auto *C2 = dyn_cast(RHS); |
0 |
14932 |
auto *C2 = dyn_cast(RHS); |
0 |
| 14933 |
if (!C1 || !C2 || !LHSUnknown) |
0 |
14933 |
if (!C1 || !C2 || !LHSUnknown) |
0 |
| 14934 |
return false; |
0 |
14934 |
return false; |
0 |
| 14935 |
|
--- |
14935 |
|
--- |
| 14936 |
auto ExactRegion = |
--- |
14936 |
auto ExactRegion = |
--- |
| 14937 |
ConstantRange::makeExactICmpRegion(Predicate, C2->getAPInt()) |
0 |
14937 |
ConstantRange::makeExactICmpRegion(Predicate, C2->getAPInt()) |
0 |
| 14938 |
.sub(C1->getAPInt()); |
0 |
14938 |
.sub(C1->getAPInt()); |
0 |
| 14939 |
|
--- |
14939 |
|
--- |
| 14940 |
// Bail out, unless we have a non-wrapping, monotonic range. |
--- |
14940 |
// Bail out, unless we have a non-wrapping, monotonic range. |
--- |
| 14941 |
if (ExactRegion.isWrappedSet() || ExactRegion.isFullSet()) |
0 |
14941 |
if (ExactRegion.isWrappedSet() || ExactRegion.isFullSet()) |
0 |
| 14942 |
return false; |
0 |
14942 |
return false; |
0 |
| 14943 |
auto I = RewriteMap.find(LHSUnknown); |
0 |
14943 |
auto I = RewriteMap.find(LHSUnknown); |
0 |
| 14944 |
const SCEV *RewrittenLHS = I != RewriteMap.end() ? I->second : LHSUnknown; |
0 |
14944 |
const SCEV *RewrittenLHS = I != RewriteMap.end() ? I->second : LHSUnknown; |
0 |
| 14945 |
RewriteMap[LHSUnknown] = getUMaxExpr( |
0 |
14945 |
RewriteMap[LHSUnknown] = getUMaxExpr( |
0 |
| 14946 |
getConstant(ExactRegion.getUnsignedMin()), |
0 |
14946 |
getConstant(ExactRegion.getUnsignedMin()), |
0 |
| 14947 |
getUMinExpr(RewrittenLHS, getConstant(ExactRegion.getUnsignedMax()))); |
0 |
14947 |
getUMinExpr(RewrittenLHS, getConstant(ExactRegion.getUnsignedMax()))); |
0 |
| 14948 |
ExprsToRewrite.push_back(LHSUnknown); |
0 |
14948 |
ExprsToRewrite.push_back(LHSUnknown); |
0 |
| 14949 |
return true; |
0 |
14949 |
return true; |
0 |
| 14950 |
}; |
0 |
14950 |
}; |
0 |
| 14951 |
if (MatchRangeCheckIdiom()) |
0 |
14951 |
if (MatchRangeCheckIdiom()) |
0 |
| 14952 |
return; |
0 |
14952 |
return; |
0 |
| 14953 |
|
--- |
14953 |
|
--- |
| 14954 |
// Return true if \p Expr is a MinMax SCEV expression with a non-negative |
--- |
14954 |
// Return true if \p Expr is a MinMax SCEV expression with a non-negative |
--- |
| 14955 |
// constant operand. If so, return in \p SCTy the SCEV type and in \p RHS |
--- |
14955 |
// constant operand. If so, return in \p SCTy the SCEV type and in \p RHS |
--- |
| 14956 |
// the non-constant operand and in \p LHS the constant operand. |
--- |
14956 |
// the non-constant operand and in \p LHS the constant operand. |
--- |
| 14957 |
auto IsMinMaxSCEVWithNonNegativeConstant = |
--- |
14957 |
auto IsMinMaxSCEVWithNonNegativeConstant = |
--- |
| 14958 |
[&](const SCEV *Expr, SCEVTypes &SCTy, const SCEV *&LHS, |
0 |
14958 |
[&](const SCEV *Expr, SCEVTypes &SCTy, const SCEV *&LHS, |
0 |
| 14959 |
const SCEV *&RHS) { |
--- |
14959 |
const SCEV *&RHS) { |
--- |
| 14960 |
if (auto *MinMax = dyn_cast(Expr)) { |
0 |
14960 |
if (auto *MinMax = dyn_cast(Expr)) { |
0 |
| 14961 |
if (MinMax->getNumOperands() != 2) |
0 |
14961 |
if (MinMax->getNumOperands() != 2) |
0 |
| 14962 |
return false; |
0 |
14962 |
return false; |
0 |
| 14963 |
if (auto *C = dyn_cast(MinMax->getOperand(0))) { |
0 |
14963 |
if (auto *C = dyn_cast(MinMax->getOperand(0))) { |
0 |
| 14964 |
if (C->getAPInt().isNegative()) |
0 |
14964 |
if (C->getAPInt().isNegative()) |
0 |
| 14965 |
return false; |
0 |
14965 |
return false; |
0 |
| 14966 |
SCTy = MinMax->getSCEVType(); |
0 |
14966 |
SCTy = MinMax->getSCEVType(); |
0 |
| 14967 |
LHS = MinMax->getOperand(0); |
0 |
14967 |
LHS = MinMax->getOperand(0); |
0 |
| 14968 |
RHS = MinMax->getOperand(1); |
0 |
14968 |
RHS = MinMax->getOperand(1); |
0 |
| 14969 |
return true; |
0 |
14969 |
return true; |
0 |
| 14970 |
} |
--- |
14970 |
} |
--- |
| 14971 |
} |
--- |
14971 |
} |
--- |
| 14972 |
return false; |
0 |
14972 |
return false; |
0 |
| 14973 |
}; |
--- |
14973 |
}; |
--- |
| 14974 |
|
--- |
14974 |
|
--- |
| 14975 |
// Checks whether Expr is a non-negative constant, and Divisor is a positive |
--- |
14975 |
// Checks whether Expr is a non-negative constant, and Divisor is a positive |
--- |
| 14976 |
// constant, and returns their APInt in ExprVal and in DivisorVal. |
--- |
14976 |
// constant, and returns their APInt in ExprVal and in DivisorVal. |
--- |
| 14977 |
auto GetNonNegExprAndPosDivisor = [&](const SCEV *Expr, const SCEV *Divisor, |
0 |
14977 |
auto GetNonNegExprAndPosDivisor = [&](const SCEV *Expr, const SCEV *Divisor, |
0 |
| 14978 |
APInt &ExprVal, APInt &DivisorVal) { |
--- |
14978 |
APInt &ExprVal, APInt &DivisorVal) { |
--- |
| 14979 |
auto *ConstExpr = dyn_cast(Expr); |
0 |
14979 |
auto *ConstExpr = dyn_cast(Expr); |
0 |
| 14980 |
auto *ConstDivisor = dyn_cast(Divisor); |
0 |
14980 |
auto *ConstDivisor = dyn_cast(Divisor); |
0 |
| 14981 |
if (!ConstExpr || !ConstDivisor) |
0 |
14981 |
if (!ConstExpr || !ConstDivisor) |
0 |
| 14982 |
return false; |
0 |
14982 |
return false; |
0 |
| 14983 |
ExprVal = ConstExpr->getAPInt(); |
0 |
14983 |
ExprVal = ConstExpr->getAPInt(); |
0 |
| 14984 |
DivisorVal = ConstDivisor->getAPInt(); |
0 |
14984 |
DivisorVal = ConstDivisor->getAPInt(); |
0 |
| 14985 |
return ExprVal.isNonNegative() && !DivisorVal.isNonPositive(); |
0 |
14985 |
return ExprVal.isNonNegative() && !DivisorVal.isNonPositive(); |
0 |
| 14986 |
}; |
--- |
14986 |
}; |
--- |
| 14987 |
|
--- |
14987 |
|
--- |
| 14988 |
// Return a new SCEV that modifies \p Expr to the closest number divides by |
--- |
14988 |
// Return a new SCEV that modifies \p Expr to the closest number divides by |
--- |
| 14989 |
// \p Divisor and greater or equal than Expr. |
--- |
14989 |
// \p Divisor and greater or equal than Expr. |
--- |
| 14990 |
// For now, only handle constant Expr and Divisor. |
--- |
14990 |
// For now, only handle constant Expr and Divisor. |
--- |
| 14991 |
auto GetNextSCEVDividesByDivisor = [&](const SCEV *Expr, |
0 |
14991 |
auto GetNextSCEVDividesByDivisor = [&](const SCEV *Expr, |
0 |
| 14992 |
const SCEV *Divisor) { |
--- |
14992 |
const SCEV *Divisor) { |
--- |
| 14993 |
APInt ExprVal; |
0 |
14993 |
APInt ExprVal; |
0 |
| 14994 |
APInt DivisorVal; |
0 |
14994 |
APInt DivisorVal; |
0 |
| 14995 |
if (!GetNonNegExprAndPosDivisor(Expr, Divisor, ExprVal, DivisorVal)) |
0 |
14995 |
if (!GetNonNegExprAndPosDivisor(Expr, Divisor, ExprVal, DivisorVal)) |
0 |
| 14996 |
return Expr; |
0 |
14996 |
return Expr; |
0 |
| 14997 |
APInt Rem = ExprVal.urem(DivisorVal); |
0 |
14997 |
APInt Rem = ExprVal.urem(DivisorVal); |
0 |
| 14998 |
if (!Rem.isZero()) |
0 |
14998 |
if (!Rem.isZero()) |
0 |
| 14999 |
// return the SCEV: Expr + Divisor - Expr % Divisor |
--- |
14999 |
// return the SCEV: Expr + Divisor - Expr % Divisor |
--- |
| 15000 |
return getConstant(ExprVal + DivisorVal - Rem); |
0 |
15000 |
return getConstant(ExprVal + DivisorVal - Rem); |
0 |
| 15001 |
return Expr; |
0 |
15001 |
return Expr; |
0 |
| 15002 |
}; |
0 |
15002 |
}; |
0 |
| 15003 |
|
--- |
15003 |
|
--- |
| 15004 |
// Return a new SCEV that modifies \p Expr to the closest number divides by |
--- |
15004 |
// Return a new SCEV that modifies \p Expr to the closest number divides by |
--- |
| 15005 |
// \p Divisor and less or equal than Expr. |
--- |
15005 |
// \p Divisor and less or equal than Expr. |
--- |
| 15006 |
// For now, only handle constant Expr and Divisor. |
--- |
15006 |
// For now, only handle constant Expr and Divisor. |
--- |
| 15007 |
auto GetPreviousSCEVDividesByDivisor = [&](const SCEV *Expr, |
0 |
15007 |
auto GetPreviousSCEVDividesByDivisor = [&](const SCEV *Expr, |
0 |
| 15008 |
const SCEV *Divisor) { |
--- |
15008 |
const SCEV *Divisor) { |
--- |
| 15009 |
APInt ExprVal; |
0 |
15009 |
APInt ExprVal; |
0 |
| 15010 |
APInt DivisorVal; |
0 |
15010 |
APInt DivisorVal; |
0 |
| 15011 |
if (!GetNonNegExprAndPosDivisor(Expr, Divisor, ExprVal, DivisorVal)) |
0 |
15011 |
if (!GetNonNegExprAndPosDivisor(Expr, Divisor, ExprVal, DivisorVal)) |
0 |
| 15012 |
return Expr; |
0 |
15012 |
return Expr; |
0 |
| 15013 |
APInt Rem = ExprVal.urem(DivisorVal); |
0 |
15013 |
APInt Rem = ExprVal.urem(DivisorVal); |
0 |
| 15014 |
// return the SCEV: Expr - Expr % Divisor |
--- |
15014 |
// return the SCEV: Expr - Expr % Divisor |
--- |
| 15015 |
return getConstant(ExprVal - Rem); |
0 |
15015 |
return getConstant(ExprVal - Rem); |
0 |
| 15016 |
}; |
0 |
15016 |
}; |
0 |
| 15017 |
|
--- |
15017 |
|
--- |
| 15018 |
// Apply divisibilty by \p Divisor on MinMaxExpr with constant values, |
--- |
15018 |
// Apply divisibilty by \p Divisor on MinMaxExpr with constant values, |
--- |
| 15019 |
// recursively. This is done by aligning up/down the constant value to the |
--- |
15019 |
// recursively. This is done by aligning up/down the constant value to the |
--- |
| 15020 |
// Divisor. |
--- |
15020 |
// Divisor. |
--- |
| 15021 |
std::function |
--- |
15021 |
std::function |
--- |
| 15022 |
ApplyDivisibiltyOnMinMaxExpr = [&](const SCEV *MinMaxExpr, |
0 |
15022 |
ApplyDivisibiltyOnMinMaxExpr = [&](const SCEV *MinMaxExpr, |
0 |
| 15023 |
const SCEV *Divisor) { |
--- |
15023 |
const SCEV *Divisor) { |
--- |
| 15024 |
const SCEV *MinMaxLHS = nullptr, *MinMaxRHS = nullptr; |
0 |
15024 |
const SCEV *MinMaxLHS = nullptr, *MinMaxRHS = nullptr; |
0 |
| 15025 |
SCEVTypes SCTy; |
--- |
15025 |
SCEVTypes SCTy; |
--- |
| 15026 |
if (!IsMinMaxSCEVWithNonNegativeConstant(MinMaxExpr, SCTy, MinMaxLHS, |
0 |
15026 |
if (!IsMinMaxSCEVWithNonNegativeConstant(MinMaxExpr, SCTy, MinMaxLHS, |
0 |
| 15027 |
MinMaxRHS)) |
--- |
15027 |
MinMaxRHS)) |
--- |
| 15028 |
return MinMaxExpr; |
0 |
15028 |
return MinMaxExpr; |
0 |
| 15029 |
auto IsMin = |
--- |
15029 |
auto IsMin = |
--- |
| 15030 |
isa(MinMaxExpr) || isa(MinMaxExpr); |
0 |
15030 |
isa(MinMaxExpr) || isa(MinMaxExpr); |
0 |
| 15031 |
assert(isKnownNonNegative(MinMaxLHS) && |
0 |
15031 |
assert(isKnownNonNegative(MinMaxLHS) && |
0 |
| 15032 |
"Expected non-negative operand!"); |
--- |
15032 |
"Expected non-negative operand!"); |
--- |
| 15033 |
auto *DivisibleExpr = |
--- |
15033 |
auto *DivisibleExpr = |
--- |
| 15034 |
IsMin ? GetPreviousSCEVDividesByDivisor(MinMaxLHS, Divisor) |
0 |
15034 |
IsMin ? GetPreviousSCEVDividesByDivisor(MinMaxLHS, Divisor) |
0 |
| 15035 |
: GetNextSCEVDividesByDivisor(MinMaxLHS, Divisor); |
0 |
15035 |
: GetNextSCEVDividesByDivisor(MinMaxLHS, Divisor); |
0 |
| 15036 |
SmallVector Ops = { |
--- |
15036 |
SmallVector Ops = { |
--- |
| 15037 |
ApplyDivisibiltyOnMinMaxExpr(MinMaxRHS, Divisor), DivisibleExpr}; |
0 |
15037 |
ApplyDivisibiltyOnMinMaxExpr(MinMaxRHS, Divisor), DivisibleExpr}; |
0 |
| 15038 |
return getMinMaxExpr(SCTy, Ops); |
0 |
15038 |
return getMinMaxExpr(SCTy, Ops); |
0 |
| 15039 |
}; |
0 |
15039 |
}; |
0 |
| 15040 |
|
--- |
15040 |
|
--- |
| 15041 |
// If we have LHS == 0, check if LHS is computing a property of some unknown |
--- |
15041 |
// If we have LHS == 0, check if LHS is computing a property of some unknown |
--- |
| 15042 |
// SCEV %v which we can rewrite %v to express explicitly. |
--- |
15042 |
// SCEV %v which we can rewrite %v to express explicitly. |
--- |
| 15043 |
const SCEVConstant *RHSC = dyn_cast(RHS); |
0 |
15043 |
const SCEVConstant *RHSC = dyn_cast(RHS); |
0 |
| 15044 |
if (Predicate == CmpInst::ICMP_EQ && RHSC && |
0 |
15044 |
if (Predicate == CmpInst::ICMP_EQ && RHSC && |
0 |
| 15045 |
RHSC->getValue()->isNullValue()) { |
0 |
15045 |
RHSC->getValue()->isNullValue()) { |
0 |
| 15046 |
// If LHS is A % B, i.e. A % B == 0, rewrite A to (A /u B) * B to |
--- |
15046 |
// If LHS is A % B, i.e. A % B == 0, rewrite A to (A /u B) * B to |
--- |
| 15047 |
// explicitly express that. |
--- |
15047 |
// explicitly express that. |
--- |
| 15048 |
const SCEV *URemLHS = nullptr; |
0 |
15048 |
const SCEV *URemLHS = nullptr; |
0 |
| 15049 |
const SCEV *URemRHS = nullptr; |
0 |
15049 |
const SCEV *URemRHS = nullptr; |
0 |
| 15050 |
if (matchURem(LHS, URemLHS, URemRHS)) { |
0 |
15050 |
if (matchURem(LHS, URemLHS, URemRHS)) { |
0 |
| 15051 |
if (const SCEVUnknown *LHSUnknown = dyn_cast(URemLHS)) { |
0 |
15051 |
if (const SCEVUnknown *LHSUnknown = dyn_cast(URemLHS)) { |
0 |
| 15052 |
auto I = RewriteMap.find(LHSUnknown); |
0 |
15052 |
auto I = RewriteMap.find(LHSUnknown); |
0 |
| 15053 |
const SCEV *RewrittenLHS = |
--- |
15053 |
const SCEV *RewrittenLHS = |
--- |
| 15054 |
I != RewriteMap.end() ? I->second : LHSUnknown; |
0 |
15054 |
I != RewriteMap.end() ? I->second : LHSUnknown; |
0 |
| 15055 |
RewrittenLHS = ApplyDivisibiltyOnMinMaxExpr(RewrittenLHS, URemRHS); |
0 |
15055 |
RewrittenLHS = ApplyDivisibiltyOnMinMaxExpr(RewrittenLHS, URemRHS); |
0 |
| 15056 |
const auto *Multiple = |
--- |
15056 |
const auto *Multiple = |
--- |
| 15057 |
getMulExpr(getUDivExpr(RewrittenLHS, URemRHS), URemRHS); |
0 |
15057 |
getMulExpr(getUDivExpr(RewrittenLHS, URemRHS), URemRHS); |
0 |
| 15058 |
RewriteMap[LHSUnknown] = Multiple; |
0 |
15058 |
RewriteMap[LHSUnknown] = Multiple; |
0 |
| 15059 |
ExprsToRewrite.push_back(LHSUnknown); |
0 |
15059 |
ExprsToRewrite.push_back(LHSUnknown); |
0 |
| 15060 |
return; |
0 |
15060 |
return; |
0 |
| 15061 |
} |
--- |
15061 |
} |
--- |
| 15062 |
} |
--- |
15062 |
} |
--- |
| 15063 |
} |
--- |
15063 |
} |
--- |
| 15064 |
|
--- |
15064 |
|
--- |
| 15065 |
// Do not apply information for constants or if RHS contains an AddRec. |
--- |
15065 |
// Do not apply information for constants or if RHS contains an AddRec. |
--- |
| 15066 |
if (isa(LHS) || containsAddRecurrence(RHS)) |
0 |
15066 |
if (isa(LHS) || containsAddRecurrence(RHS)) |
0 |
| 15067 |
return; |
0 |
15067 |
return; |
0 |
| 15068 |
|
--- |
15068 |
|
--- |
| 15069 |
// If RHS is SCEVUnknown, make sure the information is applied to it. |
--- |
15069 |
// If RHS is SCEVUnknown, make sure the information is applied to it. |
--- |
| 15070 |
if (!isa(LHS) && isa(RHS)) { |
0 |
15070 |
if (!isa(LHS) && isa(RHS)) { |
0 |
| 15071 |
std::swap(LHS, RHS); |
0 |
15071 |
std::swap(LHS, RHS); |
0 |
| 15072 |
Predicate = CmpInst::getSwappedPredicate(Predicate); |
0 |
15072 |
Predicate = CmpInst::getSwappedPredicate(Predicate); |
0 |
| 15073 |
} |
--- |
15073 |
} |
--- |
| 15074 |
|
--- |
15074 |
|
--- |
| 15075 |
// Puts rewrite rule \p From -> \p To into the rewrite map. Also if \p From |
--- |
15075 |
// Puts rewrite rule \p From -> \p To into the rewrite map. Also if \p From |
--- |
| 15076 |
// and \p FromRewritten are the same (i.e. there has been no rewrite |
--- |
15076 |
// and \p FromRewritten are the same (i.e. there has been no rewrite |
--- |
| 15077 |
// registered for \p From), then puts this value in the list of rewritten |
--- |
15077 |
// registered for \p From), then puts this value in the list of rewritten |
--- |
| 15078 |
// expressions. |
--- |
15078 |
// expressions. |
--- |
| 15079 |
auto AddRewrite = [&](const SCEV *From, const SCEV *FromRewritten, |
0 |
15079 |
auto AddRewrite = [&](const SCEV *From, const SCEV *FromRewritten, |
0 |
| 15080 |
const SCEV *To) { |
--- |
15080 |
const SCEV *To) { |
--- |
| 15081 |
if (From == FromRewritten) |
0 |
15081 |
if (From == FromRewritten) |
0 |
| 15082 |
ExprsToRewrite.push_back(From); |
0 |
15082 |
ExprsToRewrite.push_back(From); |
0 |
| 15083 |
RewriteMap[From] = To; |
0 |
15083 |
RewriteMap[From] = To; |
0 |
| 15084 |
}; |
0 |
15084 |
}; |
0 |
| 15085 |
|
--- |
15085 |
|
--- |
| 15086 |
// Checks whether \p S has already been rewritten. In that case returns the |
--- |
15086 |
// Checks whether \p S has already been rewritten. In that case returns the |
--- |
| 15087 |
// existing rewrite because we want to chain further rewrites onto the |
--- |
15087 |
// existing rewrite because we want to chain further rewrites onto the |
--- |
| 15088 |
// already rewritten value. Otherwise returns \p S. |
--- |
15088 |
// already rewritten value. Otherwise returns \p S. |
--- |
| 15089 |
auto GetMaybeRewritten = [&](const SCEV *S) { |
0 |
15089 |
auto GetMaybeRewritten = [&](const SCEV *S) { |
0 |
| 15090 |
auto I = RewriteMap.find(S); |
0 |
15090 |
auto I = RewriteMap.find(S); |
0 |
| 15091 |
return I != RewriteMap.end() ? I->second : S; |
0 |
15091 |
return I != RewriteMap.end() ? I->second : S; |
0 |
| 15092 |
}; |
0 |
15092 |
}; |
0 |
| 15093 |
|
--- |
15093 |
|
--- |
| 15094 |
// Check for the SCEV expression (A /u B) * B while B is a constant, inside |
--- |
15094 |
// Check for the SCEV expression (A /u B) * B while B is a constant, inside |
--- |
| 15095 |
// \p Expr. The check is done recuresively on \p Expr, which is assumed to |
--- |
15095 |
// \p Expr. The check is done recuresively on \p Expr, which is assumed to |
--- |
| 15096 |
// be a composition of Min/Max SCEVs. Return whether the SCEV expression (A |
--- |
15096 |
// be a composition of Min/Max SCEVs. Return whether the SCEV expression (A |
--- |
| 15097 |
// /u B) * B was found, and return the divisor B in \p DividesBy. For |
--- |
15097 |
// /u B) * B was found, and return the divisor B in \p DividesBy. For |
--- |
| 15098 |
// example, if Expr = umin (umax ((A /u 8) * 8, 16), 64), return true since |
--- |
15098 |
// example, if Expr = umin (umax ((A /u 8) * 8, 16), 64), return true since |
--- |
| 15099 |
// (A /u 8) * 8 matched the pattern, and return the constant SCEV 8 in \p |
--- |
15099 |
// (A /u 8) * 8 matched the pattern, and return the constant SCEV 8 in \p |
--- |
| 15100 |
// DividesBy. |
--- |
15100 |
// DividesBy. |
--- |
| 15101 |
std::function HasDivisibiltyInfo = |
--- |
15101 |
std::function HasDivisibiltyInfo = |
--- |
| 15102 |
[&](const SCEV *Expr, const SCEV *&DividesBy) { |
0 |
15102 |
[&](const SCEV *Expr, const SCEV *&DividesBy) { |
0 |
| 15103 |
if (auto *Mul = dyn_cast(Expr)) { |
0 |
15103 |
if (auto *Mul = dyn_cast(Expr)) { |
0 |
| 15104 |
if (Mul->getNumOperands() != 2) |
0 |
15104 |
if (Mul->getNumOperands() != 2) |
0 |
| 15105 |
return false; |
0 |
15105 |
return false; |
0 |
| 15106 |
auto *MulLHS = Mul->getOperand(0); |
0 |
15106 |
auto *MulLHS = Mul->getOperand(0); |
0 |
| 15107 |
auto *MulRHS = Mul->getOperand(1); |
0 |
15107 |
auto *MulRHS = Mul->getOperand(1); |
0 |
| 15108 |
if (isa(MulLHS)) |
0 |
15108 |
if (isa(MulLHS)) |
0 |
| 15109 |
std::swap(MulLHS, MulRHS); |
0 |
15109 |
std::swap(MulLHS, MulRHS); |
0 |
| 15110 |
if (auto *Div = dyn_cast(MulLHS)) |
0 |
15110 |
if (auto *Div = dyn_cast(MulLHS)) |
0 |
| 15111 |
if (Div->getOperand(1) == MulRHS) { |
0 |
15111 |
if (Div->getOperand(1) == MulRHS) { |
0 |
| 15112 |
DividesBy = MulRHS; |
0 |
15112 |
DividesBy = MulRHS; |
0 |
| 15113 |
return true; |
0 |
15113 |
return true; |
0 |
| 15114 |
} |
--- |
15114 |
} |
--- |
| 15115 |
} |
--- |
15115 |
} |
--- |
| 15116 |
if (auto *MinMax = dyn_cast(Expr)) |
0 |
15116 |
if (auto *MinMax = dyn_cast(Expr)) |
0 |
| 15117 |
return HasDivisibiltyInfo(MinMax->getOperand(0), DividesBy) || |
0 |
15117 |
return HasDivisibiltyInfo(MinMax->getOperand(0), DividesBy) || |
0 |
| 15118 |
HasDivisibiltyInfo(MinMax->getOperand(1), DividesBy); |
0 |
15118 |
HasDivisibiltyInfo(MinMax->getOperand(1), DividesBy); |
0 |
| 15119 |
return false; |
0 |
15119 |
return false; |
0 |
| 15120 |
}; |
0 |
15120 |
}; |
0 |
| 15121 |
|
--- |
15121 |
|
--- |
| 15122 |
// Return true if Expr known to divide by \p DividesBy. |
--- |
15122 |
// Return true if Expr known to divide by \p DividesBy. |
--- |
| 15123 |
std::function IsKnownToDivideBy = |
--- |
15123 |
std::function IsKnownToDivideBy = |
--- |
| 15124 |
[&](const SCEV *Expr, const SCEV *DividesBy) { |
0 |
15124 |
[&](const SCEV *Expr, const SCEV *DividesBy) { |
0 |
| 15125 |
if (getURemExpr(Expr, DividesBy)->isZero()) |
0 |
15125 |
if (getURemExpr(Expr, DividesBy)->isZero()) |
0 |
| 15126 |
return true; |
0 |
15126 |
return true; |
0 |
| 15127 |
if (auto *MinMax = dyn_cast(Expr)) |
0 |
15127 |
if (auto *MinMax = dyn_cast(Expr)) |
0 |
| 15128 |
return IsKnownToDivideBy(MinMax->getOperand(0), DividesBy) && |
0 |
15128 |
return IsKnownToDivideBy(MinMax->getOperand(0), DividesBy) && |
0 |
| 15129 |
IsKnownToDivideBy(MinMax->getOperand(1), DividesBy); |
0 |
15129 |
IsKnownToDivideBy(MinMax->getOperand(1), DividesBy); |
0 |
| 15130 |
return false; |
0 |
15130 |
return false; |
0 |
| 15131 |
}; |
0 |
15131 |
}; |
0 |
| 15132 |
|
--- |
15132 |
|
--- |
| 15133 |
const SCEV *RewrittenLHS = GetMaybeRewritten(LHS); |
0 |
15133 |
const SCEV *RewrittenLHS = GetMaybeRewritten(LHS); |
0 |
| 15134 |
const SCEV *DividesBy = nullptr; |
0 |
15134 |
const SCEV *DividesBy = nullptr; |
0 |
| 15135 |
if (HasDivisibiltyInfo(RewrittenLHS, DividesBy)) |
0 |
15135 |
if (HasDivisibiltyInfo(RewrittenLHS, DividesBy)) |
0 |
| 15136 |
// Check that the whole expression is divided by DividesBy |
--- |
15136 |
// Check that the whole expression is divided by DividesBy |
--- |
| 15137 |
DividesBy = |
0 |
15137 |
DividesBy = |
0 |
| 15138 |
IsKnownToDivideBy(RewrittenLHS, DividesBy) ? DividesBy : nullptr; |
0 |
15138 |
IsKnownToDivideBy(RewrittenLHS, DividesBy) ? DividesBy : nullptr; |
0 |
| 15139 |
|
--- |
15139 |
|
--- |
| 15140 |
// Collect rewrites for LHS and its transitive operands based on the |
--- |
15140 |
// Collect rewrites for LHS and its transitive operands based on the |
--- |
| 15141 |
// condition. |
--- |
15141 |
// condition. |
--- |
| 15142 |
// For min/max expressions, also apply the guard to its operands: |
--- |
15142 |
// For min/max expressions, also apply the guard to its operands: |
--- |
| 15143 |
// 'min(a, b) >= c' -> '(a >= c) and (b >= c)', |
--- |
15143 |
// 'min(a, b) >= c' -> '(a >= c) and (b >= c)', |
--- |
| 15144 |
// 'min(a, b) > c' -> '(a > c) and (b > c)', |
--- |
15144 |
// 'min(a, b) > c' -> '(a > c) and (b > c)', |
--- |
| 15145 |
// 'max(a, b) <= c' -> '(a <= c) and (b <= c)', |
--- |
15145 |
// 'max(a, b) <= c' -> '(a <= c) and (b <= c)', |
--- |
| 15146 |
// 'max(a, b) < c' -> '(a < c) and (b < c)'. |
--- |
15146 |
// 'max(a, b) < c' -> '(a < c) and (b < c)'. |
--- |
| 15147 |
|
--- |
15147 |
|
--- |
| 15148 |
// We cannot express strict predicates in SCEV, so instead we replace them |
--- |
15148 |
// We cannot express strict predicates in SCEV, so instead we replace them |
--- |
| 15149 |
// with non-strict ones against plus or minus one of RHS depending on the |
--- |
15149 |
// with non-strict ones against plus or minus one of RHS depending on the |
--- |
| 15150 |
// predicate. |
--- |
15150 |
// predicate. |
--- |
| 15151 |
const SCEV *One = getOne(RHS->getType()); |
0 |
15151 |
const SCEV *One = getOne(RHS->getType()); |
0 |
| 15152 |
switch (Predicate) { |
0 |
15152 |
switch (Predicate) { |
0 |
| 15153 |
case CmpInst::ICMP_ULT: |
0 |
15153 |
case CmpInst::ICMP_ULT: |
0 |
| 15154 |
if (RHS->getType()->isPointerTy()) |
0 |
15154 |
if (RHS->getType()->isPointerTy()) |
0 |
| 15155 |
return; |
0 |
15155 |
return; |
0 |
| 15156 |
RHS = getUMaxExpr(RHS, One); |
0 |
15156 |
RHS = getUMaxExpr(RHS, One); |
0 |
| 15157 |
[[fallthrough]]; |
--- |
15157 |
[[fallthrough]]; |
--- |
| 15158 |
case CmpInst::ICMP_SLT: { |
0 |
15158 |
case CmpInst::ICMP_SLT: { |
0 |
| 15159 |
RHS = getMinusSCEV(RHS, One); |
0 |
15159 |
RHS = getMinusSCEV(RHS, One); |
0 |
| 15160 |
RHS = DividesBy ? GetPreviousSCEVDividesByDivisor(RHS, DividesBy) : RHS; |
0 |
15160 |
RHS = DividesBy ? GetPreviousSCEVDividesByDivisor(RHS, DividesBy) : RHS; |
0 |
| 15161 |
break; |
0 |
15161 |
break; |
0 |
| 15162 |
} |
--- |
15162 |
} |
--- |
| 15163 |
case CmpInst::ICMP_UGT: |
0 |
15163 |
case CmpInst::ICMP_UGT: |
0 |
| 15164 |
case CmpInst::ICMP_SGT: |
--- |
15164 |
case CmpInst::ICMP_SGT: |
--- |
| 15165 |
RHS = getAddExpr(RHS, One); |
0 |
15165 |
RHS = getAddExpr(RHS, One); |
0 |
| 15166 |
RHS = DividesBy ? GetNextSCEVDividesByDivisor(RHS, DividesBy) : RHS; |
0 |
15166 |
RHS = DividesBy ? GetNextSCEVDividesByDivisor(RHS, DividesBy) : RHS; |
0 |
| 15167 |
break; |
0 |
15167 |
break; |
0 |
| 15168 |
case CmpInst::ICMP_ULE: |
0 |
15168 |
case CmpInst::ICMP_ULE: |
0 |
| 15169 |
case CmpInst::ICMP_SLE: |
--- |
15169 |
case CmpInst::ICMP_SLE: |
--- |
| 15170 |
RHS = DividesBy ? GetPreviousSCEVDividesByDivisor(RHS, DividesBy) : RHS; |
0 |
15170 |
RHS = DividesBy ? GetPreviousSCEVDividesByDivisor(RHS, DividesBy) : RHS; |
0 |
| 15171 |
break; |
0 |
15171 |
break; |
0 |
| 15172 |
case CmpInst::ICMP_UGE: |
0 |
15172 |
case CmpInst::ICMP_UGE: |
0 |
| 15173 |
case CmpInst::ICMP_SGE: |
--- |
15173 |
case CmpInst::ICMP_SGE: |
--- |
| 15174 |
RHS = DividesBy ? GetNextSCEVDividesByDivisor(RHS, DividesBy) : RHS; |
0 |
15174 |
RHS = DividesBy ? GetNextSCEVDividesByDivisor(RHS, DividesBy) : RHS; |
0 |
| 15175 |
break; |
0 |
15175 |
break; |
0 |
| 15176 |
default: |
0 |
15176 |
default: |
0 |
| 15177 |
break; |
0 |
15177 |
break; |
0 |
| 15178 |
} |
--- |
15178 |
} |
--- |
| 15179 |
|
--- |
15179 |
|
--- |
| 15180 |
SmallVector Worklist(1, LHS); |
0 |
15180 |
SmallVector Worklist(1, LHS); |
0 |
| 15181 |
SmallPtrSet Visited; |
0 |
15181 |
SmallPtrSet Visited; |
0 |
| 15182 |
|
--- |
15182 |
|
--- |
| 15183 |
auto EnqueueOperands = [&Worklist](const SCEVNAryExpr *S) { |
0 |
15183 |
auto EnqueueOperands = [&Worklist](const SCEVNAryExpr *S) { |
0 |
| 15184 |
append_range(Worklist, S->operands()); |
0 |
15184 |
append_range(Worklist, S->operands()); |
0 |
| 15185 |
}; |
0 |
15185 |
}; |
0 |
| 15186 |
|
--- |
15186 |
|
--- |
| 15187 |
while (!Worklist.empty()) { |
0 |
15187 |
while (!Worklist.empty()) { |
0 |
| 15188 |
const SCEV *From = Worklist.pop_back_val(); |
0 |
15188 |
const SCEV *From = Worklist.pop_back_val(); |
0 |
| 15189 |
if (isa(From)) |
0 |
15189 |
if (isa(From)) |
0 |
| 15190 |
continue; |
0 |
15190 |
continue; |
0 |
| 15191 |
if (!Visited.insert(From).second) |
0 |
15191 |
if (!Visited.insert(From).second) |
0 |
| 15192 |
continue; |
0 |
15192 |
continue; |
0 |
| 15193 |
const SCEV *FromRewritten = GetMaybeRewritten(From); |
0 |
15193 |
const SCEV *FromRewritten = GetMaybeRewritten(From); |
0 |
| 15194 |
const SCEV *To = nullptr; |
0 |
15194 |
const SCEV *To = nullptr; |
0 |
| 15195 |
|
--- |
15195 |
|
--- |
| 15196 |
switch (Predicate) { |
0 |
15196 |
switch (Predicate) { |
0 |
| 15197 |
case CmpInst::ICMP_ULT: |
0 |
15197 |
case CmpInst::ICMP_ULT: |
0 |
| 15198 |
case CmpInst::ICMP_ULE: |
--- |
15198 |
case CmpInst::ICMP_ULE: |
--- |
| 15199 |
To = getUMinExpr(FromRewritten, RHS); |
0 |
15199 |
To = getUMinExpr(FromRewritten, RHS); |
0 |
| 15200 |
if (auto *UMax = dyn_cast(FromRewritten)) |
0 |
15200 |
if (auto *UMax = dyn_cast(FromRewritten)) |
0 |
| 15201 |
EnqueueOperands(UMax); |
0 |
15201 |
EnqueueOperands(UMax); |
0 |
| 15202 |
break; |
0 |
15202 |
break; |
0 |
| 15203 |
case CmpInst::ICMP_SLT: |
0 |
15203 |
case CmpInst::ICMP_SLT: |
0 |
| 15204 |
case CmpInst::ICMP_SLE: |
--- |
15204 |
case CmpInst::ICMP_SLE: |
--- |
| 15205 |
To = getSMinExpr(FromRewritten, RHS); |
0 |
15205 |
To = getSMinExpr(FromRewritten, RHS); |
0 |
| 15206 |
if (auto *SMax = dyn_cast(FromRewritten)) |
0 |
15206 |
if (auto *SMax = dyn_cast(FromRewritten)) |
0 |
| 15207 |
EnqueueOperands(SMax); |
0 |
15207 |
EnqueueOperands(SMax); |
0 |
| 15208 |
break; |
0 |
15208 |
break; |
0 |
| 15209 |
case CmpInst::ICMP_UGT: |
0 |
15209 |
case CmpInst::ICMP_UGT: |
0 |
| 15210 |
case CmpInst::ICMP_UGE: |
--- |
15210 |
case CmpInst::ICMP_UGE: |
--- |
| 15211 |
To = getUMaxExpr(FromRewritten, RHS); |
0 |
15211 |
To = getUMaxExpr(FromRewritten, RHS); |
0 |
| 15212 |
if (auto *UMin = dyn_cast(FromRewritten)) |
0 |
15212 |
if (auto *UMin = dyn_cast(FromRewritten)) |
0 |
| 15213 |
EnqueueOperands(UMin); |
0 |
15213 |
EnqueueOperands(UMin); |
0 |
| 15214 |
break; |
0 |
15214 |
break; |
0 |
| 15215 |
case CmpInst::ICMP_SGT: |
0 |
15215 |
case CmpInst::ICMP_SGT: |
0 |
| 15216 |
case CmpInst::ICMP_SGE: |
--- |
15216 |
case CmpInst::ICMP_SGE: |
--- |
| 15217 |
To = getSMaxExpr(FromRewritten, RHS); |
0 |
15217 |
To = getSMaxExpr(FromRewritten, RHS); |
0 |
| 15218 |
if (auto *SMin = dyn_cast(FromRewritten)) |
0 |
15218 |
if (auto *SMin = dyn_cast(FromRewritten)) |
0 |
| 15219 |
EnqueueOperands(SMin); |
0 |
15219 |
EnqueueOperands(SMin); |
0 |
| 15220 |
break; |
0 |
15220 |
break; |
0 |
| 15221 |
case CmpInst::ICMP_EQ: |
0 |
15221 |
case CmpInst::ICMP_EQ: |
0 |
| 15222 |
if (isa(RHS)) |
0 |
15222 |
if (isa(RHS)) |
0 |
| 15223 |
To = RHS; |
0 |
15223 |
To = RHS; |
0 |
| 15224 |
break; |
0 |
15224 |
break; |
0 |
| 15225 |
case CmpInst::ICMP_NE: |
0 |
15225 |
case CmpInst::ICMP_NE: |
0 |
| 15226 |
if (isa(RHS) && |
0 |
15226 |
if (isa(RHS) && |
0 |
| 15227 |
cast(RHS)->getValue()->isNullValue()) { |
0 |
15227 |
cast(RHS)->getValue()->isNullValue()) { |
0 |
| 15228 |
const SCEV *OneAlignedUp = |
--- |
15228 |
const SCEV *OneAlignedUp = |
--- |
| 15229 |
DividesBy ? GetNextSCEVDividesByDivisor(One, DividesBy) : One; |
0 |
15229 |
DividesBy ? GetNextSCEVDividesByDivisor(One, DividesBy) : One; |
0 |
| 15230 |
To = getUMaxExpr(FromRewritten, OneAlignedUp); |
0 |
15230 |
To = getUMaxExpr(FromRewritten, OneAlignedUp); |
0 |
| 15231 |
} |
--- |
15231 |
} |
--- |
| 15232 |
break; |
0 |
15232 |
break; |
0 |
| 15233 |
default: |
0 |
15233 |
default: |
0 |
| 15234 |
break; |
0 |
15234 |
break; |
0 |
| 15235 |
} |
--- |
15235 |
} |
--- |
| 15236 |
|
--- |
15236 |
|
--- |
| 15237 |
if (To) |
0 |
15237 |
if (To) |
0 |
| 15238 |
AddRewrite(From, FromRewritten, To); |
0 |
15238 |
AddRewrite(From, FromRewritten, To); |
0 |
| 15239 |
} |
--- |
15239 |
} |
--- |
| 15240 |
}; |
0 |
15240 |
}; |
0 |
| 15241 |
|
--- |
15241 |
|
--- |
| 15242 |
BasicBlock *Header = L->getHeader(); |
0 |
15242 |
BasicBlock *Header = L->getHeader(); |
0 |
| 15243 |
SmallVector> Terms; |
0 |
15243 |
SmallVector> Terms; |
0 |
| 15244 |
// First, collect information from assumptions dominating the loop. |
--- |
15244 |
// First, collect information from assumptions dominating the loop. |
--- |
| 15245 |
for (auto &AssumeVH : AC.assumptions()) { |
0 |
15245 |
for (auto &AssumeVH : AC.assumptions()) { |
0 |
| 15246 |
if (!AssumeVH) |
0 |
15246 |
if (!AssumeVH) |
0 |
| 15247 |
continue; |
0 |
15247 |
continue; |
0 |
| 15248 |
auto *AssumeI = cast(AssumeVH); |
0 |
15248 |
auto *AssumeI = cast(AssumeVH); |
0 |
| 15249 |
if (!DT.dominates(AssumeI, Header)) |
0 |
15249 |
if (!DT.dominates(AssumeI, Header)) |
0 |
| 15250 |
continue; |
0 |
15250 |
continue; |
0 |
| 15251 |
Terms.emplace_back(AssumeI->getOperand(0), true); |
0 |
15251 |
Terms.emplace_back(AssumeI->getOperand(0), true); |
0 |
| 15252 |
} |
--- |
15252 |
} |
--- |
| 15253 |
|
--- |
15253 |
|
--- |
| 15254 |
// Second, collect information from llvm.experimental.guards dominating the loop. |
--- |
15254 |
// Second, collect information from llvm.experimental.guards dominating the loop. |
--- |
| 15255 |
auto *GuardDecl = F.getParent()->getFunction( |
0 |
15255 |
auto *GuardDecl = F.getParent()->getFunction( |
0 |
| 15256 |
Intrinsic::getName(Intrinsic::experimental_guard)); |
--- |
15256 |
Intrinsic::getName(Intrinsic::experimental_guard)); |
--- |
| 15257 |
if (GuardDecl) |
0 |
15257 |
if (GuardDecl) |
0 |
| 15258 |
for (const auto *GU : GuardDecl->users()) |
0 |
15258 |
for (const auto *GU : GuardDecl->users()) |
0 |
| 15259 |
if (const auto *Guard = dyn_cast(GU)) |
0 |
15259 |
if (const auto *Guard = dyn_cast(GU)) |
0 |
| 15260 |
if (Guard->getFunction() == Header->getParent() && DT.dominates(Guard, Header)) |
0 |
15260 |
if (Guard->getFunction() == Header->getParent() && DT.dominates(Guard, Header)) |
0 |
| 15261 |
Terms.emplace_back(Guard->getArgOperand(0), true); |
0 |
15261 |
Terms.emplace_back(Guard->getArgOperand(0), true); |
0 |
| 15262 |
|
--- |
15262 |
|
--- |
| 15263 |
// Third, collect conditions from dominating branches. Starting at the loop |
--- |
15263 |
// Third, collect conditions from dominating branches. Starting at the loop |
--- |
| 15264 |
// predecessor, climb up the predecessor chain, as long as there are |
--- |
15264 |
// predecessor, climb up the predecessor chain, as long as there are |
--- |
| 15265 |
// predecessors that can be found that have unique successors leading to the |
--- |
15265 |
// predecessors that can be found that have unique successors leading to the |
--- |
| 15266 |
// original header. |
--- |
15266 |
// original header. |
--- |
| 15267 |
// TODO: share this logic with isLoopEntryGuardedByCond. |
--- |
15267 |
// TODO: share this logic with isLoopEntryGuardedByCond. |
--- |
| 15268 |
for (std::pair Pair( |
0 |
15268 |
for (std::pair Pair( |
0 |
| 15269 |
L->getLoopPredecessor(), Header); |
0 |
15269 |
L->getLoopPredecessor(), Header); |
0 |
| 15270 |
Pair.first; Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) { |
0 |
15270 |
Pair.first; Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) { |
0 |
| 15271 |
|
--- |
15271 |
|
--- |
| 15272 |
const BranchInst *LoopEntryPredicate = |
--- |
15272 |
const BranchInst *LoopEntryPredicate = |
--- |
| 15273 |
dyn_cast(Pair.first->getTerminator()); |
0 |
15273 |
dyn_cast(Pair.first->getTerminator()); |
0 |
| 15274 |
if (!LoopEntryPredicate || LoopEntryPredicate->isUnconditional()) |
0 |
15274 |
if (!LoopEntryPredicate || LoopEntryPredicate->isUnconditional()) |
0 |
| 15275 |
continue; |
0 |
15275 |
continue; |
0 |
| 15276 |
|
--- |
15276 |
|
--- |
| 15277 |
Terms.emplace_back(LoopEntryPredicate->getCondition(), |
0 |
15277 |
Terms.emplace_back(LoopEntryPredicate->getCondition(), |
0 |
| 15278 |
LoopEntryPredicate->getSuccessor(0) == Pair.second); |
0 |
15278 |
LoopEntryPredicate->getSuccessor(0) == Pair.second); |
0 |
| 15279 |
} |
--- |
15279 |
} |
--- |
| 15280 |
|
--- |
15280 |
|
--- |
| 15281 |
// Now apply the information from the collected conditions to RewriteMap. |
--- |
15281 |
// Now apply the information from the collected conditions to RewriteMap. |
--- |
| 15282 |
// Conditions are processed in reverse order, so the earliest conditions is |
--- |
15282 |
// Conditions are processed in reverse order, so the earliest conditions is |
--- |
| 15283 |
// processed first. This ensures the SCEVs with the shortest dependency chains |
--- |
15283 |
// processed first. This ensures the SCEVs with the shortest dependency chains |
--- |
| 15284 |
// are constructed first. |
--- |
15284 |
// are constructed first. |
--- |
| 15285 |
DenseMap RewriteMap; |
0 |
15285 |
DenseMap RewriteMap; |
0 |
| 15286 |
for (auto [Term, EnterIfTrue] : reverse(Terms)) { |
0 |
15286 |
for (auto [Term, EnterIfTrue] : reverse(Terms)) { |
0 |
| 15287 |
SmallVector Worklist; |
0 |
15287 |
SmallVector Worklist; |
0 |
| 15288 |
SmallPtrSet Visited; |
0 |
15288 |
SmallPtrSet Visited; |
0 |
| 15289 |
Worklist.push_back(Term); |
0 |
15289 |
Worklist.push_back(Term); |
0 |
| 15290 |
while (!Worklist.empty()) { |
0 |
15290 |
while (!Worklist.empty()) { |
0 |
| 15291 |
Value *Cond = Worklist.pop_back_val(); |
0 |
15291 |
Value *Cond = Worklist.pop_back_val(); |
0 |
| 15292 |
if (!Visited.insert(Cond).second) |
0 |
15292 |
if (!Visited.insert(Cond).second) |
0 |
| 15293 |
continue; |
0 |
15293 |
continue; |
0 |
| 15294 |
|
--- |
15294 |
|
--- |
| 15295 |
if (auto *Cmp = dyn_cast(Cond)) { |
0 |
15295 |
if (auto *Cmp = dyn_cast(Cond)) { |
0 |
| 15296 |
auto Predicate = |
--- |
15296 |
auto Predicate = |
--- |
| 15297 |
EnterIfTrue ? Cmp->getPredicate() : Cmp->getInversePredicate(); |
0 |
15297 |
EnterIfTrue ? Cmp->getPredicate() : Cmp->getInversePredicate(); |
0 |
| 15298 |
const auto *LHS = getSCEV(Cmp->getOperand(0)); |
0 |
15298 |
const auto *LHS = getSCEV(Cmp->getOperand(0)); |
0 |
| 15299 |
const auto *RHS = getSCEV(Cmp->getOperand(1)); |
0 |
15299 |
const auto *RHS = getSCEV(Cmp->getOperand(1)); |
0 |
| 15300 |
CollectCondition(Predicate, LHS, RHS, RewriteMap); |
0 |
15300 |
CollectCondition(Predicate, LHS, RHS, RewriteMap); |
0 |
| 15301 |
continue; |
0 |
15301 |
continue; |
0 |
| 15302 |
} |
0 |
15302 |
} |
0 |
| 15303 |
|
--- |
15303 |
|
--- |
| 15304 |
Value *L, *R; |
--- |
15304 |
Value *L, *R; |
--- |
| 15305 |
if (EnterIfTrue ? match(Cond, m_LogicalAnd(m_Value(L), m_Value(R))) |
0 |
15305 |
if (EnterIfTrue ? match(Cond, m_LogicalAnd(m_Value(L), m_Value(R))) |
0 |
| 15306 |
: match(Cond, m_LogicalOr(m_Value(L), m_Value(R)))) { |
0 |
15306 |
: match(Cond, m_LogicalOr(m_Value(L), m_Value(R)))) { |
0 |
| 15307 |
Worklist.push_back(L); |
0 |
15307 |
Worklist.push_back(L); |
0 |
| 15308 |
Worklist.push_back(R); |
0 |
15308 |
Worklist.push_back(R); |
0 |
| 15309 |
} |
--- |
15309 |
} |
--- |
| 15310 |
} |
--- |
15310 |
} |
--- |
| 15311 |
} |
0 |
15311 |
} |
0 |
| 15312 |
|
--- |
15312 |
|
--- |
| 15313 |
if (RewriteMap.empty()) |
0 |
15313 |
if (RewriteMap.empty()) |
0 |
| 15314 |
return Expr; |
0 |
15314 |
return Expr; |
0 |
| 15315 |
|
--- |
15315 |
|
--- |
| 15316 |
// Now that all rewrite information is collect, rewrite the collected |
--- |
15316 |
// Now that all rewrite information is collect, rewrite the collected |
--- |
| 15317 |
// expressions with the information in the map. This applies information to |
--- |
15317 |
// expressions with the information in the map. This applies information to |
--- |
| 15318 |
// sub-expressions. |
--- |
15318 |
// sub-expressions. |
--- |
| 15319 |
if (ExprsToRewrite.size() > 1) { |
0 |
15319 |
if (ExprsToRewrite.size() > 1) { |
0 |
| 15320 |
for (const SCEV *Expr : ExprsToRewrite) { |
0 |
15320 |
for (const SCEV *Expr : ExprsToRewrite) { |
0 |
| 15321 |
const SCEV *RewriteTo = RewriteMap[Expr]; |
0 |
15321 |
const SCEV *RewriteTo = RewriteMap[Expr]; |
0 |
| 15322 |
RewriteMap.erase(Expr); |
0 |
15322 |
RewriteMap.erase(Expr); |
0 |
| 15323 |
SCEVLoopGuardRewriter Rewriter(*this, RewriteMap); |
0 |
15323 |
SCEVLoopGuardRewriter Rewriter(*this, RewriteMap); |
0 |
| 15324 |
RewriteMap.insert({Expr, Rewriter.visit(RewriteTo)}); |
0 |
15324 |
RewriteMap.insert({Expr, Rewriter.visit(RewriteTo)}); |
0 |
| 15325 |
} |
0 |
15325 |
} |
0 |
| 15326 |
} |
--- |
15326 |
} |
--- |
| 15327 |
|
--- |
15327 |
|
--- |
| 15328 |
SCEVLoopGuardRewriter Rewriter(*this, RewriteMap); |
0 |
15328 |
SCEVLoopGuardRewriter Rewriter(*this, RewriteMap); |
0 |
| 15329 |
return Rewriter.visit(Expr); |
0 |
15329 |
return Rewriter.visit(Expr); |
0 |
| 15330 |
} |
0 |
15330 |
} |
0 |
| 15331 |
|
--- |
15331 |
|
--- |